• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

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

武飞扬头像
Java程序员-小白
帮助1

目录

一、服务提供者

二、服务消费者

三、测试效果

四、开启Hystrix实现服务降级


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

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

springboot整合eureka、config搭建注册中心和配置中心学新通https://blog.csdn.net/heyl163_/article/details/131715281首先,要实现服务间的调用,需要有服务提供者和服务消费者,创建两个项目,分别用于服务提供者和服务消费者。

一、服务提供者

创建一个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.  
     
  6.  
    <parent>
  7.  
    <groupId>org.springframework.boot</groupId>
  8.  
    <artifactId>spring-boot-starter-parent</artifactId>
  9.  
    <version>2.3.4.RELEASE</version>
  10.  
    <relativePath/>
  11.  
    </parent>
  12.  
     
  13.  
    <groupId>com.example</groupId>
  14.  
    <artifactId>provider</artifactId>
  15.  
    <version>0.0.1-SNAPSHOT</version>
  16.  
     
  17.  
    <properties>
  18.  
    <java.version>1.8</java.version>
  19.  
    <eureka.version>1.4.4.RELEASE</eureka.version>
  20.  
    </properties>
  21.  
     
  22.  
    <dependencyManagement>
  23.  
    <dependencies>
  24.  
    <dependency>
  25.  
    <groupId>org.springframework.cloud</groupId>
  26.  
    <artifactId>spring-cloud-dependencies</artifactId>
  27.  
    <version>Hoxton.SR12</version>
  28.  
    <type>pom</type>
  29.  
    <scope>import</scope>
  30.  
    </dependency>
  31.  
     
  32.  
    <dependency>
  33.  
    <groupId>org.springframework.cloud</groupId>
  34.  
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
  35.  
    <version>${eureka.version}</version>
  36.  
    </dependency>
  37.  
    </dependencies>
  38.  
    </dependencyManagement>
  39.  
     
  40.  
    <dependencies>
  41.  
    <dependency>
  42.  
    <groupId>org.springframework.boot</groupId>
  43.  
    <artifactId>spring-boot-starter-web</artifactId>
  44.  
    </dependency>
  45.  
     
  46.  
    <dependency>
  47.  
    <groupId>org.springframework.boot</groupId>
  48.  
    <artifactId>spring-boot-starter-test</artifactId>
  49.  
    <scope>test</scope>
  50.  
    </dependency>
  51.  
     
  52.  
    <dependency>
  53.  
    <groupId>org.springframework.cloud</groupId>
  54.  
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
  55.  
    </dependency>
  56.  
    </dependencies>
  57.  
     
  58.  
    <build>
  59.  
    <plugins>
  60.  
    <plugin>
  61.  
    <groupId>org.springframework.boot</groupId>
  62.  
    <artifactId>spring-boot-maven-plugin</artifactId>
  63.  
    </plugin>
  64.  
    </plugins>
  65.  
    </build>
  66.  
    </project>
学新通

2、修改系统配置文件

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

3、创建一个controller

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

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

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

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

二、服务消费者

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.  
     
  6.  
    <parent>
  7.  
    <groupId>org.springframework.boot</groupId>
  8.  
    <artifactId>spring-boot-starter-parent</artifactId>
  9.  
    <version>2.3.4.RELEASE</version>
  10.  
    <relativePath/>
  11.  
    </parent>
  12.  
     
  13.  
    <groupId>com.example</groupId>
  14.  
    <artifactId>consumer</artifactId>
  15.  
    <version>0.0.1-SNAPSHOT</version>
  16.  
     
  17.  
    <properties>
  18.  
    <java.version>1.8</java.version>
  19.  
    <eureka.version>1.4.4.RELEASE</eureka.version>
  20.  
    </properties>
  21.  
     
  22.  
    <dependencyManagement>
  23.  
    <dependencies>
  24.  
    <dependency>
  25.  
    <groupId>org.springframework.cloud</groupId>
  26.  
    <artifactId>spring-cloud-dependencies</artifactId>
  27.  
    <version>Hoxton.SR12</version>
  28.  
    <type>pom</type>
  29.  
    <scope>import</scope>
  30.  
    </dependency>
  31.  
     
  32.  
    <dependency>
  33.  
    <groupId>org.springframework.cloud</groupId>
  34.  
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
  35.  
    <version>${eureka.version}</version>
  36.  
    </dependency>
  37.  
    </dependencies>
  38.  
    </dependencyManagement>
  39.  
     
  40.  
    <dependencies>
  41.  
    <dependency>
  42.  
    <groupId>org.springframework.boot</groupId>
  43.  
    <artifactId>spring-boot-starter-web</artifactId>
  44.  
    </dependency>
  45.  
     
  46.  
    <dependency>
  47.  
    <groupId>org.springframework.boot</groupId>
  48.  
    <artifactId>spring-boot-starter-test</artifactId>
  49.  
    <scope>test</scope>
  50.  
    </dependency>
  51.  
     
  52.  
    <dependency>
  53.  
    <groupId>org.springframework.cloud</groupId>
  54.  
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
  55.  
    </dependency>
  56.  
     
  57.  
    <dependency>
  58.  
    <groupId>org.springframework.cloud</groupId>
  59.  
    <artifactId>spring-cloud-starter-openfeign</artifactId>
  60.  
    </dependency>
  61.  
    </dependencies>
  62.  
    </project>
学新通

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

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

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

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

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

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

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

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

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

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

  1.  
    package com.example.consumer;
  2.  
     
  3.  
    import org.springframework.boot.SpringApplication;
  4.  
    import org.springframework.boot.autoconfigure.SpringBootApplication;
  5.  
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  6.  
    import org.springframework.cloud.openfeign.EnableFeignClients;
  7.  
     
  8.  
    @EnableFeignClients
  9.  
    @EnableDiscoveryClient
  10.  
    @SpringBootApplication
  11.  
    public class ConsumerApplication {
  12.  
     
  13.  
    public static void main(String[] args) {
  14.  
    SpringApplication.run(ConsumerApplication.class, args);
  15.  
    }
  16.  
     
  17.  
    }
学新通

三、测试效果

完成以上操作之后,依次启动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.  
     
  3.  
    import org.springframework.stereotype.Component;
  4.  
     
  5.  
    /**
  6.  
    * @author heyunlin
  7.  
    * @version 1.0
  8.  
    */
  9.  
    @Component
  10.  
    public class FeignServiceImpl implements FeignService {
  11.  
     
  12.  
    @Override
  13.  
    public String name() {
  14.  
    return "error";
  15.  
    }
  16.  
     
  17.  
    }
学新通

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

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

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

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

学新通

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

注册中心

eureka学新通https://gitee.com/he-yunlin/eureka.git服务提供者

provider学新通https://gitee.com/he-yunlin/provider.git服务消费者

consumer学新通https://gitee.com/he-yunlin/consumer.git

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgaaabe
系列文章
更多 icon
同类精品
更多 icon
继续加载