Spring boot2X Consul如何使用Feign實(shí)現(xiàn)服務(wù)調(diào)用
這篇文章主要介紹了spring boot2X Consul如何使用Feign實(shí)現(xiàn)服務(wù)調(diào)用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
服務(wù)調(diào)用有兩種方式:
A.使用RestTemplate 進(jìn)行服務(wù)調(diào)用
B.使用Feign 進(jìn)行聲明式服務(wù)調(diào)用
上一次寫了使用RestTemplate的方式,這次使用Feign的方式實(shí)現(xiàn)
服務(wù)注冊(cè)發(fā)現(xiàn)中心使用Consul
啟動(dòng)Consul
consul agent -dev
spring boot 版本 2.2.1.RELEASE
1.服務(wù)端
provider
(1)添加依賴
<properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR3</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
(2)修改配置
server.port=8010 spring.application.name=provider spring.cloud.consul.host=localhost spring.cloud.consul.port=8500 spring.cloud.consul.discovery.health-check-path=/actuator/health spring.cloud.consul.discovery.service-name=service-provider spring.cloud.consul.discovery.heartbeat.enabled=true management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always
(3)測(cè)試方法
package com.xyz.provider.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class demoController { @RequestMapping("/hello") public String Hello(){ return "hello,provider"; } }
provider1
修改端口為8011
修改測(cè)試方法
package com.xyz.provider1.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class demoController { @RequestMapping("/hello") public String Hello(){ return "hello,another provider"; } }
啟動(dòng)provider和provider1
2.客戶端
customer
(1)添加依賴
<properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR4</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
(2)配置
server.port=8015 spring.application.name=xyz-comsumer spring.cloud.consul.host=localhost spring.cloud.consul.port=8500 spring.cloud.consul.discovery.register=false spring.cloud.consul.discovery.health-check-url=/actuator/health spring.cloud.consul.discovery.heartbeat.enabled=true management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always
(3)修改啟動(dòng)類
添加注解 @EnableFeignClients,開啟掃描Spring Cloud Feign客戶端的功能
package com.xyz.comsumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @EnableFeignClients @SpringBootApplication public class ComsumerApplication { public static void main(String[] args) { SpringApplication.run(ComsumerApplication.class, args); } }
(4)添加Feign接口
添加注解@FeignClient(name = "provider")
provider是要調(diào)用的服務(wù)名
說明:
添加跟調(diào)用目標(biāo)方法一樣的方法聲明,必須跟目標(biāo)方法的定義一致
package com.xyz.consumer.controller; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; @FeignClient(name = "provider") public interface ProviderService { @RequestMapping("/hello") public String hello(); }
(4)服務(wù)調(diào)用
注入剛才聲明的ProviderService,就可以像本地方法一樣進(jìn)行調(diào)用了
package com.xyz.consumer.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class FeignController { @Autowired private ProviderService providerService; @RequestMapping("/call") public String call() { return providerService.hello(); } }
啟動(dòng)customer
訪問http://localhost:8015/call
交替返回結(jié)果
hello,provider 或 hello,another provider
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java中的ThreadPoolExecutor線程池原理細(xì)節(jié)解析
這篇文章主要介紹了Java中的ThreadPoolExecutor線程池原理細(xì)節(jié)解析,ThreadPoolExecutor是一個(gè)線程池,最多可使用7個(gè)參數(shù)來控制線程池的生成,使用線程池可以避免創(chuàng)建和銷毀線程的資源損耗,提高響應(yīng)速度,并且可以管理線程池中線程的數(shù)量和狀態(tài)等等,需要的朋友可以參考下2023-12-12Springboot集成JUnit5優(yōu)雅進(jìn)行單元測(cè)試的示例
這篇文章主要介紹了Springboot集成JUnit5優(yōu)雅進(jìn)行單元測(cè)試的示例,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2020-10-10java字符串相加時(shí)的內(nèi)存表現(xiàn)和原理分析
這篇文章主要介紹了java字符串相加時(shí)的內(nèi)存表現(xiàn)和原理分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07SpringBoot整合flyway實(shí)現(xiàn)步驟解析
這篇文章主要介紹了SpringBoot整合flyway實(shí)現(xiàn)步驟解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08基于Java Callable接口實(shí)現(xiàn)線程代碼實(shí)例
這篇文章主要介紹了基于Java Callable接口實(shí)現(xiàn)線程代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08Spring中使用AOP進(jìn)行事務(wù)管理實(shí)例
這篇文章主要介紹了Spring中使用AOP進(jìn)行事務(wù)管理實(shí)例,當(dāng)在Spring項(xiàng)目中涉及數(shù)據(jù)庫操作時(shí),事務(wù)管理是非常重要的,它可以確保數(shù)據(jù)庫操作的一致性和完整性,Spring提供了強(qiáng)大的事務(wù)管理功能,可以通過聲明式或編程式兩種方式進(jìn)行配置,需要的朋友可以參考下2023-09-09java實(shí)現(xiàn)合并2個(gè)文件中的內(nèi)容到新文件中
這篇文章主要介紹了java實(shí)現(xiàn)合并2個(gè)文件中的內(nèi)容到新文件中,思路非常不錯(cuò),這里推薦給大家。2015-03-03Java模擬HTTP Get Post請(qǐng)求 輕松實(shí)現(xiàn)校園BBS自動(dòng)回帖
這篇文章主要介紹了Java模擬HTTP Get Post請(qǐng)求,輕松實(shí)現(xiàn)校園BBS自動(dòng)回帖,感興趣的小伙伴們可以參考一下2015-12-12