SpringCloud微服务 之Feign(一)
前言
本小节我们将开始学习一下SpringCloud封装好了的用于实现微服务节点间通信的工具–Feign。
其实关于微服务架构下各个模块(节点)间的通信方式其实有很多种,个人比较推荐使用的是SpringCloud封装好了的RESTful API调用工具,比如说RestTemplate(在之前的案例中有实用参考:SpringCloud微服务 之Ribbon(一))和Feign。
其中个人比较喜欢的是使用Feign,因为Feign是SpringCloud对RestTemplate的高度封装,同时还集成更多的功能比如Hystrix等优秀工具及其拓展功能,Feign是一个非常不错的用于实现和保证微服务模块间通信的工具。下面我们先来简单了解一下Feign。
- Feign是一个声明式的Web服务客户端,是Netflix下的一个用于实现分布式客服端间通信的工具,SpringCloud
为服务架构下,SpringCloud对Feign做了进一步封装,是的Feign的功能能为强大。 - Feign 具有可插入注释支持,包括Feign注释和JAX-RS注释。Feign还支持可插拔编码器和解码器。
- Spring Cloud在对Feign的封装过程中增加了对Spring MVC注释的支持,并使用Spring Web中默认使用的HttpMessageConverters,是的Feign能与Spring生态基本无缝集成。
- Spring Cloud集成Ribbon和Eureka以在使用Feign时提供负载均衡的http客户端。
- …
案例
为了更直观地体验Feign的强大,我们将通过一些案例来学习Feign。
- Eureka Server端编写:(参考前例)。
Eureka Client端服务提供方编写(参考前例)。
- 项目结构
CoreCode
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients //开启FeignClient注解
public class MicroserviceDealBrokerFeignApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceDealBrokerFeignApplication.class,
args);
}
}
@FeignClient(name = "microservice-deal-cloud") //声明服务节点
public interface FeignConfig {
//第一个坑:使用Feign的时候,如果参数中带有@PathVariable形式的参数,则要用value=""标明对应的参数,否则会抛出IllegalStateException异常
@GetMapping("/deal/{id}")
public Deal findById(@PathVariable(value="id") Long id);
}
@RestController
public class BrokerController {
@Autowired
private FeignConfig feignConfig;
@GetMapping("/deal/{id}")
public Deal findById(@PathVariable(value="id") Long id) {
return this.feignConfig.findById(id);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<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>
<artifactId>microservice-deal-broker-cloud-feign</artifactId>
<packaging>jar</packaging>
<name>microservice-deal-broker-cloud-feign</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.example</groupId>
<artifactId>microservice-deal-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<!-- Eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- Feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
- 项目结构
</dependencies>
</project>
- Eureka Client端服务提消费编写(单实例) (参考前例)。
- 访问:http://localhost:8082/deal/2
小结
Feign虽好用可实际上应与起来会有表较多的坑…
- 使用Feign的时候,如果参数中带有@PathVariable形式的参数,则要用value=””标明对应的参数,否则会抛出IllegalStateException异常。
- 在配置文件中必须做一些声明(参考以下说明)。
服务消费者若要使用Feign作为声明式客户端至少需要做以下三步(以本节案例为例子):
- 开启@EnableFeignClients注解,用于声明该服务节点是一个FeignClient
- 编写FeignClient接口用于声明客户端URLs。
- 在配置文件中声明开启一些特性,比如:ribbon
enabled: true(开启Eureka中Ribbon均衡负载功能)和hystrix
default
isolation
timeoutInMilliseconds: 5000(配置断熔器超时时间)。
- 本节用到的案例:microservice-deal-eureka、microservice-deal-cloud、microservice-deal-broker-cloud-feign
还没有评论,来说两句吧...