- Feign的作用:项目中会有很多的服务间的调用,对于服务的调用不可能是一处。所以我们针对各个服务自行封装一些客户端类来包装这些依赖服务的调用,SpringCloud Feign就在这个基础上做了封装,他可以帮助我们定义接口。开发时候只需要定义接口并添加相应的注解,大大的简化了使用ribbon的开发量
- 项目开始搭建,开始由上一篇文章的项目作为基本开发。
- 环境搭建添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</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>
创建项目的时候可以直接由SpringCloud提示中去创建
1、确定

2、点击next

3、修改包名称和相关信息
4、在查询按钮里面直接查找到需要的东西

如上提示
- 现在开始对项目的编辑
- 首先是配置文件的编辑application.properties
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
server.port=7001
spring.application.name=feign-service
如果没有代码提示,说明你有些依赖没有写入,需要去添加,这里本人踩过的许多坑,提示一下
- 启动的FeignApplication类
package com.example.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
@EnableFeignClients
@EnableDiscoveryClient
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
- 新建一个servicepackage,创建ClientService接口
package com.example.feign.Service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "client1")
public interface ClientService {
@RequestMapping(value = "/user/findById",method = RequestMethod.GET)
String findById(@RequestParam("id") String id);
}
`
- 创建一个controller package,创建一个ClientController 类
package com.example.feign.controller;
import com.example.feign.Service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
public class ClientController {
@Autowired
ClientService clientService;
@RequestMapping(value = "/user/findById",method = RequestMethod.GET)
public String findById(@RequestParam("id") String id) {
return clientService.findById(id);
}
}
- 依次运行Eureka,两个client,和刚刚创建的Feigen
- 访问
http://localhost:7001//user/findById?id=wl
- 显示
- 执行着之前的策略,默认是随机的策略
- 开启Feign里面的断路器,作用在于,当访问某个client的时候出了异常,能够及时断开,执行处理操作。
- 配置文件中,修改如下
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
server.port=7001
spring.application.name=feign-service
feign.hystrix.enabled=true
- 创建Service的实现类,记得@Component添加,不加会报错,加载不到这个类,因为没有放进容器里面
package com.example.feign.Service;
import org.springframework.stereotype.Component;
@Component
public class ClientFallback implements ClientService{
@Override
public String findById(String id) {
return "调用Client1服务超时,调用方法findById(id)-根据id查询所有记录"+id;
}
}
- service接口修改如下,添加fallback
package com.example.feign.Service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "client1",fallback = ClientFallback.class)
public interface ClientService {
@GetMapping(value = "/user/findById")
String findById(@RequestParam("id") String id);
}
-接着重启Feign
- 输入
http://localhost:7001//user/findById?id=wl
- 当停掉客户端1时。
、
断路机制,直接显现出来。
附上本次项目的GitHub地址