您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Spring boot2X Consul使用Feign實現服務調用的方法,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
服務調用有兩種方式:
A.使用RestTemplate 進行服務調用
B.使用Feign 進行聲明式服務調用
上一次寫了使用RestTemplate的方式,這次使用Feign的方式實現
服務注冊發現中心使用Consul
啟動Consul
consul agent -dev
spring boot 版本 2.2.1.RELEASE
1.服務端
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)測試方法
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
修改測試方法
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"; } }
啟動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)修改啟動類
添加注解 @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是要調用的服務名
說明:
添加跟調用目標方法一樣的方法聲明,必須跟目標方法的定義一致
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)服務調用
注入剛才聲明的ProviderService,就可以像本地方法一樣進行調用了
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(); } }
啟動customer
訪問http://localhost:8015/call
交替返回結果
hello,provider 或 hello,another provider
關于“Spring boot2X Consul使用Feign實現服務調用的方法”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。