集成Ribbon的客户端调用工具——Feign
什么是Feign?
客户端调用工具
- Ribbon+RestTemplate
- Feign
Feign特性:
- Feign本身包含Ribbon
- Fegin是一个采用基于接口的注解的声明式客户端调用工具,更加简便
基于上个环境来做一些改造:
https://github.com/HCJ-shadow/Ribbon
创建msc-consumer-feign-80工程

pom依赖
主要是spring-cloud-starter-openfeign
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>
<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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- Eureka客户端启动需要依赖web模块-->
<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>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
定义service接口
package zkrun.top.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient("msc-provider") //@FeignClient("服务名")
public interface FeignService {
@RequestMapping(value = "/info/get")
public String request();
}
定义Controller调用Service接口
package zkrun.top.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import zkrun.top.service.FeignService;
@RestController
public class FeignController {
@Autowired
FeignService feignService;
@RequestMapping(value = "/feign/info/get")
public String request()
{
return this.feignService.request();
}
}
主启动类
package zkrun.top;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
@EnableEurekaClient
public class App_Consumer_Feign_80
{
public static void main(String[] args)
{
SpringApplication.run(App_Consumer_Feign_80.class, args);
}
}
application.yaml
server:
port: 80
eureka:
client:
service-url:
defaultZone: http://eureka6001.com:6001/eureka/,http://eureka6002.com:6002/eureka/,http://eureka6003.com:6003/eureka/
spring:
application:
name: feign-consumer
运行测试

集成了轮询效果。
小结:
- Feign的调用逻辑,访问Controller定义的路径,调用feignService.request()方法,而service接口层中定义了服务名和服务请求路径,最后通过服务名和请求路径实现对服务提供者的调用。
- Feign的接口在实际开发中经常抽取出一个单独模块,以便复用。
代码参考:https://github.com/Noneplus/JavaDev-Note/tree/master/SpringCloud代码
集成Ribbon的客户端调用工具——Feign的更多相关文章
- Spring Cloud之Feign客户端调用工具
feign介绍 Feign客户端是一个web声明式http远程调用工具,提供了接口和注解方式进行调用. Spring Cloud 支持 RestTemplate Fetin Feign客户端实际开发 ...
- springcloud第七步:fegin客户端调用工具
什么是Feign Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单.使用Feign,只需要创建一个接口并注解.它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解 ...
- 客户端负载均衡Feign之一:申明式服务调用Feign入门示例
Spring Cloud提供了Ribbon和Feign作为客户端的负载均衡. 前面使用了Ribbon做客户端负载均衡,使用Hystrix做容错保护,这两者被作为基础工具类框架被广泛地应用在各个微服务的 ...
- Spring Boot使用Feign客户端调用远程服务时出现:timed-out and no fallback available,failed and no fallback available的问题解决
timed-out and no fallback available: 这个错误基本是出现在Hystrix熔断器,熔断器的作用是判断该服务能不能通,如果通了就不管了,调用在指定时间内超时时,就会通过 ...
- SpringCloud微服务实战二:Spring Cloud Ribbon 负载均衡 + Spring Cloud Feign 声明式调用
1.Spring Cloud Ribbon的作用 Ribbon是Netflix开发的一个负载均衡组件,它在服务体系中起着重要作用,Pivotal将其整合成为Spring Cloud Ribbon,与其 ...
- Springcloud 整合Hystrix 断路器,支持Feign客户端调用
1,在这篇博文中,已经大致说过了Springcloud服务保护框架 Hystrix在服务隔离,服务降级,以及服务熔断中的使用 https://www.cnblogs.com/pickKnow/p/11 ...
- 用JDK自带的工具生成客户端调用Webservice的代码
JAVA下客户端调用Webservice代码简直是让人心生畏惧,今日尝试,做记录如下,参考网上的众多解决方案,下面这种方式是比较简单的. 在jdk的bin目录下有一个wsimport.exe的工具,使 ...
- Feign 客户端调用错误
1.@RequestBody 必须要写在实现接口中 2.Feign 客户端调用的时候如果有参数的话,默认是发送post请求 3.服务接口中的请求参数必须要加上@RequestParam("r ...
- Spring集成CXF发布WebService并在客户端调用
Spring集成CXF发布WebService 1.导入jar包 因为官方下载的包里面有其他版本的sprring包,全导入会产生版本冲突,所以去掉spring的部分,然后在项目根目录下新建了一个CXF ...
随机推荐
- Redis HyperLogLog用法简介
(1)HyperLogLog简介 在Redis 在 2.8.9 版本才添加了 HyperLogLog,HyperLogLog算法是用于基数统计的算法,每个 HyperLogLog 键只需要花费 12 ...
- Python-入门学习
开始学习Python(围绕windows 平台上),记录一些点滴的知识点,也希望可以和大家交流分享. ================================================= ...
- [Linxu] Ubuntu下载mysql
//下载: sudo apt install mysql-server sudo apt install mysql-client sudo apt install libmysqlclient-de ...
- spring配置文件比较全的约束
个人总结:Spring的配置文件applicationContext.xml约束文件.全面约束 <?xml version="1.0" encoding="utf- ...
- Winform 连接Web Service 记录
一般自己控制的项目都会使用webApi,比较少使用WS,感觉要配置一堆东西很繁琐. 场景:多个系统间数据交互. 角色:我们属于下游系统,要把一部分数据格式化后上传到SAP中. SAP提供了一个WS,使 ...
- windows 下搭建安装 sass
众所周知,sass 解析需要有 ruby 的支撑,所以, 第一步:点我下载 ruby: 第二步:安装 ruby: 在安装 ruby 过程中需要注意的一点:把 ruby 执行文件添加到 path,勾选这 ...
- session对象和cookie对象的区别
1.cookie数据存放在客户的浏览器上,session数据放在服务器上2.cookie不是很安全,别人可以分析存放在本地的COOKIE并进行COOKIE欺骗考虑到安全应当使用session3.ses ...
- PHP与ECMAScript_1_变量与常量
PHP ECMAScript 变量命名规则 (相同点) 变量包含:字母.数字.下划线字符 变量只能以字母或下划线开头 变量不能以数字开头 变量名是区分大小写 变量包含:字母.数字.下划线字符 变量只能 ...
- Could not launch "APP_NAME" process launch failed: 4294967295
真机调试忽然遇到这个问题, Could not launch "APP_NAME" process launch failed: 如图所示: 模拟器上能正常调试………… 这个问题还 ...
- go单元测试
testing模块 测试代码放在当前包以_test.go结尾的文件中 测试函数以Test为名称前缀 测试命令(go test) 正常编译操作(go build/install)会忽略测试文件 单例模式 ...