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

目录

一、服务提供者

二、服务消费者

三、测试效果

四、开启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

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.4.RELEASE</version><relativePath/></parent><groupId>com.example</groupId><artifactId>provider</artifactId><version>0.0.1-SNAPSHOT</version><properties><java.version>1.8</java.version><eureka.version>1.4.4.RELEASE</eureka.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR12</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId><version>${eureka.version}</version></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>

2、修改系统配置文件

server:port: 8085spring:application:name: providereureka:instance:hostname: localhostclient:service-url:defaultZone: http://${eureka.instance.hostname}:8761/eureka

3、创建一个controller

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

package com.example.provider.controller;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;/*** @author heyunlin* @version 1.0*/
@RestController
@RequestMapping(path = "/user", produces = "application/json;charset=utf-8")
public class UserController {@RequestMapping(value = "/name", method = RequestMethod.GET)public String name() {return "heyunlin";}}

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

package com.example.provider;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@EnableDiscoveryClient
@SpringBootApplication
public class ProviderApplication {public static void main(String[] args) {SpringApplication.run(ProviderApplication.class, args);}}

二、服务消费者

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

2、修改pom.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.4.RELEASE</version><relativePath/></parent><groupId>com.example</groupId><artifactId>consumer</artifactId><version>0.0.1-SNAPSHOT</version><properties><java.version>1.8</java.version><eureka.version>1.4.4.RELEASE</eureka.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR12</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId><version>${eureka.version}</version></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency></dependencies>
</project>

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

server:port: 8086spring:application:name: consumereureka:instance:hostname: localhostclient:service-url:defaultZone: http://${eureka.instance.hostname}:8761/eureka

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

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

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

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

package com.example.consumer.feign;import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;/*** @author heyunlin* @version 1.0*/
@FeignClient("provider")
public interface FeignService {@RequestMapping(value = "/user/name", method = RequestMethod.GET)String name();}

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

package com.example.consumer.controller;import com.example.consumer.feign.FeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;/*** @author heyunlin* @version 1.0*/
@RestController
@RequestMapping("/user")
public class UserController {@AutowiredFeignService feignService;@RequestMapping(value = "/name", method = RequestMethod.GET)public String name() {return feignService.name();}}

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

package com.example.consumer;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApplication {public static void main(String[] args) {SpringApplication.run(ConsumerApplication.class, args);}}

三、测试效果

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

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

四、开启Hystrix实现降级功能

首先需要开启hystrix

在pom.xml文件中添加配置

feign:hystrix:enabled: true

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

package com.example.consumer.feign;import org.springframework.stereotype.Component;/*** @author heyunlin* @version 1.0*/
@Component
public class FeignServiceImpl implements FeignService {@Overridepublic String name() {return "error";}}

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

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

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

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

 

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

注册中心

eurekahttps://gitee.com/he-yunlin/eureka.git服务提供者

providerhttps://gitee.com/he-yunlin/provider.git服务消费者

consumerhttps://gitee.com/he-yunlin/consumer.git

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/28702.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【已解决】哪些软件可以解压RAR文件?

RAR文件是我们日常生活及工作中经常用的压缩文件&#xff0c;文件压缩后可以更方便储存或者传输&#xff0c;后续要使用的时候再进行解压。 那RAR文件如何解压呢&#xff1f;哪些软件可以用来解压RAR文件&#xff1f;在这一方面还是小白的小伙伴可以来看看下面的分享。 解压任…

Cadence Allegro PCB设计88问解析(三十一) 之 Allegro 中 打印(Plot)设置

一个学习信号完整性仿真的layout工程师 在PCB进行投板时&#xff0c;往往会打印一下装备层(Assembly)&#xff0c;给贴片&#xff0c;用于核对器件的信息等。下面简单介绍Allegro中打印(Plot)设置。 1. 在Allegro的菜单下选择File命令&#xff0c;点击Plot Setup&#xff0c;会…

