超詳細(xì)講解SpringCloud?Commons公共抽象的用法
本期主角——Spring Cloud Commons:公共抽象
Spring Cloud Commons公共抽象
Spring Cloud將服務(wù)發(fā)現(xiàn)、負(fù)載均衡和斷路器等通用模型封裝在一個公共抽象中,可以被所有的Spring Cloud客戶端使用,不依賴于具體的實現(xiàn)(例如服務(wù)發(fā)現(xiàn)就有Eureka和Consul等不同的實現(xiàn)),這些公共抽象位于Spring Cloud Commons項目中。
@EnableDiscoveryClient
Commons提供@EnableDiscoveryClient注釋。這通過META-INF/spring.factories查找DiscoveryClient接口的實現(xiàn)。Discovery Client的實現(xiàn)將在org.springframework.cloud.client.discovery.EnableDiscoveryClient鍵下的spring.factories中添加一個配置類。DiscoveryClient實現(xiàn)的示例是Spring Cloud Netflix Eureka,Spring Cloud Consul發(fā)現(xiàn)和Spring Cloud Zookeeper發(fā)現(xiàn)。
默認(rèn)情況下,DiscoveryClient的實現(xiàn)將使用遠(yuǎn)程發(fā)現(xiàn)服務(wù)器自動注冊本地Spring Boot服務(wù)器??梢酝ㄟ^在@EnableDiscoveryClient中設(shè)置autoRegister=false來禁用此功能。
服務(wù)注冊ServiceRegistry
Commons現(xiàn)在提供了一個ServiceRegistry接口,它提供了諸如register(Registration)和deregister(Registration)之類的方法,允許您提供定制的注冊服務(wù)。Registration是一個標(biāo)記界面。
@Configuration
@EnableDiscoveryClient(autoRegister=false)
public class MyConfiguration {
private ServiceRegistry registry;
public MyConfiguration(ServiceRegistry registry) {
this.registry = registry;
}
// called via some external process, such as an event or a custom actuator endpoint
public void register() {
Registration registration = constructRegistration();
this.registry.register(registration);
}
}
每個ServiceRegistry實現(xiàn)都有自己的Registry實現(xiàn)。
RestTemplate的負(fù)載均衡
創(chuàng)建RestTemplate實例的時候,使用@LoadBalanced注解可以將RestTemplate自動配置為使用負(fù)載均衡的狀態(tài)。@LoadBalanced將使用Ribbon為RestTemplate執(zhí)行負(fù)載均衡策略。
創(chuàng)建負(fù)載均衡的RestTemplate不再能通過自動配置來創(chuàng)建,必須通過配置類創(chuàng)建,具體實例如下所示:
@Configuration
public class MyConfiguration {
@LoadBalanced
@Bean
RestTemplate restTemplate(){
return new RestTemplate():
}
}
public class MyApplication {
@Autowired
private RestTemplate restTemplate ;
public string getMyApplicationName() {
//使用restTemplate訪問my-application微服務(wù)的/name接口
string name = restTemplate.getFor0bject("http://my-application/name",string.class) ;
return name;
}
}URI需要使用服務(wù)名來指定需要訪問應(yīng)用服務(wù),Ribbon客戶端將通過服務(wù)名從服務(wù)發(fā)現(xiàn)應(yīng)用處獲取具體的服務(wù)地址來創(chuàng)建一個完整的網(wǎng)絡(luò)地址,以實現(xiàn)網(wǎng)絡(luò)調(diào)用。
RestTemplate的失敗重試
負(fù)載均衡的RestTemplate可以添加失敗重試機制。默認(rèn)情況下,失敗重試機制是關(guān)閉的,啟用方式是將Spring Retry添加到應(yīng)用程序的類路徑中。還可以設(shè)置
spring.cloud.loadbalancer.retry.enabled=false禁止類路徑中Spring retry的重試邏輯。
如果想要添加一個或者多個RetryListener到重試請求中,可以創(chuàng)建一個類型為LoadBalancedRetryListenerFactory的Bean,用來返回將要用于重試機制的RetryListener的列表,如下代碼所示:
@Configuration
public class RryListenerConfiguration {
@Bean
LoadBalancedRetryListenerFactory retryListenerFactory( {
return new LoadBalancedRetryListenerFactoryO {
@override
public RetryListener[] createRetryListeners (String service)
return new RetryListener[] {new RetryListener ( {
@Override
//重試開始前的工作
public<T,E extends Throwable> boolean open(RetryContext context,RetryCallback<T,E>callback){
return true;
}
//重試結(jié)束后的工作@Override
public<T, E extends Throwable> void close(RetryContext context,RetryCallback<T,E>callback,Throwable throwable){
}
//重試出錯后的工作@Override
publicT,E extends Throwable> void onError(RetryContext context,RetryCal1back<T,E>callback,Throwable throwable){
}
}};
}};
}}其中,自定義配置類中定義了生成LoadBalancedRetryListenerFactory實例的@Bean方法,該工廠類的createRetryListeners方法會生成一個RetryListener實例,用于進(jìn)行網(wǎng)絡(luò)請求的重試。
到此這篇關(guān)于超詳細(xì)講解SpringCloud Commons 公共抽象的用法的文章就介紹到這了,更多相關(guān)SpringCloud 公共抽象內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實現(xiàn)解壓zip壓縮包的兩種方法(支持多層級)
壓縮文件在生活中經(jīng)常能用到,在Java中提供了壓縮和解壓縮文件的功能,本文主要介紹了Java實現(xiàn)解壓zip壓縮包的兩種方法(支持多層級),感興趣的可以了解一下2024-03-03
java+sqlserver實現(xiàn)學(xué)生信息管理系統(tǒng)
這篇文章主要介紹了利用java和sqlserver實現(xiàn)學(xué)生信息管理系統(tǒng),違章內(nèi)容主要建立了與sqlserver數(shù)據(jù)庫的連接開始展開內(nèi)容,能學(xué)到了解JDBC執(zhí)行SQL的語法,需要的朋友可以參考一下2021-12-12
如何利用反射生成?MyBatisPlus中QueryWrapper動態(tài)條件
這篇文章主要介紹了如何利用反射生成?MyBatisPlus中QueryWrapper動態(tài)條件,分享在MyBatisPlus中經(jīng)常會用到代碼來構(gòu)造查詢條件等內(nèi)容,需要的小伙伴可以參考一下2022-02-02

