1、Feign简介
Feign是一个声明式的web客户端,封装Htttp请求的整个过程,构造请求、建立连接、解析响应结果等,并且整合了Ribbon来实现负载均衡及Hystrix实现断路器(下一篇文章会讲到),Feing有两个主要注解: @EnableFeignClients 用于开启feign功能,@FeignClient 用于定义feign 接口。
2、创建一个Feign工程
引入核心依赖:spring-cloud-starter-netflix-eureka-client、spring-cloud-starter-openfeign
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>service-feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>service-feign</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
application.yml
server:
port: 8003
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8000/eureka
Spring:
application:
name: service-feigin
3、启动类加上必要的注解,@EnableDiscoveryClient开启Ribbon,@EnableFeignClients开启Feign
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceFeignApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceFeignApplication.class, args);
}
}
4、创建一个Feign服务,@FeignClient声明你要调用的实例名称,@RequestMapping就是这个实例下的具体方法,这里就是说要去调用eureka-client1实例下的/serviceTest/test方法,也就是我们本系列第一章中eureka-client1工程下声明的方法
/**
* @author XuJD
* @create 2019-11-26 11:13
**/
@FeignClient(value = "eureka-client1")
@Service
public interface FeignService {
@RequestMapping(value = "/serviceTest/test")
String test();
}
5、定义一个控制类,对外提供接口测试
/**
* @author XuJD
* @create 2019-11-26 11:13
**/
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private FeignService feignService;
@RequestMapping("/hello")
public String test(){
return feignService.test();
}
}
6、测试
访问 :http://localhost:8003/test/hello,可以看到8001和8002在循环打印
Hello world,this is eureka-client1--8001
Hello world,this is eureka-client1--8002
代码地址:https://github.com/xujiangdong/SpringCloud_Study
都看到这里了,点个star支持下吧,万分感谢!