一、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负载均衡(五)的更多相关文章

  1. java框架之SpringCloud(4)-Ribbon&Feign负载均衡

    在上一章节已经学习了 Eureka 的使用,SpringCloud 也提供了基于 Eureka 负载均衡的两种方案:Ribbon 和 Feign. Ribbon负载均衡 介绍 SpringCloud ...

  2. SpringCloud 进阶之Ribbon和Feign(负载均衡)

    1. Ribbon 负载均衡 Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端,负载均衡的工具; 1.1 Ribbon 配置初步 1.1.1 修改 micros ...

  3. SpringCloud之Feign 负载均衡请求超时时间

    版本声明: SpringCloud:Greenwich.SR4 SpringBoot:2.1.9.RELEASE Feign调用服务的默认时长是1秒钟,也就是如果超过1秒没连接上或者超过1秒没响应,那 ...

  4. SpringCloud学习(5)——Feign负载均衡

    Feign概述 Feign是声明式的Web服务客户端, 使得编写Web服务客户端变的非常容易, 只需要创建一个接口, 然后在上面添加注解即可. Feign旨在使编写Java Http客户端变的更容易. ...

  5. 4.Spring Cloud初相识--------Feign负载均衡

    前言: 在上一节里,我们学习了ribbon的使用. 我们了解到ribbon是一个客户端负载均衡机制. 而我们今天要讲的Feign呢,也是一款客户端负载均衡机制. 或者这样说,Feign封装了ribbo ...

  6. feign 负载均衡熔断器

    feign:和zuul配合进行负载均衡. 注解的含义: @EnableDiscoveryClient 声明它是一个资源服务端,即可以通过某些接口调用一些资源: @EnableFeignClients ...

  7. Feign 负载均衡

    一.是什么 Feign 是一个声明式 WebService 客户端.使用 Feign 能让编写 Web Service 客户端更加简单,他的使用方法是定义一个接口,然后在上面添加注解.同时也支持 JA ...

  8. SpringCloud之Feign负载均衡(四)

    整合Feign pom.xml <dependency> <groupId>org.springframework.cloud</groupId> <arti ...

  9. SpringCloud的入门学习之概念理解、Feign负载均衡入门

    1.Feign是SpringCloud的一个负载均衡组件. Feign是一个声明式WebService客户端.使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口, ...

随机推荐

  1. BZOJ4361 isn(动态规划+树状数组+容斥原理)

    首先dp出长度为i的不下降子序列个数,显然这可以树状数组做到O(n2logn). 考虑最后剩下的序列是什么,如果不管是否合法只是将序列删至只剩i个数,那么方案数显然是f[i]*(n-i)!.如果不合法 ...

  2. bootstrap-table 回显选中行,行样式

    { filed:'status', checkbox:true, formatter:function(value,row,index){ if (row.status == 1) //根据行里字段判 ...

  3. [bzoj5285] [HNOI2018]寻宝游戏

    Description 某大学每年都会有一次Mystery Hunt的活动,玩家需要根据设置的线索解谜,找到宝藏的位置,前一年获胜的队伍可以获得这一年出题的机会. 作为新生的你,对这个活动非常感兴趣. ...

  4. wmic的用法

    原始文章链接:http://blog.sina.com.cn/s/blog_5fb265c70100w4d0.html 一.wmic的基本命令格式简析 经常看网上的相关资料的话,读者可能会对wmic有 ...

  5. Adore 解题报告

    Adore 问题描述 小\(\text{w}\) 偶然间得到了\(1\)个 \(DAG\). 这个 \(DAG\) 有 \(m\) 层,第\(1\)层只有\(1\)个源点,最后\(1\)层只有\(1\ ...

  6. final变量属性小记

    final 修饰符对于类成员变量来说,具备语法上不可变的特性:对于类成员方法来说,具备语法上子类不可覆盖重写的特性(能被继承的前提下). 但 final 并不限制子类对父类被修饰声明的成员变量进行覆盖 ...

  7. 运用yunwei.zip压缩包安装过程:

    12   yum install lrzsz -y     13  rz    14  ll    15  unzip -o yunwei.zip     16  unzip yunwei.zip  ...

  8. E. Intercity Travelling

    E. Intercity Travelling time limit per test 1.5 seconds memory limit per test 256 megabytes input st ...

  9. java的GC与内存泄漏

    从诞生至今,20多年过去,Java至今仍是使用最为广泛的语言.这仰赖于Java提供的各种技术和特性,让开发人员能优雅的编写高效的程序.今天我们就来说说Java的一项基本但非常重要的技术内存管理 了解C ...

  10. source改变当前路径

    转摘自:http://hi.baidu.com/homappy/item/90e416525d2faf958c12edb7 Shell 脚本执行有三种方法 bash 脚本名 sh 脚本名 chmod ...