Spring Cloud Alibaba 使用Feign进行服务消费
为什么使用Feign?
Feign可以把Rest的请求进行隐藏,伪装成类似SpringMVC的Controller一样。你不用再自己拼接url,拼接参数等等操作,一切都交给Feign去做。
使用Feign进行消费
将需要使用feign的工程增加一下依赖
pom.xml
<!-- openfeign 服务发现调用 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
启动类增加启用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<Map<String, Object>> getUserMember(@PathVariable("username") String username);
}
创建请求进行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<Map<String, Object>> getUserInfo(@PathVariable("username") String username) {
Response<Map<String, Object>> response = client.getUserMember(username);
Map<String, Object> userinfo = new HashMap<>();
userinfo.put("userage", "100");
userinfo.put("email", "xxx@email.com");
response.getResult().putAll(userinfo);
response.setMsg("获取用户信息成功!");
return 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
Spring Cloud Alibaba 使用Feign进行服务消费的更多相关文章
- Spring Cloud Alibaba 使用RestTemplate进行服务消费
创建服务提供者工程 创建spring-cloud-alibaba-service-member工程,会员中心服务该服务提供用户会员信息. pom.xml <?xml version=" ...
- Spring Cloud Alibaba(8)---Feign服务调用
Feign服务调用 有关Spring Cloud Alibaba之前写过五篇文章,这篇也是在上面项目的基础上进行开发. Spring Cloud Alibaba(1)---入门篇 Spring Clo ...
- 0.9.0.RELEASE版本的spring cloud alibaba sentinel+feign降级处理实例
既然用到了feign,那么主要是针对服务消费方的降级处理.我们基于0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例添油加醋,把sentinel功能加 ...
- Spring Cloud Alibaba+Nacos搭建微服务架构
1. Spring Cloud Alibaba 简介 Spring Cloud Alibaba是阿里巴巴为分布式应用提供的一站式解决方案,能够更方便快捷地搭建分布式平台,nacos拥有着替换eu ...
- 0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例
这里的feign依然是原来的feign,只不过将注册中心由eureka换成了nacos.服务提供方参见0.9.0.RELEASE版本的spring cloud alibaba nacos实例,消费方跟 ...
- Spring Cloud Alibaba(2)---RestTemplate微服务项目
RestTemplate微服务项目 前言 因为要运用 Spring Cloud Alibaba 开源组件到分布式项目中,所以这里先搭建一个不通过 Spring Cloud只通过 RestTemplat ...
- Spring Cloud Alibaba系列之分布式服务组件Dubbo
本博客的例子代码可以在github找到下载链接:代码下载 SpringBoot.SpringCloud Alibaba系列博客专栏:链接 1.分布式理论 1.1.分布式基本定义 <分布式系统原理 ...
- 阿里巴巴开源 Spring Cloud Alibaba,加码微服务生态建设
本周,Spring Cloud联合创始人Spencer Gibb在Spring官网的博客页面宣布:阿里巴巴开源 Spring Cloud Alibaba,并发布了首个预览版本.随后,Spring Cl ...
- Spring Cloud Alibaba 实战 之 Nacos 服务注册和发现
服务注册与发现,服务发现主要用于实现各个微服务实例的自动化注册与发现,是微服务治理的核心,学习 Spring Cloud Alibaba,首先要了解框架中的服务注册和发现组件——Nacos. 一.Sp ...
随机推荐
- Java面向对象系列(7)- 什么是继承
继承 继承的本质是对某一批类的抽象,从而实现对现实世界更好的建模 extands的意思是"扩展".子类是父类的扩展 JAVA中类只有单继承,没有多继承 继承是类和类之间的一种关系. ...
- Navicat15 最新版本破解版操作步骤
1.关于Navicat 15的安装版本以及破解机下载 分享的网盘连接:https://pan.baidu.com/s/12DaG0TmS9hXlYmJ_T5ytz2rA 提取码:7cg6 2.安装Na ...
- jmeter之命令行执行jmx脚本
使用界面执行不稳定,且保存报告非常麻烦 https://www.jb51.net/article/191367.htm 作者:Anthony_tester 来源:CSDN 原文:https://blo ...
- python学习笔记(四)-文件操作
文件读写"""一.文件打开有3种方式 1.读 r #如果打开的文件的时候没有指定模式,那么默认是读 读写模式 r+,只要沾上r,文件不存在的时候,打开都会报错 2.写 w ...
- 51nod1229-序列求和V2【数学,拉格朗日插值】
正题 题目链接:http://www.51nod.com/Challenge/Problem.html#problemId=1229 题目大意 给出\(n,k,r\)求 \[\sum_{i=1}^ni ...
- P7443-加边【博弈论】
正题 题目链接:https://www.luogu.com.cn/problem/P7443?contestId=41429 题目大意 \(n\)个点的一棵有根树,两个人从一号点开始进行有向图博弈. ...
- Python+requests环境搭建和GET基本用法
Python+requests环境搭建 首先你得安装Python,然后安装requests模块(第3方模块,安装方法:pip install requests) 基本用法 get 请求(不带参数的) ...
- jQuery实现购物车功能
1.HTML代码(品优购模版) <!DOCTYPE html> <html lang="zh-CN"> <head> <meta char ...
- [源码解析] PyTorch 流水线并行实现 (5)--计算依赖
[源码解析] PyTorch 流水线并行实现 (5)--计算依赖 目录 [源码解析] PyTorch 流水线并行实现 (5)--计算依赖 0x00 摘要 0x01 前文回顾 0x02 计算依赖 0x0 ...
- 对于caffe程序中出现的Unknown database backend问题的报错怎么办?
在预处理器中添加USE_LMDB,因为caffe需要一种数据输入格式 这样,在db.cpp中#ifdef USE_LMDB就会变亮,显示使用的数据格式为LMDB