Spring|Spring Cloud Alibaba 使用Feign进行服务消费
为什么使用Feign?
Feign可以把Rest的请求进行隐藏,伪装成类似SpringMVC的Controller一样。你不用再自己拼接url,拼接参数等等操作,一切都交给Feign去做。
使用Feign进行消费
【Spring|Spring Cloud Alibaba 使用Feign进行服务消费】将需要使用feign的工程增加一下依赖
pom.xml
org.springframework.cloud
spring-cloud-starter-openfeign
启动类增加启用feign注解EnableFeignClients
UserServiceApplication.java
/**
* 用户中心服务启动类
*
* @author wentao.wu
*/
@EnableDiscoveryClient//服务注册
@EnableFeignClients//服务发现
@SpringBootApplication
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}
创建feign调用接口
/**
* service-member服务远程调用接口
* @author wentao.wu
*/
@FeignClient(name = "service-member")
public interface MemberInfoControllerClient {
/**
* 通过用户名称获取会员信息
* @param username
* @return
*/
@GetMapping("/member/info/getUserMember/{username}")
public Response
创建请求进行feign消费
FeignConsumerController.java
import com.gitee.eample.user.service.feign.MemberInfoControllerClient;
import com.gtiee.example.common.exception.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
* Feign template consumer
*
* @author wentao.wu
*/
@RestController
@RequestMapping("/feign/consumer")
public class FeignConsumerController {
@Autowired
private MemberInfoControllerClient client;
@GetMapping("/getUserInfo/{username}")
public Response
以上代码通过Feign生命消费者接口方法,Feign将自动生成代理方法进行远程调用。访问请求http://localhost:8080/feign/consumer/getUserInfo/zhangsan 进行消费返回结果为
{
"code": "1",
"msg": "获取用户信息成功!",
"errorCode": null,
"errorMsg": null,
"result": {
"level": "vip1",
"username": "zhangsan",
"userage": "100",
"email": "xxx@email.com"
}
}
源码代码存放地址 gitee: https://gitee.com/SimpleWu/spring-cloud-alibaba-example.git
cnblogs: https://www.cnblogs.com/SimpleWu
持续更新目录:https://www.cnblogs.com/SimpleWu/p/15476427.html
推荐阅读
- Activiti(一)SpringBoot2集成Activiti6
- SpringBoot调用公共模块的自定义注解失效的解决
- 解决SpringBoot引用别的模块无法注入的问题
- 2018-07-09|2018-07-09 Spring 的DBCP,c3p0
- spring|spring boot项目启动websocket
- Spring|Spring Boot 整合 Activiti6.0.0
- Spring集成|Spring集成 Mina
- springboot使用redis缓存
- Spring|Spring 框架之 AOP 原理剖析已经出炉!!!预定的童鞋可以识别下发二维码去看了
- Spring|Spring Boot之ImportSelector