Springboot2XConsul中怎么利用Feign調(diào)用服務(wù),針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。
在和田等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站制作、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作按需設(shè)計(jì)網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),成都全網(wǎng)營銷,成都外貿(mào)網(wǎng)站建設(shè),和田網(wǎng)站建設(shè)費(fèi)用合理。
服務(wù)調(diào)用有兩種方式:
A.使用RestTemplate 進(jìn)行服務(wù)調(diào)用
B.使用Feign 進(jìn)行聲明式服務(wù)調(diào)用
上一次寫了使用RestTemplate的方式,這次使用Feign的方式實(shí)現(xiàn)
服務(wù)注冊發(fā)現(xiàn)中心使用Consul
啟動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=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是要調(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;@RestControllerpublic class FeignController { @Autowired private ProviderService providerService; @RequestMapping("/call") public String call() { return providerService.hello(); }}
啟動customer
訪問http://localhost:8015/call
交替返回結(jié)果
hello,provider 或 hello,another provider
關(guān)于Springboot2XConsul中怎么利用Feign調(diào)用服務(wù)問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。
本文名稱:Springboot2XConsul中怎么利用Feign調(diào)用服務(wù)
文章起源:http://chinadenli.net/article2/gdodic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、定制網(wǎng)站、、商城網(wǎng)站、自適應(yīng)網(wǎng)站、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)