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 ...
随机推荐
- Layui的落幕,是否预示一个时代的结束?
1.今天,看到LayUi(读音类UI)官方说,LayUI官网将关闭,多少有些伤感. 或许,有人会所,通知里也说了,"新版下载.文档和示例等仍会在Github 和 Gitee" 但, ...
- win10系统显示此电脑
今天电脑开机后发现从任务栏进入"文件资源管理器",直接卡死,重启电脑也没有用. 我想到是不是从"此电脑"进入不会卡死,试了一下果真没有卡死. 使用win10系统 ...
- ELK实战部署
环境 : 一台 centos 6.7 IP地址: 192.168.88.250 软件版本 : ElasticSearch 2.1.0 Logstash 2.1.1 Kibana 4.3.1 ...
- Fiddler抓包(以谷歌浏览器、安卓手机为例)
fiddler抓包流程与whistle相同,所以本章内容会相对简洁.如果需要详细说明,可参考whistle抓包. 这里以谷歌浏览器.安卓手机为例. 1.fiddler安装 下载安装包,默认安装. 2. ...
- 鸿蒙内核源码分析(编译过程篇) | 简单案例窥视GCC编译全过程 | 百篇博客分析OpenHarmony源码| v57.01
百篇博客系列篇.本篇为: v57.xx 鸿蒙内核源码分析(编译过程篇) | 简单案例窥视编译全过程 | 51.c.h.o 编译构建相关篇为: v50.xx 鸿蒙内核源码分析(编译环境篇) | 编译鸿蒙 ...
- 单页应用后退不刷新方案(vue & react)
引言 前进刷新,后退不刷新,是一个类似app页面的特点,要在单页web应用中做后退不刷新,却并非一件易事. 为什么麻烦 spa的渲染原理(以vue为例):url的更改触发onHashChange/pu ...
- 测试用例 setup 和 和 teardown
前言 学过unittest的都知道里面用前置和后置setup呾teardown非常好用,在每次用例开始前呾结束后都去执行一次.当然迓有更高级一点的 setupClass 呾 teardownClass ...
- IdentityServer4系列[6]授权码模式
授权码模式是一种混合模式,是目前功能最完整.流程最严密的授权模式.它主要分为两大步骤:认证和授权.其流程为: 用户访问客户端,客户端将用户导向Identity Server. 用户填写凭证信息向客户端 ...
- firewalld dbus接口使用指南
firewalld,一个基于动态区的iptables/nftables守护程序,自2009年左右开始开发,最新版本 - 防火墙0.6.3 - 发布于2018年10月11日.主要的开发人员是托马斯·沃纳 ...
- Elasticsearch 存储成本省 60%,稿定科技干货分享
背景 稿定科技旗下稿定设计产品是一个聚焦商业设计的多场景在线设计平台,打破了软硬件间的技术限制,汇集创意内容与设计工具于一体,为不同场景下的设计需求提供优质的解决方案,满足图片.视频等全类型媒介的设计 ...