温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring boot2X Consul使用Feign实现服务调用的方法

发布时间:2021-02-02 10:45:29 来源:亿速云 阅读:312 作者:小新 栏目:编程语言

这篇文章将为大家详细讲解有关Spring boot2X Consul使用Feign实现服务调用的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

服务调用有两种方式:

  A.使用RestTemplate 进行服务调用

  B.使用Feign 进行声明式服务调用

上一次写了使用RestTemplate的方式,这次使用Feign的方式实现

服务注册发现中心使用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=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是要调用的服务名

说明:

  添加跟调用目标方法一样的方法声明,必须跟目标方法的定义一致

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; @RestController public class FeignController {   @Autowired   private ProviderService providerService;   @RequestMapping("/call")   public String call() {     return providerService.hello();   } }

启动customer

访问http://localhost:8015/call

交替返回结果

hello,provider 或 hello,another provider

关于“Spring boot2X Consul使用Feign实现服务调用的方法”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI