Spring Cloud 学习 (三) Feign
2024-10-18 17:59:21
新建 spring-cloud-eureka-feign-client Module
pom
<parent>
<artifactId>spring-cloud-parent</artifactId>
<groupId>com.karonda</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-eureka-feign-client</artifactId>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
application.yml
server:
port: 8031
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
spring:
application:
name: feign-client
启动类
@EnableFeignClients // 开启 Feign Client
@EnableEurekaClient
@SpringBootApplication
public class EurekaFeignClientApp {
public static void main(String[] args){
SpringApplication.run(EurekaFeignClientApp.class, args);
}
}
FeignClient
@FeignClient("eureka-client")
public interface EurekaClientFeign {
@GetMapping("/hi")
String sayHi(@RequestParam(value = "name") String name);
}
Controller
@RestController
public class FeignController {
@Autowired
EurekaClientFeign eurekaClientFeign;
@GetMapping("/hi")
public String sayHi(@RequestParam String name){
return eurekaClientFeign.sayHi(name);
}
}
测试
- 启动 eureka-server
- 启动 eureka-client (两个实例:一个 8011 端口,一个 8012 端口)
- 启动 eureka-feign-client
多次访问 http://localhost:8031/hi?name=victor 可以看到 8011 和 8012 端口交替出现
添加失败重试
添加配置
@Configuration
public class FeignConfig {
@Bean
public Retryer feignRetryer(){
return new Retryer.Default(100, SECONDS.toMillis(1), 5);
}
}
修改 FeignClient
@FeignClient(value = "eureka-client", configuration = FeignConfig.class)
public interface EurekaClientFeign {
@GetMapping("/hi")
String sayHi(@RequestParam(value = "name") String name);
}
使用 HttpClient 或 OkHttp
在 Feign 中,Client 是一个非常重要的组件,Feign 最终发送 Request 请求以及接收 Response 响应都是由 Client 组件完成的。Client 在 Feign 源码中是一个接口,在默认的情况下, Client 的实现类是 Client.Default, Client.Default 是由 HttpURLConnnection 来实现网络请求的。Client 还支持 HttpClient 和 OkhHttp 来进行网络请求
直接添加 HttpClient 或 OkhHttp 依赖包,Feign 会自动使用对应的网络请求框架
完整代码:GitHub
本人 C# 转 Java 的 newbie, 如有错误或不足欢迎指正,谢谢
最新文章
- FP_PR2SAP 除包材、半成品以外的半成品下层物料展望期7天更改为40日
- Nginx和PHP-FPM的启动/重启脚本 [转发]
- 执行maven-build.cmd失败
- 深入掌握include_once与require_once的区别
- DebugDiag收集Dump的使用说明
- codeforces 681D Gifts by the List dfs+构造
- Cocos2d-x实例:设置背景音乐与音效-HelloWorld场景实现
- MyEclipse—怎样在MyEclipse中创建servlet3.0
- gulp4个基础API
- wireshark_帧信息
- postgres 11 单实例最大支持多少个database?
- 12、多线程:Threading、守护线程
- 50个常用的sql语句
- 如何使用 Telegram
- C#连接MySQL 操作步骤
- linux系统中RPM包的通用命名规则
- handler与anr机制
- javascript的原始类型(primitive type)之间的关系。
- 织梦dedecms修改include和plus重命名提高安全性防漏洞注入挂马
- nodejs操作图片方法
热门文章
- 如何使用捷映App制作朋友圈九宫格视频图片合集
- EntityFramework Core上下文实例池原理分析
- python实现对于告警规则的判断思路
- Metasploitable3学习笔记--永恒之蓝漏洞复现
- python代码实现抢票助手
- ubuntu下安装ESP8266开发环境步骤中可能出现的问题及解决办法
- gdb 调试 报 stepping until--- has no line number information
- binary hacks读数笔记(ld 链接讲解 一)
- Apache Shiro (Shiro-550)(cve_2016_4437)远程代码执行 - 漏洞复现
- 六:Redis配制文件