Spring boot2X Consul如何使用Feign實現(xiàn)服務(wù)調(diào)用
這篇文章主要介紹了spring boot2X Consul如何使用Feign實現(xiàn)服務(wù)調(diào)用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
服務(wù)調(diào)用有兩種方式:
A.使用RestTemplate 進(jìn)行服務(wù)調(diào)用
B.使用Feign 進(jìn)行聲明式服務(wù)調(diào)用
上一次寫了使用RestTemplate的方式,這次使用Feign的方式實現(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=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是要調(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;
@RestController
public 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
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java簡單實現(xiàn)復(fù)制 粘貼 剪切功能代碼分享
本文給大家分享了一段java編寫的簡單實現(xiàn)復(fù)制粘貼剪切功能的代碼,需要的小伙伴可以直接拿走使用。如有更好的方案,也可以告之本人。2014-11-11
mybatis-plus 如何判斷參數(shù)是否為空并作為查詢條件
這篇文章主要介紹了mybatis-plus 如何判斷參數(shù)是否為空并作為查詢條件,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
java IO流 之 輸出流 OutputString()的使用
這篇文章主要介紹了java IO流 之 輸出流 OutputString()的使用的相關(guān)資料,需要的朋友可以參考下2016-12-12
詳解Java編寫并運(yùn)行spark應(yīng)用程序的方法
這篇文章主要介紹了詳解Java編寫并運(yùn)行spark應(yīng)用程序的方法,內(nèi)容詳細(xì),結(jié)合了作者實際工作中的問題進(jìn)行具體分析,具有一定參考價值。2017-09-09
Java ScheduledExecutorService定時任務(wù)案例講解
這篇文章主要介紹了Java ScheduledExecutorService定時任務(wù)案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08

