超詳細(xì)講解SpringCloud?Commons公共抽象的用法
本期主角——Spring Cloud Commons:公共抽象
Spring Cloud Commons公共抽象
Spring Cloud將服務(wù)發(fā)現(xiàn)、負(fù)載均衡和斷路器等通用模型封裝在一個公共抽象中,可以被所有的Spring Cloud客戶端使用,不依賴于具體的實(shí)現(xiàn)(例如服務(wù)發(fā)現(xiàn)就有Eureka和Consul等不同的實(shí)現(xiàn)),這些公共抽象位于Spring Cloud Commons項目中。
@EnableDiscoveryClient
Commons提供@EnableDiscoveryClient注釋。這通過META-INF/spring.factories查找DiscoveryClient接口的實(shí)現(xiàn)。Discovery Client的實(shí)現(xiàn)將在org.springframework.cloud.client.discovery.EnableDiscoveryClient鍵下的spring.factories中添加一個配置類。DiscoveryClient實(shí)現(xiàn)的示例是Spring Cloud Netflix Eureka,Spring Cloud Consul發(fā)現(xiàn)和Spring Cloud Zookeeper發(fā)現(xiàn)。
默認(rèn)情況下,DiscoveryClient的實(shí)現(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實(shí)現(xiàn)都有自己的Registry實(shí)現(xiàn)。
RestTemplate的負(fù)載均衡
創(chuàng)建RestTemplate實(shí)例的時候,使用@LoadBalanced注解可以將RestTemplate自動配置為使用負(fù)載均衡的狀態(tài)。@LoadBalanced將使用Ribbon為RestTemplate執(zhí)行負(fù)載均衡策略。
創(chuàng)建負(fù)載均衡的RestTemplate不再能通過自動配置來創(chuàng)建,必須通過配置類創(chuàng)建,具體實(shí)例如下所示:
@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ò)地址,以實(shí)現(xiàn)網(wǎng)絡(luò)調(diào)用。
RestTemplate的失敗重試
負(fù)載均衡的RestTemplate可以添加失敗重試機(jī)制。默認(rèn)情況下,失敗重試機(jī)制是關(guān)閉的,啟用方式是將Spring Retry添加到應(yīng)用程序的類路徑中。還可以設(shè)置
spring.cloud.loadbalancer.retry.enabled=false禁止類路徑中Spring retry的重試邏輯。
如果想要添加一個或者多個RetryListener到重試請求中,可以創(chuàng)建一個類型為LoadBalancedRetryListenerFactory的Bean,用來返回將要用于重試機(jī)制的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實(shí)例的@Bean方法,該工廠類的createRetryListeners方法會生成一個RetryListener實(shí)例,用于進(jìn)行網(wǎng)絡(luò)請求的重試。
到此這篇關(guān)于超詳細(xì)講解SpringCloud Commons 公共抽象的用法的文章就介紹到這了,更多相關(guān)SpringCloud 公共抽象內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java獲取當(dāng)?shù)氐娜粘鋈章鋾r間代碼分享
這篇文章主要介紹了Java獲取當(dāng)?shù)氐娜粘鋈章鋾r間代碼分享,國外猿友寫的一個類,需要的朋友可以參考下2014-06-06VSCode新手教程之配置Java環(huán)境的詳細(xì)教程
這篇文章主要給大家介紹了關(guān)于VSCode新手教程之配置Java環(huán)境的詳細(xì)教程,工欲善其事必先利其器,想要工作順利我們先搭建好JAVA的開發(fā)環(huán)境,需要的朋友可以參考下2023-10-10SpringBoot加入Guava Cache實(shí)現(xiàn)本地緩存代碼實(shí)例
這篇文章主要介紹了SpringBoot加入Guava Cache實(shí)現(xiàn)本地緩存代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09Java替換中使用正則表達(dá)式實(shí)現(xiàn)中間模糊匹配的方法
今天小編就為大家分享一篇Java替換中使用正則表達(dá)式實(shí)現(xiàn)中間模糊匹配的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07SpringBoot上傳文件并配置本地資源映射來訪問文件的實(shí)例代碼
這篇文章主要介紹了SpringBoot上傳文件并配置本地資源映射來訪問文件的實(shí)例代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04