欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Springboot2XConsul中怎么利用Feign調(diào)用服務(wù)-創(chuàng)新互聯(lián)

Springboot2XConsul中怎么利用Feign調(diào)用服務(wù),針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。

創(chuàng)新互聯(lián)建站是一家專注于成都網(wǎng)站建設(shè)、網(wǎng)站制作與策劃設(shè)計(jì),蒼南網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專注于網(wǎng)站建設(shè)十載,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:蒼南等地區(qū)。蒼南做網(wǎng)站價(jià)格咨詢:18980820575

服務(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=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)測(cè)試方法

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

修改測(cè)試方法

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";  }}

啟動(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=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)修改啟動(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@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();  }}

啟動(dòng)customer

訪問http://localhost:8015/call

交替返回結(jié)果

hello,provider 或 hello,another provider

關(guān)于Springboot2XConsul中怎么利用Feign調(diào)用服務(wù)問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

當(dāng)前名稱:Springboot2XConsul中怎么利用Feign調(diào)用服務(wù)-創(chuàng)新互聯(lián)
網(wǎng)址分享:http://chinadenli.net/article34/dhjgse.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、全網(wǎng)營(yíng)銷推廣、網(wǎng)站設(shè)計(jì)公司企業(yè)網(wǎng)站制作、面包屑導(dǎo)航、網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營(yíng)