<Spring Cloud>入门四 Feign
1.Feign
之前使用的是Ribbon+RestTemplate调用,通过的是微服务的名字进行调用,实现负载均衡

但是为了满足接口编程,提供了Feign
2.实现
2.1引入坐标
在 ms-common-api 和 ms-consumer-dept-80-feign 引入坐标
<!--feign 客户端负载均衡-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2.2 在 ms-common-api 实现调用
package org.maple.service; import org.maple.entity.Dept;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import java.util.List; /**
* @author mapleins
* @Date 2019-01-12 23:10
* @Desc 通过接口和注解 面向接口编程访问微服务
**/
@FeignClient("ms-provider-dept")
public interface DeptClientService { @PostMapping("/dept/add")
boolean add(@RequestBody Dept dept); @GetMapping("/dept/get/{id}")
Dept get(@PathVariable("id") Long id); @GetMapping("/dept/list")
List<Dept> list(); }
2.3修改 dept-80-feign的启动类
package org.maple; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients; /**
* @author mapleins
* @Date 2019-01-09 20:26
* @Desc
**/
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = {"org.maple.service"}) //扫描ms-common-api中的service包
public class App_Consumer_Dept_80_Feign { public static void main(String[] args) {
SpringApplication.run(App_Consumer_Dept_80_Feign.class, args);
}
}
2.4 controller层
这样就相当于不是去调用微服务去编程,而是通过controller调用service层,实现接口编程,并且自带ribbon的轮询算法
package org.maple.controller; import org.maple.entity.Dept;
import org.maple.service.DeptClientService;
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.PostMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.List; /**
* @author mapleins
* @Date 2019-01-09 20:10
* @Desc
**/
@RestController
public class DeptController_Consumer { @Autowired
private DeptClientService service; @GetMapping("/consumer/dept/get/{id}")
public Dept get(@PathVariable("id") Long id){
return service.get(id);
} @GetMapping("/consumer/dept/list")
public List<Dept> list(){
return service.list();
} @PostMapping("/consumer/dept/add")
public boolean add(Dept dept){
return service.add(dept);
} }
<Spring Cloud>入门四 Feign的更多相关文章
- Spring Cloud 入门 之 Feign 篇(三)
原文地址:Spring Cloud 入门 之 Feign 篇(三) 博客地址:http://www.extlight.com 一.前言 在上一篇文章<Spring Cloud 入门 之 Ribb ...
- Spring Cloud 入门 之 Hystrix 篇(四)
原文地址:Spring Cloud 入门 之 Hystrix 篇(四) 博客地址:http://www.extlight.com 一.前言 在微服务应用中,服务存在一定的依赖关系,如果某个目标服务调用 ...
- Spring Cloud 入门教程(六): 用声明式REST客户端Feign调用远端HTTP服务
首先简单解释一下什么是声明式实现? 要做一件事, 需要知道三个要素,where, what, how.即在哪里( where)用什么办法(how)做什么(what).什么时候做(when)我们纳入ho ...
- spring cloud 入门系列四:使用Hystrix 实现断路器进行服务容错保护
在微服务中,我们将系统拆分为很多个服务单元,各单元之间通过服务注册和订阅消费的方式进行相互依赖.但是如果有一些服务出现问题了会怎么样? 比如说有三个服务(ABC),A调用B,B调用C.由于网络延迟或C ...
- Spring Cloud 入门教程(四): 分布式环境下自动发现配置服务
前一章, 我们的Hello world应用服务,通过配置服务器Config Server获取到了我们配置的hello信息“hello world”. 但自己的配置文件中必须配置config serve ...
- Spring Cloud 入门教程(九): 路由网关zuul
在微服务架构中,需要几个关键的组件,服务注册与发现.服务消费.负载均衡.断路器.智能路由.配置管理等,由这几个组件可以组建一个简单的微服务架构.客户端的请求首先经过负载均衡(zuul.Ngnix),再 ...
- Spring Cloud 入门教程(五): Ribbon实现客户端的负载均衡
接上节,假如我们的Hello world服务的访问量剧增,用一个服务已经无法承载, 我们可以把Hello World服务做成一个集群. 很简单,我们只需要复制Hello world服务,同时将原来的端 ...
- spring cloud 入门系列:总结
从我第一次接触Spring Cloud到现在已经有3个多月了,当时是在博客园里面注册了账号,并且看到很多文章都在谈论微服务,因此我就去了解了下,最终决定开始学习Spring Cloud.我在一款阅读A ...
- spring cloud 入门系列六:使用Zuul 实现API网关服务
通过前面几次的分享,我们了解了微服务架构的几个核心设施,通过这些组件我们可以搭建简单的微服务架构系统.比如通过Spring Cloud Eureka搭建高可用的服务注册中心并实现服务的注册和发现: 通 ...
随机推荐
- Codeforces 702B【二分】
题意: 给一个a数组,输出有多少对相加是等于2^x的.1<=a[i]<=1e9,n<=1e5 思路: a[i]+a[j]=2^x 对于每个a[i],枚举x,然后二分查找a[j]; p ...
- 3DMAX 9 角色建模3 uv展开
将角色删除一半,展好uv再镜像出来,节省一半工作量(前提是对称) 添加UVW展开编辑器(Unwrap UVW),选择面 打开UV编辑器 这里要注意映射问题,默认打开UV编辑器后所选择的面是映射到与选择 ...
- HDU6447(离散化扫描线+树状数组)
一眼看过去就x排序扫描一下,y是1e9的离散化一下,每层用树状数组维护一下,然后像dp倒着循环似的树状数组就用y倒着插就可行了. 类似题目练习:BZOJ4653.BZOJ1218 #pragma co ...
- HBase备份恢复练习
一.冷备 1.创建测试表并插入测试数据 [root@weekend05 ~]# hbase shell hbase(main):005:0> create 'scores','grade','c ...
- qconbeijing2018
https://2018.qconbeijing.com/schedule 会议 · 第一天 (2018/04/20 周五) 时间 日程 上午 主题演讲 大数据下的软件质量建设实践 黄闻欣 出品 人工 ...
- WIN2003 IIS相关错误解决方案
我碰到的主要问题是:“Server Application Unavailable 错误”.“无法显示网页”: 1.如果你的.NET版本是2.0及以上的话,那要注意了:win2003是默认安装1.1的 ...
- 外文翻译 《How we decide》多巴胺的预言 第三节
这是第二章的最后一节. 书的导言 本章第一节 本章第二节 本节阅读感言:自我批评是自我提升的妙方. 多巴胺是我们感情的源泉.多巴胺相关的神经系统在不断的记录着我们主观意识没有注意到的一个个模式,将它们 ...
- Thymeleaf 总结
在javaScript中使用表达式 var list = /*[[${list}]]*/ null; <script th:inline="javascript"> ...
- Activity的创建、生命周期
Activity是Android四大组件之一.一个Activity负责管理一个界面. 创建一个Activity: New -> Activity -> 选择要创建的Activity类型(一 ...
- 【经验总结】关于使用某些第三方插件库元素设置display:none后重新show不显示的问题;(display、opacity、宽高0的使用场景)
display:none 直接取消元素所占用的位置(但是元素还是存在的),后面元素看他就相当于不存在了: opacity:0 隐藏,但是其依旧占用位置: height.width:0 和displa ...