Feign-使用接口方式调用服务
1.简介
feign是声明式的web service客户端,它让微服务之间的调用变得更简单了,类似controller调用service,Spring Cloud集成了Ribbon和Eureka,可在使用Feign时提供负载均衡的http客户端
2.Feign的作用
-
Feign旨在使编写Java Http客户端变得更容易
-
前面在使用Ribbon+RestTemplate时,利用RestTemplate对Http请求的封装处理,形成一套模板化的调用方法。但是在实际开发中,由于对服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用。所以,Feign在此基础上做了进一步封装,由他来帮我们定义和实现依赖服务接口的定义,在Feign的实现下,我们只需要创建一个接口并使用注解的方式来配置它(类似于以前Dao接口上标注Mapper注解,现在是一个微服务接口上面标注一个Feign注解即可)即可完成对服务提供方的接口绑定,简化了使用Spring Cloud Ribbon时,自动封装服务调用客户端的开发量
3.Feign集成了Ribbon
利用Ribbon维护了MicroServiceCloud-Dept的服务列表,并且通过了轮询实现了客户端的负载均衡,而与Ribbon不同的是,通过Feign只需要定义服务绑定接口且以声明式的方法,优雅而且简单的实现了服务调用
4.Feign实战
前提:在上一篇文章负载均衡及Ribbon中的Rbbon实战项目的基础上进行改动
4.1对springcloud-api项目做出的修改
- 添加feign的依赖
<!--feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
- 在com.zixin.springcloud包下创建service文件夹,并在该文件夹中创建FeignDeptService接口
package com.zixin.springcloud.service;
import com.zixin.springcloud.pojo.Dept;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import java.util.List;
@Component
@FeignClient(value = "PROVIDER-DEPT/provider/dept")
public interface FeignDeptService {
//添加部门
@PostMapping("/addDept")
int addDept(Dept dept);
//根据id查询部门信息
@GetMapping("/findDeptById/{deptno}")
Dept findDeptById(Long deptno);
//查询所有部门信息
@GetMapping("/findDeptList")
List<Dept> getDeptList();
}
4.2对consumer-dept-9001项目进行修改
-
新建consumer-dept-9002项目,并把consumer-dept-9001项目的代码复制过来
-
在原来依赖的基础上添加feign的依赖,全部依赖如下:
<dependencies>
<dependency>
<groupId>com.zixin.springcloud</groupId>
<artifactId>springcloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.6.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-ribbon -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-ribbon -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!--feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
</dependencies>
- 修改ConsumerController 中调用接口的方式
package com.zixin.springcloud.controller;
import com.zixin.springcloud.pojo.Dept;
import com.zixin.springcloud.service.FeignDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
public class ConsumerController {
//引入FeignDeptService
@Autowired
private FeignDeptService feignDeptService = null;
@RequestMapping("/consumer/dept/addDept")
public String addDept(@RequestBody Dept dept){
int i = feignDeptService.addDept(dept);
if(i > 0){
return "添加部门信息成功~";
}else{
return "添加部门信息失败~";
}
}
@RequestMapping("/consumer/dept/findDeptById/{deptno}")
public Dept findDeptById(@PathVariable Long deptno){
return feignDeptService.findDeptById(deptno);
}
@RequestMapping("/consumer/dept/findDeptList")
public List<Dept> findDeptList(){
return feignDeptService.getDeptList();
}
}
- 修改启动类为ConsumerFeignApplication,同时添加
@EnableFeignClients
注解,启动类代码如下:
package com.zixin.springcloud;
import com.zixin.myrule.MyRule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
@SpringBootApplication
@EnableEurekaClient //开启eureka客户端
@EnableFeignClients(basePackages = {"com.zixin.springcloud"})
public class ConsumerFeignApplication {
private static final Logger logger = LoggerFactory.getLogger(ConsumerFeignApplication.class);
public static void main(String[] args) {
logger.debug("-------ConsumerFeignApplication正在启动-----");
SpringApplication.run(ConsumerFeignApplication.class, args);
}
}
- 运行eureka-server-7001、provider-dept-8001以及consumer-dept-feign项目,访问
http://localhost:9002/consumer/dept/findDeptList
,得到如下结果,表示以feign的方式调用服务成功