Feign主要作用:自动根据参数拼接http请求地址。
将原先 controller 中写死的调用服务地址 ,拆分单独写client客户端,调用时直接注入。
服务发现(consumer-demo)
添加spring-cloud-starter-openfeign坐标
导入坐标(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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud_parent</artifactId>
<groupId>li.chen.com</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>consumer_demo</artifactId>
<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-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
</project>
配置文件
- application.yml
spring:
application:
name: consumer_demo
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
# 获取服务地址列表间隔时间,默认30秒
registry-fetch-interval-seconds: 10
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 2000 # 延迟?
circuitBreaker:
errorThresholdPercentage: 50 # 触发熔断错误比例阈值,默认值50%
sleepWindowInMilliseconds: 10000 # 熔断后休眠时长,默认值5秒
requestVolumeThreshold: 10 # 熔断触发最小请求次数,默认值是20
springboot启动类
- ConsumerApplication
package li.chen.com.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@EnableDiscoveryClient //开启Eureka客户端发现功能
@SpringBootApplication
@EnableFeignClients //开启Feign功能
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class,args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
各层文件
pojo层
- User
package li.chen.com.consumer.pojo;
import lombok.Data;
import java.util.Date;
/**
* @ClassName: User
* @Description 接收数据,不进行数据库交互,故不需要加user_service里的那些注解
**/
@Data
public class User {
private Integer id;
private String userName;
private String passWord;
private Date birthday;
}
client层
package li.chen.com.consumer.client;
import li.chen.com.consumer.pojo.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
//声明当前类是一个Feign客户端,指定服务名为UserService
@FeignClient("UserService")
public interface UserClient {
// http://UserService/1 拼接的路径--》是UserService服务提供的路径
@GetMapping("/UserController/{id}")
User queryById(@PathVariable Long id);
}
controller层
- ConsumerFeignController
package li.chen.com.consumer.controller;
import li.chen.com.consumer.client.UserClient;
import li.chen.com.consumer.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;
@RestController
@RequestMapping("ConsumerFeignController")
public class ConsumerFeignController {
@Autowired
private UserClient userClient;
@GetMapping("/{id}")
public User queryById(@PathVariable Long id){
return userClient.queryById(id);
}
}
测试
启动UserApplication9090
启动 EurekaServerApplication
启动 ConsumerApplication
访问 http://localhost:8080/ConsumerFeignController/2

本文介绍了如何利用Feign实现服务发现和自动拼接HTTP请求地址,将原本硬编码在Controller中的服务调用地址转移到独立的Client客户端,简化代码并提高可维护性。通过在consumer-demo项目中引入spring-cloud-starter-openfeign坐标,配置application.yml,创建启动类及各层文件,包括pojo、client和controller,最后进行测试验证。
4017





