环境:
jdk1.8;spring boot2.0.3;spring cloud(Finchley.RELEASE版本);Maven3.3
摘要说明:
Spring Cloud Feign:Spring Cloud Feign是一套基于Netflix Feign实现的声明式服务调用客户端。它使得编写Web服务客户端变得更加简单。通过创建接口并用注解来配置它既可完成对Web服务接口的绑定。它具备可插拔的注解支持,包括Feign注解、JAX-RS注解。它也支持可插拔的编码器和解码器。Spring Cloud Feign还扩展了对Spring MVC注解的支持,同时还整合了Ribbon和Eureka来提供均衡负载的HTTP客户端实现。
总之,Feign是基于Ribbon实现的Feign使得接口更加规范化,更有阅读性并且更加简单化;
步骤:
1.创建Feign(Feign的服务消费者)子项目
通过SPRING INITIALIZR
工具选择Cloud Routing:Feign模块构建feign子项目引入依赖(pom.xnl):
<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>pers.cc</groupId>
<artifactId>springCloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>feign</artifactId>
<name>feign</name>
<description>Feign消费者</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</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-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
2.配置Feign(Feign的服务消费者)子项目
使用@EnableDiscoveryClient和@EnableFeignClients注解开启feign的服务消费者配置;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
配置application.properties
#配置服务及端口
spring.application.name=feign-consumer
server.port=4001
#注册到注册中心
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
3.开发Feign接口
使用Feign与Ribbon不同的是需要调用前实现规范化接口,使用@FeignClient和Spring Mvc注解来绑定服务提供方的REST接口
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
//根据服务名称来指定服务提供方
@FeignClient("eureka-client")
public interface TestService {
// 通过注解指定访问方式,访问路径,访问参数
@RequestMapping(method = RequestMethod.GET, value = "/add")
Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b);
}
然后就是实现controller来调用接口:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import pers.cc.springCloud.feign.test.service.TestService;
@RestController
public class TestController {
@Autowired
TestService testService;
@GetMapping("/test")
public Integer test() {
return testService.add(12, 6);
}
}
4.调用Feign
先后启动:
eurekaServer
服务提供者(eurekaDiscovery):eureka-client(2001)
服务提供者(eurekaDiscovery):eureka-client(2002)
服务消费者(feign):feign-consumer(4001)
多次输入http://localhost:4001/test;查看eurekaDiscovery两个服务提供者都会出现调用达到负载均衡的作用;