溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Spring boot2X Consul使用Feign實(shí)現(xiàn)服務(wù)調(diào)用的方法

發(fā)布時(shí)間:2021-02-02 10:45:29 來(lái)源:億速云 閱讀:274 作者:小新 欄目:編程語(yǔ)言

這篇文章將為大家詳細(xì)講解有關(guān)Spring boot2X Consul使用Feign實(shí)現(xiàn)服務(wù)調(diào)用的方法,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

服務(wù)調(diào)用有兩種方式:

  A.使用RestTemplate 進(jìn)行服務(wù)調(diào)用

  B.使用Feign 進(jìn)行聲明式服務(wù)調(diào)用

上一次寫(xiě)了使用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)添加依賴(lài)

<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)添加依賴(lài)

<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)類(lèi)

添加注解 @EnableFeignClients,開(kāi)啟掃描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ù)名

說(shuō)明:

  添加跟調(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

訪問(wèn)http://localhost:8015/call

交替返回結(jié)果

hello,provider 或 hello,another provider

關(guān)于“Spring boot2X Consul使用Feign實(shí)現(xiàn)服務(wù)調(diào)用的方法”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI