十、Feign负载均衡
10.1简介
feign是声明式的web service客户端,它让微服务之间的调用变得更简单了,类似controller调用service。SpringCloud集成了Ribbon和Eureka,可在使用Feign时提供负载均衡的http客户端。
只需要创建一个接口,然后添加注解 即可!
feign,主要是社区,大家都习惯面向接口编程。这个是很多开发人员的规范。调用微服务访问两种方法
- 微服务名字【ribbon】
- 接口和注解【feign】
Feign能干什么
- Feign旨在使编写Java Http客户端变得更容易
- 前面在使用Ribbon + RestTemplate时,利用RestTemplate对Http请求的封装处理,形成了一套模板化的调用方法。但是在实际开发中,由于对服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用。所以,Feign在此基础上做了进一步封装,由他来帮助我们定义和实现依赖服务接口的定义,在Feign的实现下,我们只需要创建一个接口并使用注解的方式来配置它(类似于以前Dao接口上标注Mapper注解,现在是一个微服务接口上面标注一个Feign注解即可。)即可完成对服务提供方的接口绑定,简化了使用Spring Cloud Ribbon时,自动封装服务调用客户端的开发量。
-
Feign集成了Ribbon
- 利用Ribbon维护了MicroServiceCloud-Dept的服务列表信息,并且通过轮询实现了客户端的负载均衡,而与Ribbon不同的是,通过Feign只需要定义服务绑定接口且以声明式的方法,优雅而且简单的实现了服务调用。
10.2.Feign使用步骤
1. 在springcloud-api项目下新建service层,和pojo同级
2. 复制springcloud-consumer-dept-80项目且命名为springcloud-consumer-dept-feign
- 删除MyRule文件夹
- 修改启动类名称为FeignDeptConsumer_80
3.在API项目和feign的项目的pom文件中添加Feign依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
3. 编写刚才新建的DeptClientService 接口
package com.buba.springcloud.service;
import com.buba.springcloud.pojo.Dept;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
@Component
@FeignClient(value = "SPRINGCLOUD-PROVIDER-DEPT")
public interface DeptClientService {
@GetMapping("/dept/get/{id}")
public Dept queryById(@PathVariable("id") Long id);
@GetMapping("/dept/list")
public Dept queryAll();
@PostMapping("/dept/add")
public Dept addDept(Dept dept);
}
4.修改springcloud-consumer-dept-feign中controller层中的DeptConsumerController
package com.buba.springcloud.controller;
import com.buba.springcloud.pojo.Dept;
import com.buba.springcloud.service.DeptClientService;
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.RestController;
import java.util.List;
@RestController
public class DeptConsumerController {
@Autowired
private DeptClientService deptClientService=null;
@RequestMapping("/consumer/dept/get/{id}")
public Dept get(@PathVariable("id") Long id){
return this.deptClientService.queryById(id);
}
@RequestMapping("/consumer/dept/add")
public Boolean add(Dept dept){
return this.deptClientService.addDept(dept);
}
@RequestMapping("/consumer/dept/list")
public List<Dept> list(){
return this.deptClientService.queryAll();
}
}
5.修改springcloud-consumer-dept-feign启动类
package com.buba.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;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages ={"com.buba.springcloud"})
@ComponentScan("com.buba.springcloud")
public class FeignDeptConsumer_80 {
public static void main(String[] args) {
SpringApplication.run(FeignDeptConsumer_80.class);
}
}
注意:这里我使用这个注解的时候报错
不用管,项目可以正常运行,上百度查,说这是因为idea的拼写检查,可以忽视的.
6.启动测试
- 可以看到使用Feign也可以实现负载均衡
- 测试成功!