Feign
目录
1. Feign概述
feign是声明式的web service客户端,它让微服务之间的调用变得更简单了,类似controller调用service。Spring Cloud集成了Ribbon和Eureka,可在使用Feign时提供负载均衡的http客户端
Feign本质上是对Ribbon做了集成封装,相当于加了一层,更加符合开发者面向接口和注解的编程习惯,免去了RestTemplate的配置,这样同时不可避免的带来了性能的损失
2. 使用Feign做负载均衡
1. 创建Feign客户端
在API中提供Feign的接口
1. 导入依赖
<!--Feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
2. 创建Feign接口
package com.wang.springcloud.service;
import com.wang.springcloud.pojo.Dept;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import java.util.List;
//value为微服务的名称
@FeignClient(value = "SPRINGCLOUD-PROVIDER-DEPT")
@Service
public interface DeptClientService {
@GetMapping("/dept/get/{id}")
Dept queryById(@PathVariable("id") Long id);
@GetMapping("/dept/list")
List<Dept> queryAll();
@PostMapping("/dept/add")
Boolean addDept(Dept dept);
}
注意
- @FeignClient 声明该接口为Feign的客户端,value指定了用于负载均衡的服务名
- 接口中的方法的url对应provider中提供的url,本质上与Ribbon一样,只是这里省略了繁琐的RestTemplate配置,通过注解直接写在此处
2. 调用Feign
1. 导入依赖
我们首先依据SpringCloud-consumer-dept-8080创建了利用Feign做负载均衡的Module
同样的,依赖只需要将Ribbon替换为Feign
<!--Feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
2. 在controller中调用Feign
package com.wang.springcloud.controller;
import com.wang.springcloud.pojo.Dept;
import com.wang.springcloud.service.DeptClientService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@ApiModel("Consumer Controller")
@RestController
public class DeptConsumerController {
@Autowired
private DeptClientService service = null;
@ApiOperation("通过部门编号查询一个部门")
@RequestMapping(value = "/consumer/dept/get/{id}",method = RequestMethod.GET)
public Dept get(@PathVariable("id") @ApiParam("部门编号") Long id) {
return this.service.queryById(id);
}
@ApiOperation("通过部门名称添加一个部门")
@RequestMapping(value = "/consumer/dept/add",method = RequestMethod.POST)
public boolean add(@ApiParam("部门的名称") Dept dept){
return this.service.addDept(dept);
}
@ApiOperation("查询全部的部门")
@RequestMapping(value = "consumer/dept/list",method = RequestMethod.GET)
public List<Dept> list() {
return this.service.queryAll();
}
}
注意
3. 在主启动类声明
package com.wang.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
//Ribbon 和 Eureka 整合以后,客户端可以直接调用,不用关心IP地址和端口号
@SpringBootApplication
@EnableEurekaClient
//开启Feign客户端支持
@EnableFeignClients
public class FeignDeptConsumer_8080 {
public static void main(String[] args) {
SpringApplication.run(FeignDeptConsumer_8080.class,args);
}
}
3. 关于Feign负载均衡策略的一些思考
Feign由于封装了Ribbon,只是更加贴近JAVA的面向接口和注解编程的习惯,因此,自定义和更改负载均衡策略与Ribbon一致,依然是注册IRule接口
当自定义策略与SpringBoot主启动类不在同一级时,使用@RibbonClient注解中的configuration属性指定配置类
package com.wang.springcloud.config;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ConfigBean {
@Bean
public IRule myRule() {
return new RandomRule();
}
}