Spring Cloud Consul的服務注冊與發(fā)現(xiàn)
運行Consul
以Windows為例,下載解壓后,以開發(fā)模式運行:
consul agent --dev

啟動成功后,可以訪問Consul提供的管理頁面,默認端口為8500,頁面上顯示了已注冊服務的列表,包括它們的運行狀況等信息。

服務注冊
1.添加Spring Cloud Consul依賴:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies>
2.在服務配置文件中添加Consul配置:
spring: cloud: consul: host: localhost port: 8500
3.運行消費者和提供者服務,Consul管理頁面將顯示對應的服務信息:

服務發(fā)現(xiàn)
使用RestTemplate調(diào)用服務
@Autowired
RestTemplate restTemplate;
public String getFirstProduct() {
return this.restTemplate.getForObject("https://服務名/products/1", String.class);
}
要使用RestTemplate別忘了加配置:
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
以上就是Spring Cloud Consul的服務注冊與發(fā)現(xiàn)的詳細內(nèi)容,更多關(guān)于Spring Cloud Consul 服務注冊與發(fā)現(xiàn)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring中的ImportBeanDefinitionRegistrar接口詳解
這篇文章主要介紹了Spring中的ImportBeanDefinitionRegistrar接口詳解,ImportBeanDefinitionRegistrar接口是也是spring的擴展點之一,它可以支持我們自己寫的代碼封裝成BeanDefinition對象,注冊到Spring容器中,功能類似于注解@Service @Component,需要的朋友可以參考下2023-09-09
SpringBoot整合第三方技術(shù)的實現(xiàn)
本文主要介紹了SpringBoot整合第三方技術(shù)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-02-02
SpringCloud Gateway網(wǎng)關(guān)功能介紹與使用
SpringCloud Gateway 是 Spring Cloud 的一個全新項目,它旨在為微服務架構(gòu)提供一種簡單有效的統(tǒng)一的 API 路由管理方式。這篇文章主要介紹了SpringCloud Gateway網(wǎng)關(guān)作用,需要的朋友可以參考下2022-12-12
Spring動態(tài)注冊多數(shù)據(jù)源的實現(xiàn)方法
這篇文章主要介紹了Spring動態(tài)注冊多數(shù)據(jù)源的實現(xiàn)方法,小編覺的挺不錯的,現(xiàn)分享到腳本之家平臺,需要的朋友可以參考下2018-01-01
SpringCloud微服務架構(gòu)實戰(zhàn)之微服務治理功能的實現(xiàn)
這篇文章主要介紹了SpringCloud微服務架構(gòu)實戰(zhàn)之微服務治理,這些治理工具主要包括服務的注冊與發(fā)現(xiàn)、負載均衡管理、動態(tài)路由、服務降級和故障轉(zhuǎn)移、鏈路跟蹤、服務監(jiān)控等,需要的朋友可以參考下2022-02-02
mybatis-plus指定字段模糊查詢的實現(xiàn)方法
最近項目中使用springboot+mybatis-plus來實現(xiàn),所以下面這篇文章主要給大家介紹了關(guān)于mybatis-plus實現(xiàn)指定字段模糊查詢的相關(guān)資料,需要的朋友可以參考下2022-04-04

