当前位置:   article > 正文

springboot整合feign实现RPC调用,并通过Hystrix实现服务降级_springboot rpc

springboot rpc

feign/openfeign和dubbo是常用的微服务RPC框架,由于feigin内部已经集成ribbon,自带了负载均衡的功能,当有多个同名的服务注册到注册中心时,会根据ribbon默认的负载均衡算法将请求分配到不同的服务。这篇文章就简单介绍一下怎么使用feign来调用远程的服务。

首先,需要有一个微服务注册中心来提供服务注册与发现,本章就使用之前创建的eureka作为注册中心。点击以下文章链接,教你快速搭建一个eureka server

springboot整合eureka、config搭建注册中心和配置中心icon-default.png?t=N7T8https://blog.csdn.net/heyl163_/article/details/131715281

目录

一、服务提供者

二、服务消费者

三、测试效果

四、开启Hystrix实现服务降级


首先,要实现服务间的调用,需要有服务提供者和服务消费者,创建两个项目,分别用于服务提供者和服务消费者。

一、服务提供者

创建一个springboot项目,取名为provider

1、修改maven配置文件pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.3.4.RELEASE</version>
  9. <relativePath/>
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>provider</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <properties>
  15. <java.version>1.8</java.version>
  16. <eureka.version>1.4.4.RELEASE</eureka.version>
  17. </properties>
  18. <dependencyManagement>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.cloud</groupId>
  22. <artifactId>spring-cloud-dependencies</artifactId>
  23. <version>Hoxton.SR12</version>
  24. <type>pom</type>
  25. <scope>import</scope>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.cloud</groupId>
  29. <artifactId>spring-cloud-starter-eureka</artifactId>
  30. <version>${eureka.version}</version>
  31. </dependency>
  32. </dependencies>
  33. </dependencyManagement>
  34. <dependencies>
  35. <dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-starter-web</artifactId>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.springframework.boot</groupId>
  41. <artifactId>spring-boot-starter-test</artifactId>
  42. <scope>test</scope>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.springframework.cloud</groupId>
  46. <artifactId>spring-cloud-starter-eureka</artifactId>
  47. </dependency>
  48. </dependencies>
  49. <build>
  50. <plugins>
  51. <plugin>
  52. <groupId>org.springframework.boot</groupId>
  53. <artifactId>spring-boot-maven-plugin</artifactId>
  54. </plugin>
  55. </plugins>
  56. </build>
  57. </project>

2、修改系统配置文件

  1. server:
  2. port: 8085
  3. spring:
  4. application:
  5. name: provider
  6. eureka:
  7. instance:
  8. hostname: localhost
  9. client:
  10. service-url:
  11. defaultZone: http://${eureka.instance.hostname}:8761/eureka

3、创建一个controller

在根目录下创建controller包,然后在controller包下创建一个UserController

  1. package com.example.provider.controller;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RequestMethod;
  4. import org.springframework.web.bind.annotation.RestController;
  5. /**
  6. * @author heyunlin
  7. * @version 1.0
  8. */
  9. @RestController
  10. @RequestMapping(path = "/user", produces = "application/json;charset=utf-8")
  11. public class UserController {
  12. @RequestMapping(value = "/name", method = RequestMethod.GET)
  13. public String name() {
  14. return "heyunlin";
  15. }
  16. }

4、启动类上添加@EnableDiscoveryClient注解

  1. package com.example.provider;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. @EnableDiscoveryClient
  6. @SpringBootApplication
  7. public class ProviderApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(ProviderApplication.class, args);
  10. }
  11. }

二、服务消费者

1、创建一个springboot项目并命名为consumer

