新建 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);
}
}

测试

  1. 启动 eureka-server
  2. 启动 eureka-client (两个实例:一个 8011 端口,一个 8012 端口)
  3. 启动 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, 如有错误或不足欢迎指正,谢谢

最新文章

  1. FP_PR2SAP 除包材、半成品以外的半成品下层物料展望期7天更改为40日
  2. Nginx和PHP-FPM的启动/重启脚本 [转发]
  3. 执行maven-build.cmd失败
  4. 深入掌握include_once与require_once的区别
  5. DebugDiag收集Dump的使用说明
  6. codeforces 681D Gifts by the List dfs+构造
  7. Cocos2d-x实例:设置背景音乐与音效-HelloWorld场景实现
  8. MyEclipse—怎样在MyEclipse中创建servlet3.0
  9. gulp4个基础API
  10. wireshark_帧信息
  11. postgres 11 单实例最大支持多少个database?
  12. 12、多线程:Threading、守护线程
  13. 50个常用的sql语句
  14. 如何使用 Telegram
  15. C#连接MySQL 操作步骤
  16. linux系统中RPM包的通用命名规则
  17. handler与anr机制
  18. javascript的原始类型(primitive type)之间的关系。
  19. 织梦dedecms修改include和plus重命名提高安全性防漏洞注入挂马
  20. nodejs操作图片方法

热门文章

  1. 如何使用捷映App制作朋友圈九宫格视频图片合集
  2. EntityFramework Core上下文实例池原理分析
  3. python实现对于告警规则的判断思路
  4. Metasploitable3学习笔记--永恒之蓝漏洞复现
  5. python代码实现抢票助手
  6. ubuntu下安装ESP8266开发环境步骤中可能出现的问题及解决办法
  7. gdb 调试 报 stepping until--- has no line number information
  8. binary hacks读数笔记(ld 链接讲解 一)
  9. Apache Shiro (Shiro-550)(cve_2016_4437)远程代码执行 - 漏洞复现
  10. 六:Redis配制文件