springcloud+Feign服务调用

2024-08-28 02:38

本文主要是介绍springcloud+Feign服务调用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

springcloud+Feign服务调用

1.简介

之前我们在搭建Ribbon负载均衡的时候,服务调用采用的RestTemplate。

相关链接:https://blog.csdn.net/u013071014/article/details/111361176

@RequestMapping(path = "/getConsumer", method = RequestMethod.GET)public String getConsumer() {String result = restTemp.getForObject("http://springcloud-provider1/provider/getProvider", String.class);return result;}

访问URL是采用字符串拼接的方式,但是如果input 参数有很多,拼接起来就相当麻烦。

使用Feign可以简化服务调用。

2.Feign的使用

2.1.前置工作

1.Eureka server

https://blog.csdn.net/u013071014/article/details/111031306

2.服务提供者

https://blog.csdn.net/u013071014/article/details/111361176

2.2.Feign的配置

1.在parent项目右键,New—> Maven Module,名称命名为springcloud-consumer2

2.在springcloud-consumer2 的pom.xml文件中新增依赖

<packaging>jar</packaging><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- OpenFeign在Feign的基础上支持了Spring MVC的注解,如@RequesMapping等等。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency>
</dependencies>

3.配置application.yml

server:port: 8092

4.在主启动类加上 @EnableFeignClients注解,开启Spring Cloud Feign的支持功能 。

package com.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class SpringCloudConsumerApplication {public static void main(String[] args) {// TODO Auto-generated method stubSpringApplication.run(SpringCloudConsumerApplication.class, args);}}

5.创建接口,添加@FeignClient注解,name属性为服务提供者的服务名

package com.springcloud.service;import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;@FeignClient(name = "springcloud-provider1")
public interface ProviderFeignClient {@RequestMapping(value = "/provider/getProvider", method = RequestMethod.GET)public String getProvider();
}

6.创建controller

package com.springcloud.controller;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;import com.springcloud.service.ProviderFeignClient;@RestController
@RequestMapping(path = "/consumer")
public class ConsumerController {@Autowiredprivate ProviderFeignClient providerFeignClient;@RequestMapping(value = "/getConsumer", method = RequestMethod.GET)public String getConsumer() {return this.providerFeignClient.getProvider();}
}

7.启动eureka server,三个provider和consumer2,访问api测试
在这里插入图片描述

多次call API访问,我们会发现返回结果以轮询的方式出现。事实如此, Feign 中本身已经集成了Ribbon依赖和自动配置,因此我们不需要额外引入依赖,也不需要再注册RestTemplate 对象

8.代码结构
在这里插入图片描述

3.Feign + Hystrix

1.在springcloud-consumer2的pom.xml文件中新增依赖

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

2.修改springcloud-consumer2的application.yml配置文件

server:port: 8092# 开启Feign的Hystrix支持,默认是false  
feign:hystrix:enabled: true

3.在主启动类添加@EnableHystrix注解

package com.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
public class SpringCloudConsumerApplication {public static void main(String[] args) {// TODO Auto-generated method stubSpringApplication.run(SpringCloudConsumerApplication.class, args);}}

4.新建一个类,实现FeignClient接口或FallbackFactory接口。使用FallbackFactory的好处在于可以获取到触发断路器的异常信息。下面我们以FallbackFactory为例。

package com.springcloud.service;import org.springframework.stereotype.Component;import feign.hystrix.FallbackFactory;@Component
public class ProviderFeignClientFallbackFactory implements FallbackFactory<ProviderFeignClient>{@Overridepublic ProviderFeignClient create(Throwable cause) {// TODO Auto-generated method stubreturn new ProviderFeignClient() {@Overridepublic String getProvider() {// TODO Auto-generated method stubreturn "发生熔断!!! 异常信息: " + cause.getMessage();}};}}

5.修改FeignClient接口,在属性中加上FallbackFactory处理类

package com.springcloud.service;import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;@FeignClient(name = "springcloud-provider1", fallbackFactory = ProviderFeignClientFallbackFactory.class)
public interface ProviderFeignClient {@RequestMapping(value = "/provider/getProvider", method = RequestMethod.GET)public String getProvider();
}

6.启动eureka server,provider和consumer2,访问api返回正常
在这里插入图片描述

7.为了方便测试异常捕获,我们将provider 抛出一个异常,再次call API测试

package com.springcloud.controller;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping(path = "/provider")
public class ProviderController {@RequestMapping(path = "/getProvider", method = RequestMethod.GET)public String getProvider() throws Exception {throw new Exception("For Test");//return "Provider1";}
}

在这里插入图片描述

4.源码地址

https://github.com/DamonLiu666/springcloud_test

这篇关于springcloud+Feign服务调用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1113497

相关文章

Java Predicate接口定义详解

《JavaPredicate接口定义详解》Predicate是Java中的一个函数式接口,它代表一个判断逻辑,接收一个输入参数,返回一个布尔值,:本文主要介绍JavaPredicate接口的定义... 目录Java Predicate接口Java lamda表达式 Predicate<T>、BiFuncti

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

Spring Security方法级安全控制@PreAuthorize注解的灵活运用小结

《SpringSecurity方法级安全控制@PreAuthorize注解的灵活运用小结》本文将带着大家讲解@PreAuthorize注解的核心原理、SpEL表达式机制,并通过的示例代码演示如... 目录1. 前言2. @PreAuthorize 注解简介3. @PreAuthorize 核心原理解析拦截与

一文详解JavaScript中的fetch方法

《一文详解JavaScript中的fetch方法》fetch函数是一个用于在JavaScript中执行HTTP请求的现代API,它提供了一种更简洁、更强大的方式来处理网络请求,:本文主要介绍Jav... 目录前言什么是 fetch 方法基本语法简单的 GET 请求示例代码解释发送 POST 请求示例代码解释

Java图片压缩三种高效压缩方案详细解析

《Java图片压缩三种高效压缩方案详细解析》图片压缩通常涉及减少图片的尺寸缩放、调整图片的质量(针对JPEG、PNG等)、使用特定的算法来减少图片的数据量等,:本文主要介绍Java图片压缩三种高效... 目录一、基于OpenCV的智能尺寸压缩技术亮点:适用场景:二、JPEG质量参数压缩关键技术:压缩效果对比

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

Feign Client超时时间设置不生效的解决方法

《FeignClient超时时间设置不生效的解决方法》这篇文章主要为大家详细介绍了FeignClient超时时间设置不生效的原因与解决方法,具有一定的的参考价值,希望对大家有一定的帮助... 在使用Feign Client时,可以通过两种方式来设置超时时间:1.针对整个Feign Client设置超时时间

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

Java利用docx4j+Freemarker生成word文档

《Java利用docx4j+Freemarker生成word文档》这篇文章主要为大家详细介绍了Java如何利用docx4j+Freemarker生成word文档,文中的示例代码讲解详细,感兴趣的小伙伴... 目录技术方案maven依赖创建模板文件实现代码技术方案Java 1.8 + docx4j + Fr

SpringBoot首笔交易慢问题排查与优化方案

《SpringBoot首笔交易慢问题排查与优化方案》在我们的微服务项目中,遇到这样的问题:应用启动后,第一笔交易响应耗时高达4、5秒,而后续请求均能在毫秒级完成,这不仅触发监控告警,也极大影响了用户体... 目录问题背景排查步骤1. 日志分析2. 性能工具定位优化方案:提前预热各种资源1. Flowable