คำนำ: สถาปัตยกรรม Microservice ย่อมมีหลายกรณีของ microservice เดียว ดังนั้นไคลเอนต์จะจัดสรรคำขอให้กับอินสแตนซ์ของไมโครไซต์หลายตัวได้อย่างไร ที่นี่เราต้องใช้การปรับสมดุลโหลด
1. บทนำสู่ริบบิ้น
Ribbon เป็นตัวโหลดบาลานซ์ที่ออกโดย Netflix ซึ่งช่วยควบคุมพฤติกรรมของไคลเอนต์ HTTP และ TCP หลังจากกำหนดค่ารายการที่อยู่ผู้ให้บริการสำหรับริบบิ้นริบบิ้นสามารถช่วยให้ผู้บริโภคให้บริการโดยอัตโนมัติตามการร้องขอตามอัลกอริทึมการโหลดบาลานซ์ ริบบิ้นให้อัลกอริทึมการปรับสมดุลโหลดจำนวนมากแก่เราโดยค่าเริ่มต้นเช่นการสำรวจการสุ่ม ฯลฯ ซึ่งสามารถปรับแต่งได้เช่นกัน
GitHub ของ Ribbon: https://github.com/netflix/ribbon
เมื่อใช้ Ribbon และ Eureka ใน SpringCloud Ribbon จะได้รับรายการที่อยู่ผู้ให้บริการจาก Eurekaserver โดยอัตโนมัติและขึ้นอยู่กับอัลกอริทึมการปรับสมดุลโหลด
2. ริบบิ้นการต่อสู้จริง
1. สร้าง Eurekaserver, Eurekaclient1, Eurekaclient2 ฉันได้กล่าวถึงการใช้ยูเรก้ามาก่อนดังนั้นนี่คือรหัส:
Eurekaserver:
serverapplication.java
@springbootapplication@enableeurekaserverpublic คลาส Serverapplication {โมฆะคงที่สาธารณะหลัก (สตริง [] args) {springapplication.run (serverapplication.class, args); -pom.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns: xsi = "http://www.w3.org/2001/ XSI: schemalocation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" <ArtIfactId> Eureka-Ribbon-Server </artifactid> <sersion> 1.0-snapshot </เวอร์ชัน> <properties> <project.build.sourceencoding> UTF-8 </project.build.sourceencoding> <RoupID> org.springframework.boot </groupid> <ratifactid> Spring-Boot-Starter-Parent </Artifactid> <Sersion> 1.5.13.release </เวอร์ชัน> </Parent> <RoupID> org.springframework.cloud </groupId> <ratifactid> Spring-Cloud-Starter-Eureka-Server </artifactid> </การพึ่งพาอาศัย> </การพึ่งพาอาศัย> </การพึ่งพา> <ArtIfactId> สปริงคลาวด์-พึ่งพาอาศัยกัน </artifactid> <persion> edgware.sr3 </เวอร์ชัน> <ply> pom </type> <scope> นำเข้า </cope> </predency> <ArtIfactId> Spring-Boot-Maven-Plugin </artifactid> </plugin> </plugins> </uffer
Application.properties
Server.port = 8761#หมายเหตุ: การกำหนดค่าทั้งสองนี้เป็นจริงโดยค่าเริ่มต้น คุณต้องเปลี่ยนเป็นเท็จมิฉะนั้นจะมีการรายงานข้อผิดพลาด Connot Connect Server# ระบุว่าจะลงทะเบียนตัวเองบน Eurekaserver Eureka.Client.register-with-eureka = false# หมายถึงว่าจะได้รับข้อมูลการลงทะเบียนจาก Eurekaserver Eureka.client.fetch-Registry = FalseeUeReKa.Client.Service-Url.Defaultzone
Eurekaclient1:
pom.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns: xsi = "http://www.w3.org/2001/ XSI: schemalocation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" <ArtifactId> Eureka-ribbon-client </artifactid> <sersion> 1.0-snapshot </เวอร์ชัน> <properties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <RoupID> org.springframework.boot </groupId> <ratifactid> Spring-Boot-Starter-Parent </artifactid> <sersion> 1.5.13.release </เวอร์ชัน> </parent> </predency> <การพึ่งพา> <roupId> org.springframework.cloud </groupid> <ratifactid> Spring-Cloud-Starter-Eureka </artifactid> </การพึ่งพาอาศัย> <ArtIfactId> สปริงคลาวด์-พึ่งพาอาศัยกัน </artifactid> <persion> edgware.sr3 </เวอร์ชัน> <ply> pom </type> <scope> นำเข้า </cope> </predency> <ArtIfactId> Spring-Boot-Maven-Plugin </artifactid> </plugin> </plugins> </uffer
Server.port = 8762Spring.application.name = client-8762eureka.client.service-url.defaultzone = http: // localhost: 8761/eureka/
เพิ่มอินสแตนซ์การโทรระยะไกลของ RestTemplate ลงในคอนเทนเนอร์ในคลาสเริ่มต้นและเพิ่มคำอธิบายประกอบการโหลดเพื่อให้ RestTemplate มีความสามารถในการโหลดบาลานซ์:
@springbootapplication@enablecoveryClientPublic คลาสไคลเอ็นต์ {โมฆะคงที่สาธารณะหลัก (สตริง [] args) {springapplication.run (clientapplication.class, args); }/** * @description: การเพิ่มคำอธิบายประกอบ @loadbalanced เพื่อเพิ่มความสามารถในการโหลดบาลานซ์ในการวาง RestTemplate * @param: * @return: * @author: * @date: 2018/6/15 */@bean -สร้างคอนโทรลเลอร์อินสแตนซ์ resttemplate และอินสแตนซ์ loadbalancerclient:
แพ็คเกจ com.cn.controller; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.cloud.client.serviceInstance; นำเข้า org.springframework.client.load.loadbalancer.loadbalancercercercercercercercercercercercercercercercercercercercercercercercercercercercercercercernt org.springframework.web.bind.annotation.getMapping; นำเข้า org.springframework.web.bind.annotation.responsebody; นำเข้า org.springframework.web.client.resttemplate;/** * @program 15:55 **/ @controllerpublic คลาส Ribboncontroller {@autowired Private Loadbalancerclient loadbalancerclient; @AutoWired RestTemplate RestTemplate; @getMapping ("/loadInstance") @ResponseBody สตริงสาธารณะ LoadInstance () {ServiceInstance เลือก = this.loadbalancerlient.choose ("client-87"); System.out.println (เลือก. getServiceId ()+":"+soope.getHost ()+":"+sole.get.getport ()); return solees.getServiceId () + ":" + เลือก. getHost () + ":" + sole.getport (); - แพ็คเกจ com.cn; นำเข้า org.springframework.boot.springapplication; นำเข้า org.springframework.boot.autoconfigure.springbootapplication; นำเข้า org.springframework.cloud.client.discovery.enablediscovery 535504 *@create: 2018-06-15 16:05 **/@springbootapplication@enablediscoveryClientPublic คลาสไคลเอ็นต์ {โมฆะคงที่สาธารณะหลัก (สตริง [] args) {springapplication.run (clientapplication.class, args); -Eurekaclient2:
pom.xml สอดคล้องกับ Eurekaclient1
Application.xml:
Server.port = 8763Spring.application.name = client-87eureka.client.service-url.defaultzone = http: // localhost: 8761/ureka
clientController.java:
แพ็คเกจ com.cn.contorller; นำเข้า org.springframework.steretype.controller; นำเข้า org.springframework.web.bind.annotation.getMapping; นำเข้า org.springframework.web.bind.annotation.responsebody; @Create: 2018-06-15 16:12 **/ @controllerpublic คลาส clientController {@getMapping ("/getUser") @ResponseBody สตริงสาธารณะ getUser () {system.out.println ("รับผู้ใช้สำเร็จ"); กลับ "รับผู้ใช้สำเร็จ"; -2. ลำดับการเริ่มต้น:
①. เริ่มต้น Eurekaserver =》 Eurekaclient1 =》 Eurekaclient2;
②. จากนั้นเปลี่ยน Server.port = 8763 ของ Application.properties ใน Eurekaclient2 เป็น Server.port = 8764 และเริ่มโครงการอีกครั้ง
③. เปิดหน้าการกำหนดค่า Eurekaserver (http: // localhost: 8761/) ดังนี้:
④. เราป้อน http: // localhost: 8762/loadinstance ในแถบที่อยู่และรีเฟรชสองสามครั้ง คุณจะพบว่าอินสแตนซ์ของพอร์ตแตกต่างกันในแต่ละครั้งที่เราเรียกว่าดังแสดงในรูปด้านล่าง:
⑤. เรากำลังดูคอนโซลดังที่แสดงในภาพ:
ณ จุดนี้ริบบิ้นได้เริ่มขึ้นแล้ว มันไม่ง่ายมากเหรอ? แต่นี่เป็นเพียงแอปพลิเคชั่นที่ง่ายที่สุดและเสียเวลา ... ไม่มีที่สิ้นสุดในการเรียนรู้!
รหัสตัวอย่าง: https://gitee.com/lfalex/springcloud-example