为了账号安全,请及时绑定邮箱和手机立即绑定

最简单的springcloud来了,一起来学习吧!

标签:
Spring Cloud

ribbon简介

Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients. Feign already uses Ribbon, so if you are using @FeignClient then this section also applies.

—–摘自官网

ribbon是一个负载均衡客户端,可以很好的控制htt和tcp的一些行为。Feign默认集成了ribbon。

ribbon 已经默认实现了这些配置bean:

IClientConfig ribbonClientConfig: DefaultClientConfigImpl

IRule ribbonRule: ZoneAvoidanceRule

IPing ribbonPing: NoOpPing

ServerList ribbonServerList: ConfigurationBasedServerList

ServerListFilter ribbonServerListFilter: ZonePreferenceServerListFilter

ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer

二、准备工作

这一篇文章基于上一篇文章的工程,启动eureka-server 工程;启动service-hi工程,它的端口为8762;将service-hi的配置文件的端口改为8763,并启动,这时你会发现:service-hi在eureka-server注册了2个实例,这就相当于一个小的集群。访问localhost:8761如图所示:

如何一个工程启动多个实例,请看这篇文章:https://blog.csdn.net/forezp/article/details/76408139

webp

三、建一个服务消费者

重新新建一个spring-boot工程,取名为:service-ribbon; 

在它的pom.xml文件分别引入起步依赖spring-cloud-starter-eureka、spring-cloud-starter-ribbon、spring-boot-starter-web,代码如下:

4.0.0com.forezpservice-ribbon0.0.1-SNAPSHOTjarservice-ribbonDemo project for Spring Bootorg.springframework.bootspring-boot-starter-parent1.5.2.RELEASEUTF-8UTF-81.8org.springframework.cloudspring-cloud-starter-eurekaorg.springframework.cloudspring-cloud-starter-ribbonorg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-testtestorg.springframework.cloudspring-cloud-dependenciesDalston.RC1pomimportorg.springframework.bootspring-boot-maven-pluginspring-milestonesSpring Milestoneshttps://repo.spring.io/milestonefalse

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

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

在工程的配置文件指定服务的注册中心地址为http://localhost:8761/eureka/,程序名称为 service-ribbon,程序端口为8764。配置文件application.yml如下:

eureka:client:    serviceUrl:      defaultZone: http://localhost:8761/eureka/server:  port:8764spring:  application:    name: service-ribbon

1

2

3

4

5

6

7

8

9

在工程的启动类中,通过@EnableDiscoveryClient向服务中心注册;并且向程序的ioc注入一个bean: restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。

@SpringBootApplication@EnableDiscoveryClientpublicclassServiceRibbonApplication{publicstaticvoidmain(String[] args) {        SpringApplication.run(ServiceRibbonApplication.class, args);    }@Bean@LoadBalancedRestTemplate restTemplate() {returnnewRestTemplate();    }}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

写一个测试类HelloService,通过之前注入ioc容器的restTemplate来消费service-hi服务的“/hi”接口,在这里我们直接用的程序名替代了具体的url地址,在ribbon中它会根据服务名来选择具体的服务实例,根据服务实例在请求的时候会用具体的url替换掉服务名,代码如下:

@ServicepublicclassHelloService{@AutowiredRestTemplate restTemplate;publicStringhiService(String name) {returnrestTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);    }}

1

2

3

4

5

6

7

8

9

10

11

12

写一个controller,在controller中用调用HelloService 的方法,代码如下:

/**

* Created by fangzhipeng on 2017/4/6.

*/@RestControllerpublicclassHelloControler{@AutowiredHelloService helloService;@RequestMapping(value ="/hi")publicStringhi(@RequestParam String name){returnhelloService.hiService(name);    }}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

在浏览器上多次访问http://localhost:8764/hi?name=forezp,浏览器交替显示:

hi forezp,i am from port:8762

hi forezp,i am from port:8763

这说明当我们通过调用restTemplate.getForObject(“http://SERVICE-HI/hi?name=“+name,String.class)方法时,已经做了负载均衡,访问了不同的端口的服务实例。



作者:清风_d587
链接:https://www.jianshu.com/p/36e2a4b2926a


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消