史上最简单的SpringCloud教程 | 第三篇: 服务消费者(Feign)(Finchley版本)
转载请标明出处:
原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f3-feign/
本文出自方志朋的博客
上一篇文章,讲述了如何通过RestTemplate+Ribbon去消费服务,这篇文章主要讲述如何通过Feign去消费服务。
一、Feign简介
Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。
简而言之:
- Feign 采用的是基于接口的注解
- Feign 整合了ribbon,具有负载均衡的能力
- 整合了Hystrix,具有熔断的能力
二、准备工作
继续用上一节的工程, 启动eureka-server,端口为8761; 启动service-hi 两次,端口分别为8762 、8773.
三、创建一个feign的服务
新建一个spring-boot工程,取名为serice-feign,在它的pom文件引入Feign的起步依赖spring-cloud-starter-feign、Eureka的起步依赖spring-cloud-starter-netflix-eureka-client、Web的起步依赖spring-boot-starter-web,代码如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.forezp</groupId>
<artifactId>service-feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>service-feign</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.forezp</groupId>
<artifactId>sc-f-chapter3</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<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>
</project>
在工程的配置文件application.yml文件,指定程序名为service-feign,端口号为8765,服务注册地址为http://localhost:8761/eureka/ ,代码如下:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8765
spring:
application:
name: service-feign
在程序的启动类ServiceFeignApplication ,加上@EnableFeignClients注解开启Feign的功能:
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceFeignApplication {
public static void main(String[] args) {
SpringApplication.run( ServiceFeignApplication.class, args );
}
}
定义一个feign接口,通过@ FeignClient(“服务名”),来指定调用哪个服务。比如在代码中调用了service-hi服务的“/hi”接口,代码如下:
@FeignClient(value = "service-hi")
public interface SchedualServiceHi {
@RequestMapping(value = "/hi",method = RequestMethod.GET)
String sayHiFromClientOne(@RequestParam(value = "name") String name);
}
在Web层的controller层,对外暴露一个"/hi"的API接口,通过上面定义的Feign客户端SchedualServiceHi 来消费服务。代码如下:
@RestController
public class HiController {
//编译器报错,无视。 因为这个Bean是在程序启动的时候注入的,编译器感知不到,所以报错。
@Autowired
SchedualServiceHi schedualServiceHi;
@GetMapping(value = "/hi")
public String sayHi(@RequestParam String name) {
return schedualServiceHi.sayHiFromClientOne( name );
}
}
启动程序,多次访问http://localhost:8765/hi?name=forezp,浏览器交替显示:
hi forezp,i am from port:8762
hi forezp,i am from port:8763
Feign源码解析:http://blog.csdn.net/forezp/article/details/73480304
本文源码下载:
https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-chapter3
五、参考资料
本文参考了以下:
http://blog.csdn.net/forezp/article/details/69808079
http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html
扫码关注公众号有惊喜
(转载本站文章请注明作者和出处 方志朋的博客)
史上最简单的SpringCloud教程 | 第三篇: 服务消费者(Feign)(Finchley版本)的更多相关文章
- 史上最简单的SpringCloud教程 | 第三篇: 服务消费者(Feign)
转载请标明出处: https://www.fangzhipeng.com/springcloud/2017/07/12/sc03-feign/ 本文出自方志朋的博客 最新Finchley版本请访问: ...
- 史上最简单的SpringCloud教程 | 第五篇: 路由网关(zuul)(Finchley版本)
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f5-zuul/ 本文出自方志朋的博客 在微服务架构中,需要几 ...
- 史上最简单的SpringCloud教程 | 第十篇: 高可用的服务注册中心(Finchley版本)
转载请标明出处: 原文首发于 https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f10-eureka/ 本文出自方志朋的博客 文章 史上最简单 ...
- 史上最简单的SpringCloud教程 | 第十三篇: 断路器聚合监控(Hystrix Turbine)(Finchley版本)
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f13-turbine/ 本文出自方志朋的博客 上一篇文章讲述 ...
- 史上最简单的SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2017/07/12/sc08-bus/ 本文出自方志朋的博客 最新Finchley版本请 ...
- SpringCloud教程 | 第三篇: 服务消费者(Feign)
上一篇文章,讲述了如何通过RestTemplate+Ribbon去消费服务,这篇文章主要讲述如何通过Feign去消费服务.一.Feign简介 Feign是一个声明式的伪Http客户端,它使得写Http ...
- SpringCloud教程 | 第三篇: 服务消费者(Feign)(Finchley版本)
上一篇文章,讲述了如何通过RestTemplate+Ribbon去消费服务,这篇文章主要讲述如何通过Feign去消费服务. 一.Feign简介 Feign是一个声明式的伪Http客户端,它使得写Htt ...
- 史上最简单的SpringCloud教程 | 第十一篇: docker部署spring cloud项目
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2017/07/12/sc11-docker/ 本文出自方志朋的博客 一.docker简介 ...
- 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)
一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件. 在Spring Cloud中,有分布式配置中心组件spring cloud confi ...
随机推荐
- [转]ASP.NET MVC 5 List Editor with Bootstrap Modals
本文转自:https://www.codeproject.com/articles/786085/asp-net-mvc-list-editor-with-bootstrap-modals With ...
- 文档碎片DocumentFragment
文档碎片是什么? 参考标准的描述,DocumentFragment是一个轻量级的文档对象,能够提取部分文档的树或创建一个新的文档片段,换句话说有文档缓存的作用. createDocumentFragm ...
- node.js发邮件
在node上使用第三方类库(nodemailer)发邮件是一件很esay的事情:) app.js 以QQ邮箱为例 var nodemailer = require('nodemailer'); v ...
- C# ObjectArx cad二次开发
1 环境搭建:安装CAD 和objectArx库,这里安装的是cad2012和objectArx2010 ,vs是2010 2 新建一个类库项目,引用objectArx安装目录下inc文件夹下的AcD ...
- js异步流程控制-回调
f1为耗时操作,f2依赖f1的数据,因此f2必须在f1之后执行: 个人理解是:将f2(回调函数)的代码放在异步函数内部的最后执行,相当于把同步操作的代码融合到异步函数内部的最后: let tag = ...
- LotusScript_文档查询循环方法整理
1. 视图(View)查询 ... Set view = db.GetView("ViewName") Set doc = view.GetFirstDocument While ...
- 设计模式之装饰模式(Decorator)
1. 装饰者模式,动态地将责任附加到对象上.若要扩展功能,装饰者提供了比继承更加有弹性的替代方案. 2.组合和继承的区别 继承.继承是给一个类添加行为的比较有效的途径.通过使用继承,可以使得子类在拥有 ...
- redis缓存技术在java中的应用
https://blog.csdn.net/zhh1072773034/article/details/78538012
- 使用WindowsService为宿主实装WCF 服务
1. 写WCF服务 创建一个接口类,与一个实现类.接口类上打上[ServiceContract]标签,需要暴露的服务方法上打上[OperationContract] (注意:增加System.Se ...
- WAKE-WIN10-SOFT-CMAKE
1,CMAKE 官网:https://cmake.org/ 下载:https://cmake.org/download/ BING:https://www.bing.com/search?q=cmak ...