2、修改pom.xml配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.3.4.RELEASE</version>
  9. <relativePath/>
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>consumer</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <properties>
  15. <java.version>1.8</java.version>
  16. <eureka.version>1.4.4.RELEASE</eureka.version>
  17. </properties>
  18. <dependencyManagement>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.cloud</groupId>
  22. <artifactId>spring-cloud-dependencies</artifactId>
  23. <version>Hoxton.SR12</version>
  24. <type>pom</type>
  25. <scope>import</scope>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.cloud</groupId>
  29. <artifactId>spring-cloud-starter-eureka</artifactId>
  30. <version>${eureka.version}</version>
  31. </dependency>
  32. </dependencies>
  33. </dependencyManagement>
  34. <dependencies>
  35. <dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-starter-web</artifactId>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.springframework.boot</groupId>
  41. <artifactId>spring-boot-starter-test</artifactId>
  42. <scope>test</scope>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.springframework.cloud</groupId>
  46. <artifactId>spring-cloud-starter-eureka</artifactId>
  47. </dependency>
  48. <dependency>
  49. <groupId>org.springframework.cloud</groupId>
  50. <artifactId>spring-cloud-starter-openfeign</artifactId>
  51. </dependency>
  52. </dependencies>
  53. </project>

3、修改配置文件,注册到eureka

  1. server:
  2. port: 8086
  3. spring:
  4. application:
  5. name: consumer
  6. eureka:
  7. instance:
  8. hostname: localhost
  9. client:
  10. service-url:
  11. defaultZone: http://${eureka.instance.hostname}:8761/eureka

4、通过feign调用远程的方法

根目录下创建feign包,在feign包下创建一个接口FeignService(类名不重要)

@FeignClient("provider")指定注册到eurka的服务名

@RequestMapping的路径写provider服务的控制器接口路径

  1. package com.example.consumer.feign;
  2. import org.springframework.cloud.openfeign.FeignClient;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RequestMethod;
  5. /**
  6. * @author heyunlin
  7. * @version 1.0
  8. */
  9. @FeignClient("provider")
  10. public interface FeignService {
  11. @RequestMapping(value = "/user/name", method = RequestMethod.GET)
  12. String name();
  13. }

5、最后,创建一个控制器类,类名随便取

  1. package com.example.consumer.controller;
  2. import com.example.consumer.feign.FeignService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestMethod;
  6. import org.springframework.web.bind.annotation.RestController;
  7. /**
  8. * @author heyunlin
  9. * @version 1.0
  10. */
  11. @RestController
  12. @RequestMapping("/user")
  13. public class UserController {
  14. @Autowired
  15. FeignService feignService;
  16. @RequestMapping(value = "/name", method = RequestMethod.GET)
  17. public String name() {
  18. return feignService.name();
  19. }
  20. }

这时候@Autowired会报错,找不到FeignService的bean,因为没有在启动类上面添加@EnableFeignClients注解

  1. package com.example.consumer;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.openfeign.EnableFeignClients;
  5. @EnableFeignClients
  6. @SpringBootApplication
  7. public class ConsumerApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(ConsumerApplication.class, args);
  10. }
  11. }

 

三、测试效果

完成以上操作之后,依次启动euraka-server,provider和consumer

浏览器上访问consumer的控制器地址http://localhost:8086/user/name,发现成功返回了字符串。

四、开启Hystrix实现服务降级

首先需要开启hystrix

在pom.xml文件中添加配置

  1. feign:
  2. hystrix:
  3. enabled: true

然后创建一个FeignService的实现类,交给spring管理

  1. package com.example.consumer.feign;
  2. import org.springframework.stereotype.Component;
  3. /**
  4. * @author heyunlin
  5. * @version 1.0
  6. */
  7. @Component
  8. public class FeignServiceImpl implements FeignService {
  9. @Override
  10. public String name() {
  11. return "error";
  12. }
  13. }

 最后,在FeiginService接口的的@FeiginCilent注解上指定fallback=FeignServiceImpl.class

@FeignClient(value = "provider", fallback = FeignServiceImpl.class)

完成以上配置之后,重启consumer,访问http://localhost:8086/user/name时正确调用了provider的控制器方法,得到了正确的结果。

接着把关掉provider项目,再访问,发现调用失败,成功执行了配置的降级方法,直接返回了error

好了,springboot整合feign的介绍到这里就完了,代码已开源,按需获取~

注册中心

eurekaicon-default.png?t=N7T8https://gitee.com/he-yunlin/eureka.git服务提供者

providericon-default.png?t=N7T8https://gitee.com/he-yunlin/provider.git服务消费者

consumericon-default.png?t=N7T8https://gitee.com/he-yunlin/consumer.git

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/145798
推荐阅读
相关标签
  

闽ICP备14008679号