W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
Spring Cloud支持Feign(REST客戶端構(gòu)建器),還支持Spring RestTemplate
,
以使用邏輯服務(wù)名稱/標(biāo)識(shí)而不是物理URL查找服務(wù)。Feign和發(fā)現(xiàn)感知的RestTemplate都使用Ribbon進(jìn)行客戶端負(fù)載平衡。
如果要使用RestTemplate訪問服務(wù)STORES,只需聲明:
@LoadBalanced @Bean public RestTemplate loadbalancedRestTemplate() { new RestTemplate(); }
并以這種方式使用它(注意我們?nèi)绾问褂肅onsul中的STORES服務(wù)名稱/ id而不是完全限定的域名):
@Autowired RestTemplate restTemplate; public String getFirstProduct() { return this.restTemplate.getForObject("https://STORES/products/1", String.class); }
如果您在多個(gè)數(shù)據(jù)中心中有Consul個(gè)群集,并且要訪問另一個(gè)數(shù)據(jù)中心中的服務(wù),則僅靠服務(wù)名稱/ id是不夠的。在這種情況下,請(qǐng)使用屬性spring.cloud.consul.discovery.datacenters.STORES=dc-west
,其中STORES
是服務(wù)名稱/ id,而dc-west
是STORES服務(wù)所在的數(shù)據(jù)中心。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: