ภาพรวม
ใช้ตัวอย่างง่ายๆเพื่อแสดงให้เห็นถึงการใช้งานพื้นฐานของยูเรก้าและริบบิ้นในสปริงเมฆ
เวอร์ชันและสภาพแวดล้อม
สร้างเซิร์ฟเวอร์ยูเรก้า
ในสปริงคลาวด์คุณสามารถใช้ยูเรก้าเพื่อจัดการไมโครไซต์และสามารถลงทะเบียนไมโครเซิร์ดกับยูเรก้าได้
ก่อนอื่นคุณสามารถใช้ Spring Initialzr ของ Idea เพื่อสร้างศูนย์ลงทะเบียนเซิร์ฟเวอร์ยูเรก้า
แก้ไขไฟล์ Application.properties และเพิ่มเนื้อหาต่อไปนี้
Spring.application.name = Eureka-Server Eureka.Instance.hostName = localhost Eureka.client.register-with-eureka = false eureka.client.fetch-registry = false server.port = 8881
เพิ่ม @EnableEurekaServer Annotation ลงในคลาสเริ่มต้น ServerApplication ที่สร้างโดย Spring Boot สำหรับเรา
แพ็คเกจ com.springcloud.eureka; นำเข้า org.springframework.boot.springapplication; นำเข้า org.springframework.boot.autoconfigure.springbootapplication; org.springframework.cloud.netflix.eureka.server.enableeurekaserver;@enableeurekaserver@springbootapplicationpublic คลาสเซิร์ฟเวอร์การเติมเต็ม {โมฆะสาธารณะคงที่หลัก (สตริง [] args) {springapplication.run -ป้อน http: // localhost: 8881/ในเบราว์เซอร์ของคุณ
คุณสามารถดูอินเทอร์เฟซต่อไปนี้:
คุณจะเห็นว่าบริการยังไม่ได้ลงทะเบียน ณ จุดนี้มีการสร้างศูนย์ลงทะเบียน Microservice อย่างง่าย
การเขียน Microservice Userservice
ถัดไปใช้ที่เหลือเพื่อสร้างอินเทอร์เฟซ microservice และลงทะเบียนในศูนย์การลงทะเบียน ยังคงใช้ Spring Initialzr เพื่อสร้างโครงการใหม่ การใช้งานเหมือนกับข้างต้น
โปรดทราบว่าคราวนี้คุณต้องตรวจสอบองค์ประกอบ Eureka Discovery แทนที่จะเป็น Eureka Server
แก้ไขไฟล์ Application.properties และเพิ่มเนื้อหาต่อไปนี้:
spring.application.name = user server.port = 8882 Eureka.client.service-url.defaultzone = http: // localhost: 8881/eureka //
ใช้คำอธิบายประกอบ @EnableDiscoveryClient ในคลาส UserApplication ที่ Spring Boot สร้างขึ้นสำหรับเรา
แพ็คเกจ com.springcloud; นำเข้า org.springframework.boot.springapplication; นำเข้า org.springframework.boot.autoconfigure.springbootapplication; นำเข้า org.springframework.cloud.client.discovery. หลัก (สตริง [] args) {springapplication.run (userapplication.class, args); -สร้างอินเทอร์เฟซ Microservice แบบเต็ม
แพ็คเกจ com.springcloud; นำเข้า org.springframework.web.bind.annotation.getMapping; นำเข้า org.springframework.web.bind.annotation.restcontroller; @restcontrollerpublic usercontroller {@getMapping ( -หลังจากเรียกใช้ userapplication ไปที่ http: // localhost: 8881/อีกครั้ง
คุณจะพบว่ามีการลงทะเบียนบริการผู้ใช้แล้ว
การเขียนคำสั่ง microservice
ต่อไปเราจะสร้างคำสั่งซื้อ microservice และเข้าถึงอินเทอร์เฟซใน Microservice ผู้ใช้
ยังคงใช้ Spring Initialzr เพื่อสร้างโครงการใหม่ Microservice ผู้ใช้สามารถปรับใช้ในหลายเครื่อง เมื่อไคลเอนต์เข้าถึงบริการนี้คำขอสามารถส่งไปยังเครื่องใด ๆ ที่ใช้บริการผู้ใช้ ดังนั้นลูกค้าจำเป็นต้องใช้อัลกอริทึมการกำหนดเส้นทางเพื่อกำหนดเวลาการบริการผู้ใช้ ในสปริงคลาวด์คุณสามารถใช้ส่วนประกอบริบบิ้นเพื่อกำหนดเส้นทางไคลเอนต์ Ribbon จะไปที่ศูนย์ลงทะเบียนบริการเพื่อรับรายการบริการเพื่อโทรหาบริการที่เกี่ยวข้อง
เวลานี้นอกเหนือจากการตรวจสอบองค์ประกอบ Eureka Discovery คุณต้องตรวจสอบ Ribbon
แก้ไขไฟล์ Application.properties และเพิ่มเนื้อหาต่อไปนี้:
spring.application.name = order server.port = 8883 Eureka.client.service-url.defaultzone = http: // localhost: 8881/eureka/
เพิ่มการกำหนดค่าต่อไปนี้ในคลาส OrderApplication ที่สร้างโดย Spring Boot
แพ็คเกจ com.springboot; นำเข้า org.springframework.boot.springapplication; นำเข้า org.springframework.boot.autoconfigure.springbootapplication; นำเข้า org.springframework.cloud.client.discovery. org.springframework.context.annotation.bean; นำเข้า org.springframework.web.client.resttemplate; @enablediscoveryclient @springbootapplicationpublic class ordplication } โมฆะคงที่สาธารณะหลัก (สตริง [] args) {springapplication.run (orderapplication.class, args); - เนื่องจากมีการใช้ริบบิ้นจึงต้องใช้คำอธิบายประกอบ @LoadBalanced ที่นี่
เขียน OrderController
แพ็คเกจ com.springboot; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.web.bind.annotation.getMapping; นำเข้า org.springframework.web.bind.annotation.restcontroller; org.springframework.web.client.resttemplate; @restcontrollerpublic คลาส OrderController {@autowired ส่วนตัว RestTemplate RestTemplate; @getMapping ("/getOrderUser") สตริงสาธารณะ getOrderUser () {return restTemplate.getForentity ("http: // user/getUser", string.class) .getBody (); - หลังจากรัน OrderApplication ให้ไปที่ http: // localhost: 8881/
คุณจะพบว่าบริการสั่งซื้อได้รับการลงทะเบียนในศูนย์ลงทะเบียน
ต่อไปเราเข้าถึงวิธี getOrderUser ใน OrderController และเรียกใช้การโทรไปยังวิธี getUser ของ UserController
Enter: http: // localhost: 8883/getOrderUser
คุณจะเห็นว่ามันส่งคืน: ฉันเป็นรายชื่อผู้ใช้
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น