简介
Feign是一个声明式的伪Http客户端,它使得实现一个Http客户端变得简单。Feign整合了Ribbon,可以与Eureka结合,实现负载均衡的效果。
特点
- 采用注解的方式
- 整合Ribbon,具有负载均衡能力
创建一个Feign应用
- 引入
spring-cloud-starter-openfeign
、spring-cloud-starter-netflix-eureka-client
依赖。
<?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.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.alex</groupId>
<artifactId>feign_consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>feign_consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR1</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-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>
</project>
- 修改启动类,添加
@EnableEurekaClient
、@EnableDiscoveryClient
、@EnableFeignClients
注释。
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class FeignConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(FeignConsumerApplication.class, args);
}
}
- 在
application.yml
中添加配置:
server:
port: 9003
spring:
application:
name: feign-consumer
eureka:
client:
service-url:
defaultZone: http://localhost:9000/eureka //Eureka服务器地址
- 新建服务映射接口,通过@ FeignClient(“服务名”),来指定调用哪个服务
@FeignClient(value = "hello-server-provider") //填入服务名称
public interface HelloServiceMappingFeign {
@RequestMapping(value = "/hi",method = RequestMethod.GET) //服务的“/hi”接口
String sayHiMapping();
}
- 新建controller,将接口注入使用。
@RestController
public class HelloControllerMapping {
@Autowired
HelloServiceMappingFeign helloServiceMappingFeign; //注入HelloServiceMappingFeign
@GetMapping("/hiMapping")
public String sayHi(){
return helloServiceMappingFeign.sayHiMapping();
}
}
- 结果