1. 简介
Feign is a declarative web service client. It makes writing web service clients easier. To use Feign create an interface and annotate it. It has pluggable annotation support including Feign annotations and JAX-RS annotations. Feign also supports pluggable encoders and decoders. Spring Cloud adds support for Spring MVC annotations and for using the same HttpMessageConverters used by default in Spring Web. Spring Cloud integrates Ribbon and Eureka to provide a load balanced http client when using Feign.
- Feign 是一个声明式调用HTTP协议服务的客户端
- 支持注解形式发现与调用服务
- 支持编码与解码
- 与Ribbon、Eureka结合使用可实现负载均衡功能
2. 代码实现
2.1涉及的模块
- eureka-server-singleton:服务注册中心,端口8761
- eureka-service: 服务提供者,通过profile指定不同端口模拟一组微服务,端口8762、8763
- eureka-service-feign:通过Feign调用服务提供者提供的服务
2.2 源代码
2.2.1 Github地址
https://github.com/andyChenHuaYing/spring-cloud-demo
2.2.2 切换
通过tag切换git tag -d v1.0,若想修改,可根据此tag创建新的分支。
2.3 eureka-server-singleton
与Spring Cloud 之服务发现与调用-Ribbon#2.3 eureka-server-singleton 没有任何区别
2.4 eureka-service
2.5 eureka-service-feign
2.5.1整体实现步骤
- pom.xml中引入
spring-cloud-starter-netflix-eureka-server
和spring-cloud-starter-openfeign
依赖 - application.yml中指定配置项,端口、application.name、eureka-server地址
- SpringBoot 启动类添加启用Fegin的注解
@EnableFeignClients
- 在当前项目Service接口类名上使用FeignClient注解指定服务提供者的服务名
@FeignClient("eureka-service")
- 在需要调用服务提供者的接口方法之上使用注解指定调用服务提供者提供的方法
@RequestMapping("/print")
- 在需要使用服务提供者提供的方法之处,使用第4步与第5步实例化好的服务提供者的方法
2.5.2 pom.xml
<?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">
<parent>
<artifactId>spring-cloud-finchley-demo</artifactId>
<groupId>org.oscar.scd</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka-service-feign</artifactId>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
</dependencies>
</project>
2.5.2 application.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8765
spring:
application:
name: eureka-serivce-feign
2.5.3 EurekaServiceFeignApplication
package org.oscar.scd.eureka.service.feign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients
@EnableEurekaClient
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaServiceFeignApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServiceFeignApplication.class, args);
}
}
2.5.4 FeignHelloController、FeignHelloService
- FeignHelloService:
package org.oscar.scd.eureka.service.feign.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient("eureka-service")
public interface FeignHelloService {
@RequestMapping("/print")
String printPort(@RequestParam("name") String name);
}
- FeignHelloController
package org.oscar.scd.eureka.service.feign.controller;
import org.oscar.scd.eureka.service.feign.service.FeignHelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/feign")
public class FeignHelloController {
@Autowired
private FeignHelloService service;
@RequestMapping("/print")
public String print(@RequestParam("name") String name) {
return this.service.printPort(name);
}
}
3. 验证
3.1 创建SpringBoot启动类
3.1.1 EurekaServerSingletonApplication
与四:Spring Cloud 之服务发现与调用-Ribbon#3.1.1 EurekaServerSingletonApplication 完全一致
3.1.2 EurekaServiceApplication-8762
与四:Spring Cloud 之服务发现与调用-Ribbon#3.1.2 EurekaServiceApplication-8762 完全一致
3.1.3 EurekaServiceApplication-8763
与四:Spring Cloud 之服务发现与调用-Ribbon#3.1.3 EurekaServiceApplication-8763完全一致
3.1.4 EurekaServiceFeignApplication
最简单的方式添加一个SpringBoot启动类型的启动类就行。
3.2 启动
依次启动EurekaServerSingletonApplication
、EurekaServiceApplication-8762
、EurekaServiceApplication-8763
、EurekaServiceFeignApplication
,启动完成后Run Dashboard界面:
3.3 访问服务信息界面
可直接点击上图中Run Dashboard中的8761端口直接访问,也可访问http://localhost:8761/查看,可看到三个eureka服务都注册到注册中心了。
3.4 调用服务
访问eureka-service-ribbon 提供的服务:http://localhost:8764/ribbon/print?name=oscar,多请求两次,返回结果在一下两种中切换,说明负载均衡起作用了。
- 第一次:
- 第二次
4. 思考
- 如何与Ribbon集成的
- 如何指定负载均衡算法
- 默认负载均衡算法是如何的
- 内部使用HttpClient访问的服务提供者提供的REST接口吗
- 如何根据注解以及本地接口定义实例化调用远程接口的代理实例的
- 一次完整的调用过程内部需要经过哪些步骤
- 数据如何序列化与反序列化的
5. 补充
5.1 资料
http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi_spring-cloud-feign.html