Feign负载均衡
Feign是一个声明式的Web Service客户端,比Ribbon好用,默认也是轮巡。我们只需要使用Feign创建一个接口,并用注解就好了。如果你基于spring cloud发布一个接口,实际上就是支持http协议的,对外发布的就是一个最普通的mvc的http接口。我们使用feign注解,实际上它会对这个接口生成动态代理,从eureka的readonly中拿到其他服务信息、进行http请求调用。
feign案例编写:
1. 导包
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/>
</parent>
<!-- springcloud依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka客户端依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- feign客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
2. 配置application.yml文件
# 项目访问路径前缀
server:
context-path: /feign
port: 8085 # 设置服务名称,服务会以这个名字注册到eureka服务器上
spring:
application:
name: feign # 设置eureka服务器的注册地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ #ribbon的超时时间, 防止feign调用超时
ribbon:
ReadTimeout: 15000
ConnectTimeout: 15000
MaxAutoRetries: 1 #同一台实例最大重试次数,不包括首次调用
MaxAutoRetriesNextServer: 1 #重试负载均衡其他的实例最大重试次数,不包括首次调用
OkToRetryOnAllOperations: false #是否所有操作都重试
3. 主函数入口开启eureka和feign客户端
@SpringBootApplication
@EnableEurekaClient//开启eureka
@EnableFeignClients//开启feign
public class FeignMain { public static void main(String[] args) {
new SpringApplicationBuilder(FeignMain.class).web(true).run(args);
}
}
4. 调用接口
@RestController
public class UserController { @Autowired
private UserService userService; @RequestMapping("/test")
public Map test() {
return userService.test("张三");
} @RequestMapping("/testObj")
public User testObj() {
return userService.testObj(new User());
}
}
5. 编写feign调用的服务层。
import java.util.Map;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.model.User;
//name 目标的服务名 目标的url前缀
@FeignClient(name ="demo",path="/demo")
public interface UserService { // 调用police服务的testOut接口
@RequestMapping("/testOut")
public Map test(@RequestParam("name") String name); // 调用police服务的testOutObj接口
@RequestMapping("/testOutObj")
public User testObj(@RequestBody User user);
}
feign客户端就开发完成了; 我们来看看提供服务的另一个项目的接口。
@RestController
public class MyController { @RequestMapping("/testOut")
public Map test(@RequestParam("name") String name,HttpServletRequest req) {
Map m = new HashMap<>();
m.put("url", req.getRequestURL().toString());
m.put("name", name);
try {
Thread.sleep(16000);
} catch (InterruptedException e) {
}
return m;
} @RequestMapping("/testOutObj")
public User testObj(@RequestBody() User user,HttpServletRequest req) {
user.setUrl(req.getRequestURL().toString());
try {
Thread.sleep(14000);
} catch (InterruptedException e) {
}
return user;
}
}
测试: 由于超时时间设置的15s, 而两个接口分别阻塞14s ,16s ,所以第二个接口会超时报错。
Feign负载均衡的更多相关文章
- java框架之SpringCloud(4)-Ribbon&Feign负载均衡
在上一章节已经学习了 Eureka 的使用,SpringCloud 也提供了基于 Eureka 负载均衡的两种方案:Ribbon 和 Feign. Ribbon负载均衡 介绍 SpringCloud ...
- SpringCloud 进阶之Ribbon和Feign(负载均衡)
1. Ribbon 负载均衡 Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端,负载均衡的工具; 1.1 Ribbon 配置初步 1.1.1 修改 micros ...
- SpringCloud之Feign 负载均衡请求超时时间
版本声明: SpringCloud:Greenwich.SR4 SpringBoot:2.1.9.RELEASE Feign调用服务的默认时长是1秒钟,也就是如果超过1秒没连接上或者超过1秒没响应,那 ...
- SpringCloud学习(5)——Feign负载均衡
Feign概述 Feign是声明式的Web服务客户端, 使得编写Web服务客户端变的非常容易, 只需要创建一个接口, 然后在上面添加注解即可. Feign旨在使编写Java Http客户端变的更容易. ...
- Feign负载均衡(五)
一.Feign定义 Feigin是服务消费者,Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单.使用Feign,只需要创建一个接口并注解.它具有可插拔的注解特性,可使用Fei ...
- 4.Spring Cloud初相识--------Feign负载均衡
前言: 在上一节里,我们学习了ribbon的使用. 我们了解到ribbon是一个客户端负载均衡机制. 而我们今天要讲的Feign呢,也是一款客户端负载均衡机制. 或者这样说,Feign封装了ribbo ...
- feign 负载均衡熔断器
feign:和zuul配合进行负载均衡. 注解的含义: @EnableDiscoveryClient 声明它是一个资源服务端,即可以通过某些接口调用一些资源: @EnableFeignClients ...
- Feign 负载均衡
一.是什么 Feign 是一个声明式 WebService 客户端.使用 Feign 能让编写 Web Service 客户端更加简单,他的使用方法是定义一个接口,然后在上面添加注解.同时也支持 JA ...
- SpringCloud之Feign负载均衡(四)
整合Feign pom.xml <dependency> <groupId>org.springframework.cloud</groupId> <arti ...
- SpringCloud的入门学习之概念理解、Feign负载均衡入门
1.Feign是SpringCloud的一个负载均衡组件. Feign是一个声明式WebService客户端.使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口, ...
随机推荐
- Mybatis入门教程之新增、更新、删除功能_java - JAVA
文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 上一节说了Mybatis的框架搭建和简单查询,这次我们来说一说用Mybatis进行基本的增删改操作: 一. 插入一条数据 ...
- KCF跟踪算法
参考:https://www.cnblogs.com/YiXiaoZhou/p/5925019.html 参考:https://blog.csdn.net/shenxiaolu1984/article ...
- 超大文件上传方案(ASP.NET)
ASP.NET上传文件用FileUpLoad就可以,但是对文件夹的操作却不能用FileUpLoad来实现. 下面这个示例便是使用ASP.NET来实现上传文件夹并对文件夹进行压缩以及解压. ASP.NE ...
- 软件安装——internal error2503/2502
安装新的软件后先报internal error 2503,随后报internal error 2502.就是不让我装新的软件,提示说发生严重错误,然后安装失败. Solution for intern ...
- 【bzoj4136】[FJOI2015]带子串包含约束LCS问题
题目描述: 带有子串包含约束的最长公共子序列问题可以具体表述如下. 给定2个长度分别为n和m的序列X和Y,以及一个子串包含约束集S. S中共有k个字符串S={S1,S2,…,Sk},其中字符串Si的长 ...
- nvm 管理 node 版本
nvm 有 Mac 版本 num 亦有 windows 版本(可以搜索 nvm for windows) 安装后 运行 nvm v 可查看版本 运行 nvm install latest 安装最新版本 ...
- Spring定时器Quartz
<bean id="startQuertz" lazy-init="false" autowire="no" class=" ...
- leetcode-mid-Linked list- 230 Kth Smallest Element in a BST
mycode 81.40% # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x ...
- Booting the Linux/ppc kernel without Open Firmware
The DT block format 这一章定义了传递给内核的FDT(flattened device tree)的格式.关于它包含的内容以及内核需要的属性将在后续章节描述. 注:DT block应 ...
- yield(放弃、谦逊、礼让) - 瞬时的,暂时放了马上再抢
两个线程抢占CPU各自执行任务,代码如下: public class Demo03 { public static void main(String[] args) throws Interrupte ...