Spring Cloud(服务的消费者 ribbon+restTemplate)
2018-08-30 17:54
921 查看
一.ribbon简介
ribbon是一个负载均衡客户端,可以很好的控制htt和tcp的一些行为。Feign默认集成了ribbon。
二.准备工作
启动eureka-server 工程;启动service-hi工程,它的端口为8762;将service-hi的配置文件的端口改为8763,并启动,这时你会发现:service-hi在eureka-server注册了2个实例,这就相当于一个小的集群。访问localhost:8761如图所示:
番外:IDEA启动多个工程实例
选择Edit Configurations
去掉右上角的Single Instance only(单实例)的对勾,此时便可以通过修改application.yml中的端口号来启动多个工程。
三、建一个服务消费者
新建一个spring-boot工程,取名为:service-ribbon; 在它的pom.xml文件分别引入起步依赖spring-cloud-starter-eureka、spring-cloud-starter-ribbon、spring-boot-starter-web
在工程的配置文件指定服务的注册中心地址为http://localhost:8761/eureka/,程序名称为 service-ribbon,程序端口为8764
在工程的启动类中,通过@EnableDiscoveryClient向服务中心注册;并且向程序的ioc注入一个bean: restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。
写一个测试类HelloService,通过之前注入ioc容器的restTemplate来消费service-hi服务的“/hi”接口,在这里我们直接用的程序名替代了具体的url地址,在ribbon中它会根据服务名来选择具体的服务实例,根据服务实例在请求的时候会用具体的url替换掉服务名
写一个controller,在controller中用调用HelloService 的方法
在浏览器上多次访问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)方法时,已经做了负载均衡,访问了不同的端口的服务实例。
ribbon是一个负载均衡客户端,可以很好的控制htt和tcp的一些行为。Feign默认集成了ribbon。
二.准备工作
启动eureka-server 工程;启动service-hi工程,它的端口为8762;将service-hi的配置文件的端口改为8763,并启动,这时你会发现:service-hi在eureka-server注册了2个实例,这就相当于一个小的集群。访问localhost:8761如图所示:
番外:IDEA启动多个工程实例
选择Edit Configurations
去掉右上角的Single Instance only(单实例)的对勾,此时便可以通过修改application.yml中的端口号来启动多个工程。
三、建一个服务消费者
新建一个spring-boot工程,取名为:service-ribbon; 在它的pom.xml文件分别引入起步依赖spring-cloud-starter-eureka、spring-cloud-starter-ribbon、spring-boot-starter-web
在工程的配置文件指定服务的注册中心地址为http://localhost:8761/eureka/,程序名称为 service-ribbon,程序端口为8764
在工程的启动类中,通过@EnableDiscoveryClient向服务中心注册;并且向程序的ioc注入一个bean: restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。
写一个测试类HelloService,通过之前注入ioc容器的restTemplate来消费service-hi服务的“/hi”接口,在这里我们直接用的程序名替代了具体的url地址,在ribbon中它会根据服务名来选择具体的服务实例,根据服务实例在请求的时候会用具体的url替换掉服务名
写一个controller,在controller中用调用HelloService 的方法
在浏览器上多次访问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)方法时,已经做了负载均衡,访问了不同的端口的服务实例。
相关文章推荐
- 玩转SpringCloud(F版本) 二.服务消费者(1)ribbon+restTemplate
- SpringCloud教程第二篇:服务消费者(rest+ribbon)
- SpringCloud 查找调用REST服务使用RestTemplate(ribbon负载)或feign模式 教程源码 火推
- SpringCloud教程二:服务消费者(rest+ribbon)
- spring cloud RestTemplate消费者使用Hystrix进行容错和服务降级
- SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)(Finchley版本)
- SpringCloud学习教程二( 服务消费者rest+ribbon )
- SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)
- SpringCloud入门2:服务消费者(rest+ribbon)
- Spring Cloud(三)服务提供者 Eureka + 服务消费者(rest + Ribbon)
- 白话SpringCloud | 第四章:服务消费者(RestTemple+Ribbon+Feign)
- spring cloud快速入门教程(五)进程间调用和微服务负载均衡(RestTemplate+Ribbon)
- (1-2)SpringCloud:服务的消费者rest+ribbon
- SpringCloud教程| 第二篇:服务消费者(rest+ribbon)
- SpringCloud: 服务消费者(rest+ribbon)
- SpringCloud | 服务消费者 ribbon (四)
- Spring Cloud学习:02服务消费者(Ribbon&Feign)
- spring boot / cloud (八) 使用RestTemplate来构建远程调用服务
- 玩转SpringCloud(F版本) 三.断路器(Hystrix)RestTemplate+Ribbon和Feign两种方式
- springcloud实战之5 服务消费者(ribbon)