spring cloud的消費服務ribbon(踩著坑往前爬)
在微服務架構中,業務都會被拆分成一個獨立的服務,服務與服務的通訊是基於http restful的。Spring cloud有兩種服務調用方式,
一種是ribbon+restTemplate,另一種是feign
Ribbon,主要提供客戶側的軟件負載均衡算法。
Ribbon客戶端組件提供一系列完善的配置選項,比如連接超時、重試、重試算法等。Ribbon內置可插拔、可定制的負載均衡組件。下面是用到的一些負載均衡策略:
- 簡單輪詢負載均衡
- 加權響應時間負載均衡
- 區域感知輪詢負載均衡
- 隨機負載均衡
Ribbon中還包括以下功能:
- 易於與服務發現組件(比如Netflix的Eureka)集成
- 使用Archaius完成運行時配置
- 使用JMX暴露運維指標,使用Servo發布
- 多種可插拔的序列化選擇
將service-hi的配置文件的端口改為8762,並啟動,這時你會發現:service-hi在eureka-server註冊了2個實例,這就相當於一個小的集群。訪問localhost:8761如圖所示:
然而你會發現idea啟動不了兩次同一個main方法的類
你只需要這樣做就行了
建一個服務消費者
重新新建一個spring-boot工程,取名為:service-ribbon;
<!--引入起步依賴spring-cloud-starter-eureka、spring-cloud-starter-ribbon、spring-boot-starter-web-->
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
上面這個節點好像包括了spring-cloud-starter-eureka這個依賴,所以只要加上兩個節點
也許是命好吧
在pom.xml加上兩個節點
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
在工程的配置文件指定服務的註冊中心地址為http://localhost:8761/eureka/,程序名稱為 service-ribbon,程序端口為8764。配置文件application.yml如下:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8764
spring:
application:
name: service-ribbon
在工程的啟動類中,通過@EnableDiscoveryClient向服務中心註冊;並且向程序的ioc註入一個bean: restTemplate;並通過@LoadBalanced註解表明這個restRemplate開啟負載均衡的功能。
package cn.zhiwei; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; import org.springframework.cloud.client.loadbalancer.LoadBalanced; @SpringBootApplication @EnableDiscoveryClient public class ServiceRibbonApplication { public static void main(String[] args) { SpringApplication.run(ServiceRibbonApplication.class, args); } @Bean @LoadBalanced//一定要給這個註解,不然會識別不了路徑 RestTemplate restTemplate() { return new RestTemplate(); } }
寫一個測試類HelloService,通過之前註入ioc容器的restTemplate來消費service-hi服務的“/hi”接口,在這裏我們直接用的程序名替代了具體的url地址,在ribbon中它會根據服務名來選擇具體的服務實例,根據服務實例在請求的時候會用具體的url替換掉服務名,代碼如下:
package cn.zhiwei.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; /** * Created by Administrator on 2018/4/6. */ @Service//加上這個表示被spring容器管理了 public class HelloService { @Autowired RestTemplate restTemplate; public String hiService(String name) { return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class); } }
寫一個controller,在controller中用調用HelloService 的方法,代碼如下:
package cn.zhiwei.controller; import cn.zhiwei.service.HelloService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * Created by Administrator on 2018/4/6. */ @RestController public class HelloControler { @Autowired HelloService helloService; @RequestMapping(value = "/hi") public String hi(@RequestParam String name){ return helloService.hiService(name); } }
在瀏覽器上多次訪問http://localhost:8764/hi?name=威哥,瀏覽器交替顯示:
hi 威哥,i am from port:8762
hi 威哥,i am from port:8763
項目結構圖
spring cloud的消費服務ribbon(踩著坑往前爬)