前言:微服務(wù)架構(gòu),不可避免的存在單個(gè)微服務(wù)有多個(gè)實(shí)例,那么客戶端如何將請求分?jǐn)偟蕉鄠€(gè)微服務(wù)的實(shí)例上呢?這里我們就需要使用負(fù)載均衡了
一、ribbon簡介
ribbon是netflix發(fā)布的負(fù)載均衡器,它有助于控制http和tcp客戶端的行為。為ribbon配置服務(wù)提供者地址列表后,ribbon就可基于某種負(fù)載均衡算法,自動地幫助服務(wù)消費(fèi)者去請求。ribbon默認(rèn)為我們提供了很多的負(fù)載均衡算法,例如:輪詢,隨機(jī)等,也可自定義;
ribbon的github: https://github.com/netflix/ribbon
而在springcloud中使用ribbon和eureka時(shí),ribbon會自動從eurekaserver中獲取服務(wù)提供者地址列表,并基于負(fù)載均衡算法。
二、ribbon實(shí)戰(zhàn)
?1、創(chuàng)建eurekaserver,eurekaclient1,eurekaclient2,之前已經(jīng)說過了eureka的使用,這里直接上代碼:
eurekaserver:
serverapplication.java
1
2
3
4
5
6
7
8
9
|
@springbootapplication @enableeurekaserver public class serverapplication { public static void main(string[] args) { springapplication.run(serverapplication. class ,args); } } |
pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
<?xml version= "1.0" encoding= "utf-8" ?> <project xmlns= "http://maven.apache.org/pom/4.0.0" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation= "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion> 4.0 . 0 </modelversion> <groupid>com.cn</groupid> <artifactid>eureka-ribbon-server</artifactid> <version> 1.0 -snapshot</version> <properties> <project.build.sourceencoding>utf- 8 </project.build.sourceencoding> <java.version> 1.8 </java.version> </properties> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 1.5 . 13 .release</version> </parent> <dependencies> <dependency> <groupid>org.springframework.</groupid> <artifactid></artifactid> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-eureka-server</artifactid> </dependency> </dependencies> <dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-dependencies</artifactid> <version>edgware.sr3</version> <type>pom</type> <scope> import </scope> </dependency> </dependencies> </dependencymanagement> <!-- 添加spring-boot的maven插件 --> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project> |
application.properties
1
2
3
4
5
6
7
8
|
server.port= 8761 #注意:這兩個(gè)配置eureka默認(rèn)為 true ,要改成 false ,否則會報(bào)錯(cuò),connot connect server #表示是否將自己注冊在eurekaserver上 eureka.client.register-with-eureka= false #表示是否從eurekaserver獲取注冊信息 eureka.client.fetch-registry= false eureka.client.service-url.defaultzone=http: //localhost:8761/eureka/ |
eurekaclient1:
pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
<?xml version= "1.0" encoding= "utf-8" ?> <project xmlns= "http://maven.apache.org/pom/4.0.0" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation= "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion> 4.0 . 0 </modelversion> <groupid>com.cn</groupid> <artifactid>eureka-ribbon-client</artifactid> <version> 1.0 -snapshot</version> <properties> <project.build.sourceencoding>utf- 8 </project.build.sourceencoding> <java.version> 1.8 </java.version> </properties> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 1.5 . 13 .release</version> </parent> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-ribbon</artifactid> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-eureka</artifactid> </dependency> </dependencies> <dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-dependencies</artifactid> <version>edgware.sr3</version> <type>pom</type> <scope> import </scope> </dependency> </dependencies> </dependencymanagement> <!-- 添加spring-boot的maven插件 --> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project> |
1
2
3
|
server.port= 8762 spring.application.name=client- 8762 eureka.client.service-url.defaultzone=http: //localhost:8761/eureka/ |
而在啟動類中加入resttemplate遠(yuǎn)程調(diào)用實(shí)例到容器中,并且 添加loadbalanced注解,使resttemplate具備負(fù)載均衡的能力 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
@springbootapplication @enablediscoveryclient public class clientapplication { public static void main(string[] args) { springapplication.run(clientapplication. class , args); } /** * @description: 加入@loadbalanced注解,就可以為resttemplate加入負(fù)載均衡的能力 * @param: * @return: * @author: * @date: 2018/6/15 */ @bean @loadbalanced public resttemplate getresttemplate() { return new resttemplate(); } } |
創(chuàng)建controller,注入resttemplate、loadbalancerclient實(shí)例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
package com.cn.controller; import org.springframework.beans.factory.annotation.autowired; import org.springframework.cloud.client.serviceinstance; import org.springframework.cloud.client.loadbalancer.loadbalancerclient; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.responsebody; import org.springframework.web.client.resttemplate; /** * @program: springcloud-example * @description: * @author: * @create: 2018-06-15 15:55 **/ @controller public class ribboncontroller { @autowired private loadbalancerclient loadbalancerclient; @autowired private resttemplate resttemplate; @getmapping ( "/loadinstance" ) @responsebody public string loadinstance() { serviceinstance choose = this .loadbalancerclient.choose( "client-87" ); system.out.println(choose.getserviceid()+ ":" +choose.gethost()+ ":" +choose.getport()); return choose.getserviceid() + ":" + choose.gethost() + ":" + choose.getport(); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.cn; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.cloud.client.discovery.enablediscoveryclient; /** * @program: springcloud-example * @description: * @author: 535504 * @create: 2018-06-15 16:05 **/ @springbootapplication @enablediscoveryclient public class clientapplication { public static void main(string[] args) { springapplication.run(clientapplication. class , args); } } |
eurekaclient2:
pom.xml與eurekaclient1中一致
application.xml:
1
2
3
|
server.port= 8763 spring.application.name=client- 87 eureka.client.service-url.defaultzone=http: //localhost:8761/eureka |
clientcontroller.java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package com.cn.contorller; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.responsebody; /** * @program: springcloud-example * @description: * @author: * @create: 2018-06-15 16:12 **/ @controller public class clientcontroller { @getmapping ( "/getuser" ) @responsebody public string getuser() { system.out.println( "獲取用戶成功" ); return "獲取用戶成功" ; } } |
2、啟動順序:
①、依次啟動eurekaserver =》 eurekaclient1 =》 eurekaclient2 ;
②、然后將eurekaclient2中的application.properties的server.port=8763改為server.port=8764,再次啟動該項(xiàng)目;
③、打開eurekaserver的配置頁面(http://localhost:8761/),如下:
④、我們在地址欄輸入http://localhost:8762/loadinstance,多刷新幾次,會發(fā)現(xiàn)每次調(diào)用的端口實(shí)例都不同,如下圖:
⑤、我們在看控制臺,如圖:
至此,ribbon已經(jīng)入門,是不是很簡單,但是這只是最簡單的應(yīng)用,九牛一毛爾...學(xué)無止境乎!
示例代碼:https://gitee.com/lfalex/springcloud-example
原文鏈接:http://www.cnblogs.com/lfalex0831/p/9188214.html