Springboot集成openFeign实现服务调用

简介: Springboot集成openFeign实现服务调用

openFeign是SpringCloud体系下进行服务调用的框架,他是一款声明式的REST服务调用框架。

一、openFeign的配置和使用

使用openFeign需要引入依赖spring-cloud-starter-openfeign,本文以nacos作为注册中心,需要引入spring-cloud-starter-alibaba-nacos-discovery依赖。

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

properties中配置项:

#feign start
feign.client.config.feignName.connectTimeout=5000
feign.client.config.feignName.readTimeout=5000
#日志级别
feign.client.config.feignName.loggerLevel=full

启动类上的配置,需要加上@EnableFeignClients注解。

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class SpringCloudStarterOpenfeignExamplesApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringCloudStarterOpenfeignExamplesApplication.class, args);
    }
}

服务提供方的只需要按照常规的@RestController进行编写:

@RestController
public class LeafController {
    @RequestMapping(value = "/api/segment/get/{key}")
    public String getSegmentId(@PathVariable("key") String key) {
        return get(key, segmentService.getId(key));
    }
}

服务调用方的配置,需要加上openFeign的注解@FeignClient,去value值即为需要远程调用的服务名。

@FeignClient("yangnkMall-uniqID")
public interface UniqIdApi {
    @RequestMapping(value = "/api/segment/get/{key}")
    String getSegmentId(@PathVariable("key") String key);
}

二、openFeign的自定义拓展

1、通过Ribbon自定义负载均衡

openFeign集成了Ribbon,默认支持Ribbon进行负责均衡,直接在properties文件中加上对应参数即可。

#Ribbon start
#请求时间5秒
ribbon.ReadTimeout=5000
#连接时间5秒
ribbon.ConnectTimeout=5000

同时可以自定义负载均衡策略:

@Slf4j
@Configuration
public class MyRibbonConfig {
    @Bean
    public IRule myRule(){
        return new RandomRule();//定义为随机,默认是轮询
    }

2、自定义HttpClient

openFeign的默认的http调用方式是JDK原生HttpURLConnection,缺乏线程池和自定义调用参数等设置。可以选择第三方客户端HttpClient,如果需要使用HttpClient做服务调用的客户端,需要引入Httpclient依赖。

<dependency>
        <groupId>io.github.openfeign</groupId>
        <artifactId>feign-httpclient</artifactId>
    </dependency>

properties中配置项:

# 开启HttpClient
feign.httpclient.enabled=true
# 使用HttpClient 5(Apache HttpClient 5)
feign.httpclient.hc5.enabled=false
# 禁用OkHttp
feign.okhttp.enabled=false

三、原理

四、实现

代码实现:https://github.com/yangnk/yangnkMall/tree/master/Order/src/main/java/com/yangnk/order/myTest


TODO

  • 充实文章中各章节;
  • 补充原理实现;

参考资料

  1. Spring Cloud openFeign官方文档:https://docs.spring.io/spring-cloud-openFeign/docs/2.2.7.RELEASE/reference/html/
  2. Github源码:https://github.com/openFeign/feign
  3. 『openFeign』使用与配置篇:https://juejin.cn/post/7130041310177820685#heading-12
  4. 详细讲解openFeign的使用姿势!:https://developer.aliyun.com/article/775626#slide-0 (简单易懂,可以首先参考这篇文章)
  5. 服务调用Ribbon、openFeign:https://juejin.cn/post/6992119925519155207#heading-18 (openFeign和Ribbon结合的文章)
  6. OpenFeign 简单使用:https://blog.csdn.net/qq_41538097/article/details/124112466 (其中解释了OpenFeign的原理)
  7. 代码参考:https://github.com/yehongzhi/example/blob/main/consumer/src/main/resources/application.yml


目录
相关文章
|
2月前
|
存储 设计模式 缓存
OpenFeign集成Ribbon负载均衡-过滤和选择服务核心实现
该文章主要介绍了如何在OpenFeign中集成Ribbon以实现负载均衡,并详细分析了Ribbon中服务选择和服务过滤的核心实现过程。文章还涉及了Ribbon中负载均衡器(ILoadBalancer)和负载均衡策略(IRule)的初始化方式。
OpenFeign集成Ribbon负载均衡-过滤和选择服务核心实现
|
2月前
|
缓存 负载均衡 Java
OpenFeign最核心组件LoadBalancerFeignClient详解(集成Ribbon负载均衡能力)
文章标题为“OpenFeign的Ribbon负载均衡详解”,是继OpenFeign十大可扩展组件讨论之后,深入探讨了Ribbon如何为OpenFeign提供负载均衡能力的详解。
OpenFeign最核心组件LoadBalancerFeignClient详解(集成Ribbon负载均衡能力)
|
3月前
|
监控 druid Java
spring boot 集成配置阿里 Druid监控配置
spring boot 集成配置阿里 Druid监控配置
191 6
|
3月前
|
Java 关系型数据库 MySQL
如何实现Springboot+camunda+mysql的集成
【7月更文挑战第2天】集成Spring Boot、Camunda和MySQL的简要步骤: 1. 初始化Spring Boot项目,添加Camunda和MySQL驱动依赖。 2. 配置`application.properties`,包括数据库URL、用户名和密码。 3. 设置Camunda引擎属性,指定数据源。 4. 引入流程定义文件(如`.bpmn`)。 5. 创建服务处理流程操作,创建控制器接收请求。 6. Camunda自动在数据库创建表结构。 7. 启动应用,测试流程启动,如通过服务和控制器开始流程实例。 示例代码包括服务类启动流程实例及控制器接口。实际集成需按业务需求调整。
217 4
|
3月前
|
消息中间件 Java 测试技术
【RocketMQ系列八】SpringBoot集成RocketMQ-实现普通消息和事务消息
【RocketMQ系列八】SpringBoot集成RocketMQ-实现普通消息和事务消息
175 1
|
4月前
|
消息中间件 Java Kafka
springboot集成kafka
springboot集成kafka
125 2
|
4月前
|
消息中间件 Java Kafka
集成Kafka到Spring Boot项目中的步骤和配置
集成Kafka到Spring Boot项目中的步骤和配置
165 7
|
4月前
|
druid Java 关系型数据库
在Spring Boot中集成Druid实现多数据源有两种常用的方式:使用Spring Boot的自动配置和手动配置。
在Spring Boot中集成Druid实现多数据源有两种常用的方式:使用Spring Boot的自动配置和手动配置。
456 5
|
4月前
|
监控 前端开发 Java
五分钟后,你将学会在SpringBoot项目中如何集成CAT调用链
五分钟后,你将学会在SpringBoot项目中如何集成CAT调用链
|
3月前
|
消息中间件 Java Kafka
Spring Boot与Apache Kafka Streams的集成
Spring Boot与Apache Kafka Streams的集成
下一篇
无影云桌面