Feign
Feign可以把Rest的请求进行隐藏,伪装成类似SpringMVC的Controller一样。你不用再自己拼接url,拼接参数等等操作,一切都交给Feign去做。
使用Feign
基于SpringCloud Eureka注册中心 负载均衡Ribbon进行扩展,我们重新编写服务调用方,使用Feign
导入依赖
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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sunyuqi</groupId>
<artifactId>feign-consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Eureka客户端 -->
<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-netflix-hystrix</artifactId>
</dependency>
<!--Feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<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>
</project>
项目结构
配置文件
server:
port: 8085
spring:
application:
name: service-consumer-feign #注册的名字
eureka:
client:
service-url:
defaultZone: http://localhost:10000/eureka
registry-fetch-interval-seconds: 5 #间隔多久拉取服务,默认为30s
feign:
hystrix:
enabled: true # 开启Feign的熔断功能
开启Feign功能
我们在启动类上,添加注解,开启Feign功能
package com.sunyuqi;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringCloudApplication
@EnableFeignClients // 开启feign客户端
public class feignApplication {
public static void main(String[] args)
{
SpringApplication.run(feignApplication.class, args);
}
}
feign已经自动集成了Ribbon负载均衡的RestTemplate。
Feign的客户端接口
package com.sunyuqi.client;
import com.sunyuqi.domain.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(value = "service-provider", fallback = UserClientFallback.class) // 标注该类是一个feign接口
public interface UserClient {
@GetMapping("/findone/{id}")
public User queryUser(@PathVariable("id") Long id);
}
- Feign会通过动态代理,帮我们生成实现类。
@FeignClient
,声明这是一个Feign客户端,类似@Mapper
注解。同时通过value
属性指定服务名称- 接口中的定义方法,完全采用SpringMVC的注解,Feign会根据注解帮我们生成URL,并访问获取结果
- fallback 指定熔断类
熔断类
package com.sunyuqi.client;
import com.sunyuqi.domain.User;
import org.springframework.stereotype.Component;
@Component
public class UserClientFallback implements UserClient {
@Override
public User queryUser(Long id) {
User user = new User();
user.setId(-1l);
user.setName("服务器正忙,请稍后再试!!!!!!!");
return user;
}
}
controller
package com.sunyuqi.controller;
import com.sunyuqi.client.UserClient;
import com.sunyuqi.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("consumer")
public class FeignController {
@Autowired
private UserClient userClient;
@GetMapping("/findone/{id}")
@ResponseBody
public User findone(@PathVariable("id") Long id)
{
User user = userClient.queryUser(id);
if (user==null)
{
user = new User();
}
return user;
}
}
访问服务
关闭服务提供方 ,再次访问