Feign负载均衡(五)
一、Feign定义
Feigin是服务消费者,Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。feign集成了Ribbon,利用Ribbon维护了MicroServiceCloud-Dept的服务列表信息,并且通过轮询实现了客服端的负载均衡.而与Ribbon不同的是,通过feign只需要定义服务绑定接口声明式的方法,优雅而简单的实现了服务调用
简而言之:是一个声明式的web服务客户端,使得Web服务端变得非常容易,只需要创建一个接口,然后在上面添加注解即可就是:接口+注解,获取调用我们的微服务---接口编程
Feign接口编程

二、Feign操作
1、修改microservicecloud-api
pom文件添加依赖
<!-- feign远程调用 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
新建一个DeptClientService 接口
@FeignClient(name = "MICROSERVICECLOUD-DEPT")//name是一个服务名称,在注册中上面
public interface DeptClientService {
@RequestMapping(value = "/dept/findAll",method = RequestMethod.GET)
public List<Dept> findAll();
}
2、创建microservicecloud-consumer-dept-feign工程
pom文件
<dependencies>
<!-- 引入自己定义的api通用包,可以使用Dept部门Entity -->
<dependency>
<groupId>com.yehui</groupId>
<artifactId>microservicecloud-api</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<!--eureka-server服务端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
yml文件
server:
port: 8087 eureka:
client:
register-with-eureka: true
service-url:
defaultZone: http://localhost:7002/eureka/,http://localhost:7003/eureka/,http://localhost:7001/eureka/
fetch-registry: true
spring:
application:
name: ConSumeAppStartDeptfeign
创建一个controller
@RestController
public class DeptController {
@Autowired
private DeptClientService deptClientService; @RequestMapping("/dept/list")
public List<Dept> findAll() {
return deptClientService.findAll();
}
}
创建启动类
@SpringBootApplication
@EnableFeignClients(basePackages = "com.yehui.feign")//扫描feignClient提供接口的包
public class ConSumeAppStartDeptfeign {
public static void main(String[] args) {
SpringApplication.run(ConSumeAppStartDeptfeign.class);
}
}
启动测试:
启动3个eureka集群
启动3个微服务8001/8002/8003
启动Feign自己启动
Feign自带负载均衡配置顶:
Feign通过接口的方法调用Rest服务(之前是Ribbon+RestTemplate),该请求发送给Eureka服务器(),通过Feign直接找到服务接口,由于在进行服务调用的时候融合了Ribbon技术,所以也支持负载均衡作用
测试访问路径http://localhost:8087/dept/list

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客户端变的更容易. ...
- 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客户端更加简单, 它的使用方法是定义一个接口, ...
随机推荐
- hdu 1575 Tr A (二分矩阵)
Tr A Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- [Leetcode] The minimum depth of binary tree二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- Java代码管理工具SVN系列
SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.互联网上很多版本控制服务已从CVS迁移到Subversion ...
- 打造适合日常使用的ubuntu,以ubuntu 16.04.1 LTS为例(不涉及版本)
因为调试些程序需要用到ubuntu,又不喜欢虚拟机,因此装了双系统,在这过程中因为各种原因ubuntu来回安装过好多次,每次安装到用得爽都要捣鼓很久,也算稍微有点经验心得,将ubuntu调教的过程写在 ...
- 简单瞎搞题(bitset的操作)
链接:https://www.nowcoder.com/acm/contest/132/C来源:牛客网 题目 一共有 n个数,第 i 个数是 xi xi 可以取 [li , ri] 中任意的一个值. ...
- white-space——处理元素内的空白
定义和用法 white-space 属性设置如何处理元素内的空白.这个属性声明建立布局过程中如何处理元素中的空白符.值 pre-wrap 和 pre-line 是 CSS 2.1 中新增的. 默认 ...
- CSS学习之float解析
转自:http://www.w3cplus.com/css/float.html 一.float是什么? float即为浮动,在CSS中的作用是使元素脱离正常的文档流并使其移动到其父元素的“最左边”或 ...
- jaspersoft中分组打印
一:前言 使用IReport已经四个月了,最近在做一个保镖,是要按照类型分类,并且这些类型要横着打印,最后还要算这个类型金额的总值,这张报表现是说需要用到子报表,最后和一个同事一起用group来分组做 ...
- OpenStack搭建glance
1.创建数据库 mysql -uroot -p create database glance; grant all privileges on glance.* to glance@'localhos ...
- CentOS7 Tomcat 启动过程很慢,JVM上的随机数与熵池策略
1. CentOS7 Tomcat 启动过程很慢 在centos启动官方的tomcat时,启动过程很慢,需要几分钟,经过查看日志,发现耗时在这里:是session引起的随机数问题导致的: <co ...