【自动驾驶汽车量子群粒子过滤器】用于无人驾驶汽车列车定位的量子粒子滤波研究(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…

SpringMVC学习笔记--下篇

SpringMVC学习笔记 文章目录 SpringMVC学习笔记1、JSON1.1、什么是JSON1.2、JSON 和 JavaScript 对象互转1.3、Controller返回JSON数据1.3.1、使用Jackson工具1.3.1.1、乱码问题的代码优化1.3.1.2、集合测试1.3.1.3、输出时间对象1.3.1.4、抽取为工具类 1.3.2、使用FastJson的工…

CCF真题练习:202209-1如此编码

题目背景 某次测验后&#xff0c;顿顿老师在黑板上留下了一串数字 23333 便飘然而去。凝望着这个神秘数字&#xff0c;小 P 同学不禁陷入了沉思…… 题目描述 已知某次测验包含 n 道单项选择题&#xff0c;其中第 i 题&#xff08;1≤i≤n&#xff09;有 个选项&#xff0c;…

Android ViewGroup onDraw为什么没调用

ViewGroup&#xff0c;它本身并没有任何可画的东西&#xff0c;它是一个透明的控件&#xff0c;因些并不会触发onDraw&#xff0c;但是你现在给LinearLayout设置一个背景色&#xff0c;其实这个背景色不管你设置成什么颜色&#xff0c;系统会认为&#xff0c;这个LinearLayout上…

android APP外包开发的三种方式

开发android APP有三种方式&#xff0c;分别是原生开发、混合开发和无代码开发&#xff0c;原生开发对开发者有一定要求&#xff0c;但用户体验好&#xff1b;混合开发是使用H5开发&#xff0c;对开发者要求相对较低&#xff1b;而无代码开发则是通过操作界面搭建APP&#xff0…

(数组与矩阵) 剑指 Offer 50. 第一个只出现一次的字符 ——【Leetcode每日一题】

❓ 剑指 Offer 50. 第一个只出现一次的字符 难度&#xff1a;简单 在字符串 s 中找出第一个只出现一次的字符。如果没有&#xff0c;返回一个单空格。 s 只包含小写字母。 示例 1: 输入&#xff1a;s “abaccdeff” 输出&#xff1a;‘b’ 示例 2: 输入&#xff1a;s “”…

Linux之vi命令

vi编辑器 vim/vi是Unix / Linux上最常用的文本编辑器而且功能非常强大。 只有命令&#xff0c;没有菜单。 建议使用vim命令&#xff0c;如果没有这个命令可以使用 yum install -y vim 进行安装 命令模式&#xff1a;又称一般模式 编辑模式&#xff1a;又称底行模式&#xff0c;…

Docker本地镜像发布到私有库

Docker Registry&#xff08;Docker镜像仓库&#xff09; 使用Docker Registry&#xff0c;可以创建私有或公共的镜像仓库&#xff0c;以存储Docker镜像。私有仓库可以用于存储公司内部的镜像&#xff0c;或者用于个人项目的镜像。公共仓库则会将发布的镜像分享到全世界。 1 …

P22-p26

p22光照渲染&#xff0c;自动曝光&#xff0c;雾 如果屋子里黑&#xff0c;可以在世界大纲搜索light把平行光和天光改变为可移动&#xff0c;屋子里就亮了&#xff08;如果屋子内还没亮就重新再构件一次光照&#xff09; 1&#xff0c;虚幻引擎自带光源 定向光源一般模拟太阳…

Ubuntu 更改内核启动顺序

ubuntu服务器系统中用run包安装了某卡的驱动&#xff0c;后来又安装了docker&#xff0c;重启后&#xff0c;驱动失效。 经分析 安装docker时&#xff0c;又把新的linux内核安装上了。驱动是安装在旧内核上。 然会重新安装驱动&#xff0c;失败&#xff0c;确认是因为驱动只支…