- 一. Feign简介
- 二.Feign实战开发
- 1.在tensquare_qa模块添加依赖
- 2.在主启动类中,添加注解
- 3. 编写接口,进行调用
- 4. 在Controller层调用接口
- 三. 启动项目,进行测试
Feign是简化Java HTTP客户端开发的工具(java-to-httpclient-binder),它的灵感 来自于Retrofit、JAXRS-2.0和WebSocket。Feign的初衷是降低统一绑定Denominator到 HTTP API的复杂度,不区分是否为restful。
二.Feign实战开发目前的需求是在问答微服务(tensquare_qa)调用基础微服务(tensquare_base)的方法(根据ID查询标签) 谁调用谁, 调用的一方需要添加Feign的依赖,而被调用的一方,是不需要变动的. 即需要在tensquare_qa中,添加Feign的依赖,而tensquare_base中,是不需要改变的.
1.在tensquare_qa模块添加依赖
org.springframework.cloud
spring-cloud-starter-openfeign
2.在主启动类中,添加注解
添加@EnableDiscoveryClient
注解,用于发现服务 @EnableFeignClients
注解,用于采取Feign的方式发现服务 完整的代码如下
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import util.IdWorker;
import util.JwtUtil;
@SpringBootApplication
@EnableEurekaClient
//发现服务
@EnableDiscoveryClient
//采取Feign的方式发现服务
@EnableFeignClients
public class QaApplication {
public static void main(String[] args) {
SpringApplication.run(QaApplication.class, args);
}
@Bean
public IdWorker idWorkker(){
return new IdWorker(1, 1);
}
/**
* 把jwt对象注入容器中
* @return
*/
@Bean
public JwtUtil jwtUtil(){
return new JwtUtil();
}
}
3. 编写接口,进行调用
在com.tensquare.qa.client包下,编写如下的接口,进行模块间的调用 其中@FeignClient("tensquare-base")
,该注解的值,指定了调用tensquare-base模块.该模块的名称,与其yml中, spring.application.name
的名称要对应上,并且不能有下划线,否则报错.
@RequestMapping
注解用于对被调用的微服务进行地址映射。注意 @PathVariable注 解一定要指定参数名称,该名称要@RequestMapping
注解中的参数要与路径一致.否则出错
package com.tensquare.qa.client;
import entity.Result;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* 类名称:BaseClient
* 类描述: 模块调用的接口
*
* 创建时间:2019/2/17 10:26
* Version 1.0
*/
@FeignClient("tensquare-base")
public interface BaseClient {
/**
* 方法名: findById
* 方法描述: 根据id查询标签
* 修改日期: 2019/1/6 15:20
* @param labelId
* @return entity.Result
* @author taohongchao
* @throws
*/
@RequestMapping(value = "/label/{labelId}",method = RequestMethod.GET)
public Result findById(@PathVariable("labelId") String labelId);
}
4. 在Controller层调用接口
在tensquare_qa模块的 ProblemController,编写如下的方法,进行接口的调用 同时把BaseClient
,进行注入,来进行模块间的调用.
@Autowired
private BaseClient baseClient;
/**
* 方法名: findById
* 方法描述: 根据id查询标签
* 修改日期: 2019/1/6 15:20
* @param labelId
* @return entity.Result
* @author taohongchao
* @throws
*/
@RequestMapping(value = "/label/{labelId}",method = RequestMethod.GET)
public Result findByLableId(@PathVariable("labelId") String labelId){
Result result = baseClient.findById(labelId);
return result;
};
三. 启动项目,进行测试
依次启动tensquare_eureka,tensquare_base,tensquare_qa这三个项目. 在数据库中,tb_label表中,有如下的数据. 可以任意选择一个进行测试. 例如选择id为1的label进行测试. 发送的get请求如下
http://localhost:9003/problem/label/1
响应的数据如下.代表成功实现了服务间的调用. 因为9003端口是tensquare_qa 问答项目的,而获取到的数据是tensquare_base基础模块的,能够获取到了数据,代表实现了服务间的调用.