一、Feign简介
Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。本章讲如何通过Feign去消费服务。
简而言之:
- Feign 采用的是基于接口的注解
- Feign 整合了ribbon,具有负载均衡的能力
- 整合了Hystrix,具有熔断的能力
二、准备工作
继续用第一篇的工程, 启动leopard-eureka,端口为8761; 启动leopard-service 两次:
第一次在application.yml原配置的端口为8081直接启动 ;启动成功后,直接修改配置文件端口好为8082,再次启动。这样可以生成两台伪服务器。
三、创建一个feign的服务
新建一个spring-boot工程,取名为leopard-service-feign,在它的pom文件引入Feign的起步依赖spring-cloud-starter-feign、Eureka的客户端起步依赖spring-cloud-starter-eureka、Web的起步依赖spring-boot-starter-web,pom代码如下:
<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>
<parent>
<groupId>com.leopard</groupId>
<artifactId>leopard-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.leopard</groupId>
<artifactId>leopard-service-feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>leopard-service-feign</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- 上边引入 parent,因此 下边无需指定版本 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
在工程的配置文件application.yml文件,指定程序名为leopard-service-feign,端口号为8181,服务注册地址为http://localhost:8761/eureka/ ,配置如下:
spring:
application:
name: leopard-service-feign
server:
port: 8181
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
编写启动类:
package com.leopard.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.netflix.feign.EnableFeignClients;
@EnableDiscoveryClient
@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class FeignServiceApplication {
public static void main(String[] args) {
SpringApplication.run(FeignServiceApplication.class, args);
}
}
注:如果启动类不在根目录,要加上@EnableFeignClients(basePackages = {"com.leopard.console.view.service"}),不然生成的代理对象会变成对应的实现类(容错Hystrix)或是报错。
@EnableFeignClients :此注解可调用其他服务的API
@EnableDiscoveryClient:此注解是将服务注册到服务中心,同@EnableEurekaClient相同,可选。@EnableDiscoveryClient基于spring-cloud-commons, @EnableEurekaClient基于spring-cloud-netflix。此处就两个都采用试用,不影响。
定义一个feign接口,通过@ FeignClient(“服务名”),来指定调用哪个服务。比如在代码中调用了leopard-service服务的“/test/getObject”接口,代码如下:
package com.leopard.service.feign.service;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(value = "leopard-service")
public interface ITestService {
@RequestMapping(value = "/test/getObject", method = RequestMethod.GET)
String getObjectForPort();
}
在controller层,对外暴露一个”/test/getPort”的API接口,通过上面定义的Feign客户端 ITestService来消费服务。代码如下:
package com.leopard.service.feign.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.leopard.service.feign.service.ITestService;
/**
* 测试类
*
* @author leopard
*
*/
@RestController
@RequestMapping(value = "/test")
public class TestController {
@Autowired
public ITestService testService;
@Value("${server.port}")
private String port;
/**
* 获取信息
*
* @return
*/
@RequestMapping(value = "/getObject", method = RequestMethod.GET)
public String getObject() {
return "hi leopard,this port is :" + port;
}
/**
* 获取被调服务端口
*
* @return
*/
@RequestMapping(value = "/getPort", method = RequestMethod.GET)
public String getPort() {
return testService.getObjectForPort();
}
}
前面已经启动好了leopard-eureka和两个leopard-service服务,现在启动lepard-service-feign,
并连续访问 http://localhost:8181/test/getPort , 输出如下:
hi leopard,this port is :8081
hi leopard,this port is :8082
会在8081和8082两个服务之间来回切换,这就是Feign内部实现的负载均衡,轮询式。