玩轉SpringCloud 二.服務消費者(1)ribbon+restTemplate
上一篇部落格有人問我,Springcloud系列會不會連載 ,大家可以看到我的標籤分類裡已經開設了SpringCloud專題,所以當然會連載啦,本人最近也是買了本書在學習SpringCloud微服務框架,知識會隨時分享的!!!!!!!!!!!!!!!!!!!!!
二. 服務消費者
在微服務架構中,業務都會被拆分成一個獨立的服務,服務與服務的通訊是基於 http restful的。Spring cloud有兩種服務呼叫方式,一種是ribbon+restTemplate,另一種是feign
本片部落格以上一篇部落格 ofollow,noindex">玩轉SpringCloud 一.服務的註冊與發現(Eureka) 的專案為基礎 https://www.cnblogs.com/lsy131479/p/9613755.html
本片部落格將講解 ribbon+restTemplate 模式,下一篇講解 feign 模式
1. ribbon+restTemplate
ribbon是一個負載均衡客戶端,可以很好的控制htt和tcp的一些行為 。
啟動 demo1 工程;啟動 demo2 工程,它的埠為 8762;將demo2 的配置檔案的埠改為 8763,並啟動,會發現:demo2 在 demo1 註冊了 2個例項 ,這就相當於一個 小的叢集 。
啟動之前先將 demo2 的啟動設定單例關掉
專案啟動後並且關掉單例啟動後,改變demo2的埠號
再次啟動demo2,檢視註冊中心的服務http://localhost:8761
會發現:demo2 在demo1 註冊了2個例項,這就相當於一個小的叢集。
建一個服務消費者
重新新建一個 spring-boot工程,取名為:demo3;
引入主專案,以及相關 jar 包:
<parent> <groupId>com.fsdm</groupId> <artifactId>SpringCloud_test1</artifactId> <version>1.0-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> </dependencies>
yml配置:
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ server: port: 8764 spring: application: #工程名稱 name: service-ribbon
在工程的啟動類中 ,通過@EnableDiscoveryClient向服務中心註冊;並且向程式的ioc注入一個bean: restTemplate;並通過@LoadBalanced註解表明這個restRemplate開啟負載均衡的功能。
@EnableEurekaClient @EnableDiscoveryClient @SpringBootApplication public class Demo3Application { public static void main(String[] args) { SpringApplication.run(Demo3Application.class, args); } @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }
註解解析:
@EnableDiscoveryClient
1. 基於 spring-cloud-commons ,並且在 classpath 中實現。
2. 就是如果選用的註冊中心是 eureka 推薦 @EnableEurekaClient ,如果是其他的註冊中心推薦使用 @EnableDiscoveryClient ,如果 classpath 中添加了 eureka ,則它們的作用是一樣的。
@Bean
1、Java 面向物件,物件有方法和屬性,那麼就需要物件例項來呼叫方法和屬性(即例項化);
2、 凡是有方法或屬性的類都需要例項化,這樣才能具象化去使用這些方法和屬性;
3、 規律: 凡是子類及帶有方法或屬性的類都要加上註冊 Bean 到 Spring IoC 的註解 ;
4、 把 Bean 理解為類的代理或代言人(實際上確實是通過反射、代理來實現的),這樣它就能代表類擁有該擁有的東西了
5、 我們都在微博上 @ 過某某,對方會優先看到這條資訊,並給你反饋,那麼在 Spring 中, 你標識一個 @ 符號,那麼 Spring 就會來看看,並且從這裡拿到一個 Bean 或者給出一個 Bean
@LoadBalanced
1. 表明這個 restRemplate開啟負載均衡的功能。
2. 實現負載均衡
寫一個測試類 HelloService,通過之前注入ioc容器的restTemplate來消費service-hi服務的“/hi”介面,在這裡我們直接用的程式名替代了具體的url地址,在ribbon中它會根據服務名來選擇具體的服務例項,根據服務例項在請求的時候會用具體的url替換掉服務名.
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 的方法
@RestController public class HelloControler { @Autowired HelloService helloService; @GetMapping(value = "/hi") public String hi(@RequestParam String name) { return helloService.hiService( name ); } }
啟動 demo3工程
執行工程情況:
(demo2雙啟動)
在瀏覽器上多次訪問 http://localhost:8764/hi?name=forezp ,瀏覽器 交替執行顯示 :
這說明當我們通過呼叫 restTemplate.getForObject(“ http://SERVICE-HI/hi?name= “+name,String.class) 方法時,已經做了負載均衡,訪問了不同的埠的服務例項。
此時的架構: ( 網摘 )
· 一個服務註冊中心, eureka server,埠為8761
·service-hi工程跑了兩個例項,埠分別為8762,8763,分別向服務註冊中心註冊
· sercvice-ribbon埠為8764,向服務註冊中心註冊
· 當 sercvice-ribbon通過restTemplate呼叫service-hi的hi介面時,因為用ribbon進行了負載均衡,會輪流的呼叫service-hi:8762和8763 兩個埠的hi介面;