为什么使用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进行服务消费的更多相关文章

  1. Spring Cloud Alibaba 使用RestTemplate进行服务消费

    创建服务提供者工程 创建spring-cloud-alibaba-service-member工程,会员中心服务该服务提供用户会员信息. pom.xml <?xml version=" ...

  2. Spring Cloud Alibaba(8)---Feign服务调用

    Feign服务调用 有关Spring Cloud Alibaba之前写过五篇文章,这篇也是在上面项目的基础上进行开发. Spring Cloud Alibaba(1)---入门篇 Spring Clo ...

  3. 0.9.0.RELEASE版本的spring cloud alibaba sentinel+feign降级处理实例

    既然用到了feign,那么主要是针对服务消费方的降级处理.我们基于0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例添油加醋,把sentinel功能加 ...

  4. Spring Cloud Alibaba+Nacos搭建微服务架构

    1. Spring Cloud Alibaba 简介    Spring Cloud Alibaba是阿里巴巴为分布式应用提供的一站式解决方案,能够更方便快捷地搭建分布式平台,nacos拥有着替换eu ...

  5. 0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例

    这里的feign依然是原来的feign,只不过将注册中心由eureka换成了nacos.服务提供方参见0.9.0.RELEASE版本的spring cloud alibaba nacos实例,消费方跟 ...

  6. Spring Cloud Alibaba(2)---RestTemplate微服务项目

    RestTemplate微服务项目 前言 因为要运用 Spring Cloud Alibaba 开源组件到分布式项目中,所以这里先搭建一个不通过 Spring Cloud只通过 RestTemplat ...

  7. Spring Cloud Alibaba系列之分布式服务组件Dubbo

    本博客的例子代码可以在github找到下载链接:代码下载 SpringBoot.SpringCloud Alibaba系列博客专栏:链接 1.分布式理论 1.1.分布式基本定义 <分布式系统原理 ...

  8. 阿里巴巴开源 Spring Cloud Alibaba,加码微服务生态建设

    本周,Spring Cloud联合创始人Spencer Gibb在Spring官网的博客页面宣布:阿里巴巴开源 Spring Cloud Alibaba,并发布了首个预览版本.随后,Spring Cl ...

  9. Spring Cloud Alibaba 实战 之 Nacos 服务注册和发现

    服务注册与发现,服务发现主要用于实现各个微服务实例的自动化注册与发现,是微服务治理的核心,学习 Spring Cloud Alibaba,首先要了解框架中的服务注册和发现组件——Nacos. 一.Sp ...

随机推荐

  1. 《集体智慧编程学习笔记》——Chapter2:提供推荐

    知识点: 1. 协作型过滤--Collaboraive Filtering 通常的做法是对一群人进行搜索,并从中找出与我们品味相近的一小群人,算法会对这些人的偏好进行考察,并将它们组合起来构造出一个经 ...

  2. Hibernate 的 <= 出现问题

    问题模拟 select new map( e.name as name , e.salary as salary) from Emplpyee e where e.salary <= :sala ...

  3. 记typora美化----让文章更加优美

    前言 昨晚偶然间看到一篇介绍记笔记工具以及如何美化的视频,突发奇想我打算也写一篇记录一下自己的美化过程,并会把自己使用的插件,样式文件等提供在下方,觉得不错得可以直接拿去使用,只希望观众能够一键3连, ...

  4. AOJ/树与二叉搜索树习题集

    ALDS1_7_A-RootedTree. Description: A graph G = (V, E) is a data structure where V is a finite set of ...

  5. 8.JVM内存分配机制超详细解析

    一.对象的加载过程 之前研究过类的加载过程.具体详情可查看文章:https://www.cnblogs.com/ITPower/p/15356099.html 那么,当一个对象被new的时候,是如何加 ...

  6. 易华录 X ShardingSphere|葫芦 App 后台数据处理的逻辑捷径

    "ShardingSphere 大大简化了分库分表的开发和维护工作,对于业务的快速上线起到了非常大的支撑作用,保守估计 ShardingSphere 至少为我们节省了 4 个月的研发成本.& ...

  7. ArcPy获取栅格属性

    获取栅格属性 (数据管理) 描述 从元数据和栅格数据集的相关描述性统计数据中检索信息. 使用方法 返回的属性将显示在结果窗口中. 此工具的 Python 结果是地理处理结果对象.要获取字符串值,请使用 ...

  8. Pytorch 的安装

    GPU版本的安装 Windows平台 CPU 版本安装 conda install pytorch torchvision cpuonly -c puython Windows平台需安装VC,需要的联 ...

  9. C 输入输出函数

    流 就C程序而言,所有的I/O操作只是简单地从程序移入或移出字节的事情.这种字节流便称为流( stream ). 绝大多数流是完全缓存的,这意味着"读取"和"写入&quo ...

  10. SignalR 在React/GO技术栈的生产应用

    哼哧哼哧半年,优化改进了一个运维开发web平台. 本文记录SignalR在react/golang 技术栈的生产小实践. 1. 背景 有个前后端分离的运维开发web平台, 后端会间隔5分钟同步一次数据 ...