Spring Cloud Consul的服務注冊與發現
以Windows為例,下載解壓后,以開發模式運行:
consul agent --dev
啟動成功后,可以訪問Consul提供的管理頁面,默認端口為8500,頁面上顯示了已注冊服務的列表,包括它們的運行狀況等信息。
<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: 85003.運行消費者和提供者服務,Consul管理頁面將顯示對應的服務信息:
使用RestTemplate調用服務
@AutowiredRestTemplate restTemplate;public String getFirstProduct() { return this.restTemplate.getForObject('https://服務名/products/1', String.class);}
要使用RestTemplate別忘了加配置:
@Bean@LoadBalancedpublic RestTemplate restTemplate(){return new RestTemplate();}
以上就是Spring Cloud Consul的服務注冊與發現的詳細內容,更多關于Spring Cloud Consul 服務注冊與發現的資料請關注好吧啦網其它相關文章!
相關文章: