springcloud微服務(wù)架構(gòu)開發(fā)實(shí)戰(zhàn):常見微服務(wù)的消費(fèi)者
常見微服務(wù)的消費(fèi)者
本節(jié)就常見的微服務(wù)的消費(fèi)者進(jìn)行介紹。在Java領(lǐng)域比較常用的消費(fèi)者框架主要有HttpClient、Ribbon、Feign 等。

Apache HttpClient
Apache HttpClient是Apache Jakarta Common下的子項(xiàng)目,用來提供高效的、最新的、功能豐富的支持HTTP的客戶端編程工具包,并且它支持HTTP最新的版本和建議。雖然在JDK的java.net包中已經(jīng)提供了訪問HTTP的基本功能,但是對于大部分應(yīng)用程序來說,JDK 庫本身提供的功能還不夠豐富和靈活。HttpClient 相比傳統(tǒng)JDK自帶的URLConnection,增加了易用性和靈活性,它不僅使客戶端發(fā)送Http請求變得容易,而且也方便了開發(fā)人員測試基于HTTP的接口,既提高了開發(fā)的效率,也方便提高代碼的健壯性。
在之前章節(jié)的示例中,我們也大規(guī)模采用了HttpClient來作為REST客戶端。
在程序中,我們經(jīng)常使用RestTemplate實(shí)例來訪問REST服務(wù)。RestTemplate 是Spring的核心類,用于同步客戶端的HTTP訪問。它簡化了與HTTP服務(wù)器的通信,并強(qiáng)制執(zhí)行RESTful原則。
默認(rèn)情況下,RestTemplate 依賴于標(biāo)準(zhǔn)的JDK功能來建立HTTP連接,當(dāng)然,我們也可以通過setRequestFactory屬性來切換到使用不同的HTTP庫,例如,上面我們所介紹的Apache HttpCli-ent, 以及其他的,如Netty、OkHtp等。
要使用Apache HttpClient,最方便的方式莫過于再添加Apache HttpClient依賴。
//依賴關(guān)系
dependencies {
//添加Apache HttpClient依賴
compile ('org . apache . httpcomponents :httpclient:4.5.3')
}其次,通過RestTemplateBuilder來創(chuàng)建RestTemplate實(shí)例。
import org. spr ingframework .beans. factory .annotation . Autowired;
import org. spr ingframework. boot . web. client. RestTemplateBuilder;
import org. spr ingf r amework. context. annotation. Bean;
import org. springframework . context . annotation . Configuration;
import org. springfr amework . web. client.RestTemplate;
@Configuration
public class RestConfiguration {
@Autowi red
private RestTemplateBuilder builder;
@Bean
public RestTemplate restTemplate() {
return builder .build() ;
}
}最后,就能通過RestTemplate實(shí)例來訪問RESTfulAPI服務(wù)了。
@Service
public class WeatherDataServiceImpl implements WeatherDataService {
CAutowired
private RestTemplate restTemplate;
private WeatherResponse doGetWeatherData (String uri) {
ResponseEntity response = restTemplate . getForEntity (uri,
String.class) ;
//。。
}
// .。} 
Ribbon
Spring Cloud Ribbon是基于Netlix Ribbon實(shí)現(xiàn)的一套客戶端負(fù)載均衡的工具。它是一一個基于HTTP和TCP的客戶端負(fù)載均衡器。
Ribbon的一一個中心概念就是命名客戶端。每個負(fù)載平衡器都是組合整個服務(wù)組件的一部分,它們一起協(xié)作,并可以根據(jù)需要與遠(yuǎn)程服務(wù)器進(jìn)行交互,獲取包含命名客戶端名稱的集合。SpringCloud根據(jù)需要,使用RibbonClientConfiguration為每個命名客戶端創(chuàng)建一個新的集合作為Applica-tionContext。這其中包括- - 個ILoadBalancer、一個RestClient和一個ServerListFilter。
Ribbon經(jīng)常與Eureka結(jié)合使用。在典型的分布式部署中,Eureka 為所有微服務(wù)實(shí)例提供服務(wù)注冊,而Ribbon則提供服務(wù)消費(fèi)的客戶端。Ribbon 客戶端組件提供一- 系列完善的配置選項(xiàng),如連接超時、重試、重試算法等。Ribbon內(nèi)置可插拔、可定制的負(fù)載均衡組件。下 面是用到的一一些負(fù)載均衡策略:
●簡單輪詢負(fù)載均衡;
●加權(quán)響應(yīng)時間負(fù) 載均衡;
●區(qū)域感知輪詢負(fù)載均衡;
●隨機(jī)負(fù)載均衡。
其中,區(qū)域感知負(fù)載均衡器是Ribbon 一個久經(jīng)考驗(yàn)的功能,該負(fù)載均衡器會采取如下步驟。
●負(fù)載均衡器會檢查、計(jì)算所有可用區(qū)域的狀態(tài)。如果某個區(qū)域中平均每個服務(wù)器的活躍請求已經(jīng)達(dá)到配置的閾值,該區(qū)域?qū)幕钴S服務(wù)器列表中排除。如果多于-一個區(qū)域已經(jīng)到達(dá)閾值,平均每服務(wù)器擁有最多活躍請求的區(qū)域?qū)⒈慌懦?/p>
●最差的區(qū)域被排除后,從剩下的區(qū)域中,將按照服務(wù)器實(shí)例數(shù)的概率抽樣法選擇一 個區(qū)域。
●在選定區(qū)域中,將會根據(jù)給定負(fù)載均衡策略規(guī)則返回一個服務(wù)器。
在
micro-weather-eureka-client應(yīng)用基礎(chǔ)上,我們稍作修改,使其成為一個新的應(yīng)用mi-cro-weather-eureka-client-ribbon,作為本章節(jié)的示例。
1.所需環(huán)境
為了演示本例子,需要采用如下開發(fā)環(huán)境。
JDK 8。
Gradle 4.0。
● Redis 3.2.100。
● Spring Boot 2.0.0.M3。
●Spring Cloud Starter Netlix Eureka Client Finchley.M2。
●Spring Cloud Starter Netilix Ribbon。
2.項(xiàng)目配置
要使用Ribbon,最簡單的方式莫過于添加Ribbon依賴。
//依賴關(guān)系
dependencies {
//添加Spring Cloud Starter Netflix Ribbon依賴
compile (' org. spr ingframework. cloud: spring-cloud-starter-netflix-rib-
bon')
}3.啟用Ribbon
Spring Cloud提供了聲明式@RibbonClient注解來使用Ribbon。
package com. waylau. spring. cloud. weather . config;
import org. springframework.beans. factory . annotation. Autowired;
import org. spr ingf r amework. boot. web. cl ient. RestTemplateBuilder;
import org. springfr amework. cloud.client. loadbalancer .LoadBalanced;
import org.spr ingframework .cloud. netflix. ribbon. RibbonCl ient;
import org. spr ingfr amework. context. annotation. Bean;
import org. springfr amework.context. annotation. Configuration;
import org. springframework. web. client. RestTemplate;
t REST配置類.
* @since 1.0.0 2017年11月03日
k Cauthor "https: / /waylau. com">Way Lau
@Configuration
@RibbonClient (name = "ribbon-client", configuration = RibbonConfiguration.
class)
public class RestConfiguration {
@Autowi red
private RestTemplateBuilder builder;
@Bean
@LoadBalanced
public RestTemplate restTemplate () {
return builder .build() ;
}
}其中RibbonConfiguration是Ribbon自定義的配置類,定義如下。
/ **
*/
package com. waylau. spring.cloud . weather . config;
import org. spr ingframework.cloud.netflix. ribbon. ZonePreferenceServerList
Filter;
import org. springfr amework. context. annotation. Bean;
import org. springframework. context. annotation. Configuration;
import com. netflix. loadbalancer. IPing;
import com. netflix. loadbalancer . PingUrl;
/**
城市配置.
* @since 1.0.0 2017年11月3日
@author Way Lau
@Configuration
public class RibbonConfiguration {
@Bean
public ZonePreferenceServerListFilter serverListFilter() {
ZonePreferenceServerListFilter filter = new ZonePreferenceServer-
ListFilter ()
filter .setZone ("myZone") ;
return filter;
@Bean
public IPing ribbonPing() {
return new PingUrl () ;
}
}這樣,我們就能通過應(yīng)用名稱msa-weather-city -eureka來訪問微服務(wù)了,并且還實(shí)現(xiàn)了服務(wù)的負(fù)載均衡。
4.使用Ribbon
編寫CityController,用于使用Ribbon配置的RestTemplate。
import org. springframework. beans. factory . annotation. Autowired;
import org . springf ramework. web. bind. annotation. GetMapping;
import org.spr ingframework . web. bind. annotation. RestController;
import org. springf ramework. web. cl ient . RestTemplate;
/**
City Controller .
* @since 1.0.0 2017年11月03日
@author Way Lau
@RestController
public class CityController
@Autowired
private RestTemplate res tTemplate;
@GetMapping ("/cities")
public String listCity() {
//通過應(yīng)用名稱來查找
String body = restTemplate . getForEntity ("http:/ /msa-weather-
city-eureka/cities", String.class) .getBody() ;
return body;
}
}5.應(yīng)用配置
該應(yīng)用同時也是一個 Eureka Client。修改application.properties,將其修改為如下配置。
spring. application. name: micro-weather -eureka-client-ribbon eureka. client. serviceUrl .defaultZone: http://localhost:8761/eureka/
Feign
Feign是一- 個聲明式的Web服務(wù)客戶端,這使Web服務(wù)客戶端的寫人更加方便。它具有可插拔注解支持,包括Feign注解和JAX-RS注解。Feign 還支持可插拔編碼器和解碼器。Spring Cloud增加了對Spring MVC注解的支持,并且使用了在Spring Web中默認(rèn)使用的相同的HttpMessageCon-verter。在使用Feign時,Spring Cloud集成了Ribbon 和Eureka 來提供負(fù)載平衡的HTTP客戶端。
在
micro-weather-eureka-client應(yīng)用基礎(chǔ)上,我們稍作修改,使其成為一個新的應(yīng)用mi-cro-weather- eureka-client-feign,作為本節(jié)的示例。
1.所需環(huán)境
為了演示本例子,需要采用如下開發(fā)環(huán)境。
●Gradle 4.0。
●Redis 3.2.100。
●Spring Boot 2.0.0.M3。
●Spring Cloud Starter Netflix Eureka Client Finchley.M2。
●Spring Cloud Starter OpenFeign Finchley.M2。
2.項(xiàng)目配置
為了使用Feign,增加如下配置。
dependencies {
//添加Spring Cloud Starter OpenFeign依賴
compile('org. spr ingframework. cloud:spring-cloud-starter-openfeign')
}3.啟用Feign
要啟用Feign,最簡單的方式就是在應(yīng)用的根目錄的Application 類上添加
org.springframework.cloud.netlix. feign.EnableFeignClients注解。
import org.springframework .boot. SpringApplication;
import org. springfr amework . boot. autoconfigure . SpringBootApplication;
import org. springframework. cloud. client . discovery . EnableDi scoveryClient;
import org.springframework. cloud. netflix. feign. EnableFeignClients;
/**
★主應(yīng)用程序.
★asince 1.0.0 2017年11月04日
* Cauthor Way Lau
*/
@SpringBootApplication
@EnableDiscoveryCl ient
@EnableFeignClients
public class Appl ication {
public static void main(String[] args) {
SpringApplication. run (Application.class, args) ;
}
}4.使用Feign
要使用Feign,首先是編寫Feign請求接口。
package com. waylau. spring.cloud. weather .service;
import org. spr ingf ramework. cloud . netflix. feign. FeignClient;
import org. springfr amework. web . bind. annotation . GetMapping;
★訪問城市信息的客戶端.
* Gsince 1.0.0 2017年11月4日
* @author "https:/ /waylau. com">Way Lau
*/
@FeignClient ("msa-weather-city-eureka")
public interface CityClient {
@GetMapping("/cities")
String listCity() ;
}
}其中,@FeignClient指定了要訪問的服務(wù)的名稱msa- weather-city-eureka。
在聲明了CityClient 接口之后,我們就能在控制器CityController中使用該接口的實(shí)現(xiàn)。
import org. springframework .beans. factory .annotation. Autowired;
import org. springframework. web .bind. annotation. GetMapping;
import org. springf r amework. web.bind. annotation. RestController;
import com. waylau. spring. cloud . weather . service. CityClient;
/★*
* City Controller.
★@since 1.0.0 2017年11月04日
* @author "https:/ /waylau. com">Way Lau
@RestController
public class CityController {
@Autowired
private CityClient cityClient;
@GetMapping("/cities")
public String listCity() {
/通過Feign客戶端來查找
String body = cityClient. listCity() ;
return body;
}
}CityContoller 控制器專門用于請求獲取城市信息的響應(yīng)。這里,我們直接注入CityClient 接口即可,F(xiàn)eign框架會為我們提供具體的實(shí)現(xiàn)。
最后,修改application.properties。將其修改為如下配置。
spr ing . application. name: micro-weather -eureka-client-feign
eureka. client. serviceUrl. defaultZone: http:/ /localhost: 8761/eureka/
feign.cl ient. config. feignName . connectTimeout: 5000
feign. cl ient. config. feignName . readTimeout: 5000其中:
●
feign.client.config.feignName.connectTimeout為連接超時時間;
feign.client.conig. feignName.readTimeout為讀數(shù)據(jù)的超時時間。
源碼
本節(jié)示例所涉及的源碼,見
micro-weather-eureka-server、micro-weather-eureka-client 和msa-weather-city-eureka,以及micro-weather-eureka-client-ribbon和micro-weather- eureka-client-feign。
本篇文章內(nèi)容給大家講解的是常見微服務(wù)的消費(fèi)者
下篇文章給大家講解使用Feign實(shí)現(xiàn)服務(wù)的消費(fèi)者;
覺得文章不錯的朋友可以轉(zhuǎn)發(fā)此文關(guān)注小編;
感謝大家的支持!
本文就是愿天堂沒有BUG給大家分享的內(nèi)容,大家有收獲的話可以分享下,想學(xué)習(xí)更多的話可以到微信公眾號里找我,我等你哦。

