您好,登錄后才能下訂單哦!
Springboot2XConsul中怎么利用Feign調用服務,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
服務調用有兩種方式:
A.使用RestTemplate 進行服務調用
B.使用Feign 進行聲明式服務調用
上一次寫了使用RestTemplate的方式,這次使用Feign的方式實現(xiàn)
服務注冊發(fā)現(xiàn)中心使用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=8010spring.application.name=providerspring.cloud.consul.host=localhostspring.cloud.consul.port=8500spring.cloud.consul.discovery.health-check-path=/actuator/healthspring.cloud.consul.discovery.service-name=service-providerspring.cloud.consul.discovery.heartbeat.enabled=truemanagement.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;@RestControllerpublic 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;@RestControllerpublic 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=8015spring.application.name=xyz-comsumerspring.cloud.consul.host=localhostspring.cloud.consul.port=8500spring.cloud.consul.discovery.register=falsespring.cloud.consul.discovery.health-check-url=/actuator/healthspring.cloud.consul.discovery.heartbeat.enabled=truemanagement.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@SpringBootApplicationpublic 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;@RestControllerpublic class FeignController { @Autowired private ProviderService providerService; @RequestMapping("/call") public String call() { return providerService.hello(); }}
啟動customer
訪問http://localhost:8015/call
交替返回結果
hello,provider 或 hello,another provider
關于Springboot2XConsul中怎么利用Feign調用服務問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業(yè)資訊頻道了解更多相關知識。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。