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的更多相关文章

  1. Spring Cloud 入门 之 Feign 篇(三)

    原文地址:Spring Cloud 入门 之 Feign 篇(三) 博客地址:http://www.extlight.com 一.前言 在上一篇文章<Spring Cloud 入门 之 Ribb ...

  2. Spring Cloud 入门 之 Hystrix 篇(四)

    原文地址:Spring Cloud 入门 之 Hystrix 篇(四) 博客地址:http://www.extlight.com 一.前言 在微服务应用中,服务存在一定的依赖关系,如果某个目标服务调用 ...

  3. Spring Cloud 入门教程(六): 用声明式REST客户端Feign调用远端HTTP服务

    首先简单解释一下什么是声明式实现? 要做一件事, 需要知道三个要素,where, what, how.即在哪里( where)用什么办法(how)做什么(what).什么时候做(when)我们纳入ho ...

  4. spring cloud 入门系列四:使用Hystrix 实现断路器进行服务容错保护

    在微服务中,我们将系统拆分为很多个服务单元,各单元之间通过服务注册和订阅消费的方式进行相互依赖.但是如果有一些服务出现问题了会怎么样? 比如说有三个服务(ABC),A调用B,B调用C.由于网络延迟或C ...

  5. Spring Cloud 入门教程(四): 分布式环境下自动发现配置服务

    前一章, 我们的Hello world应用服务,通过配置服务器Config Server获取到了我们配置的hello信息“hello world”. 但自己的配置文件中必须配置config serve ...

  6. Spring Cloud 入门教程(九): 路由网关zuul

    在微服务架构中,需要几个关键的组件,服务注册与发现.服务消费.负载均衡.断路器.智能路由.配置管理等,由这几个组件可以组建一个简单的微服务架构.客户端的请求首先经过负载均衡(zuul.Ngnix),再 ...

  7. Spring Cloud 入门教程(五): Ribbon实现客户端的负载均衡

    接上节,假如我们的Hello world服务的访问量剧增,用一个服务已经无法承载, 我们可以把Hello World服务做成一个集群. 很简单,我们只需要复制Hello world服务,同时将原来的端 ...

  8. spring cloud 入门系列:总结

    从我第一次接触Spring Cloud到现在已经有3个多月了,当时是在博客园里面注册了账号,并且看到很多文章都在谈论微服务,因此我就去了解了下,最终决定开始学习Spring Cloud.我在一款阅读A ...

  9. spring cloud 入门系列六:使用Zuul 实现API网关服务

    通过前面几次的分享,我们了解了微服务架构的几个核心设施,通过这些组件我们可以搭建简单的微服务架构系统.比如通过Spring Cloud Eureka搭建高可用的服务注册中心并实现服务的注册和发现: 通 ...

随机推荐

  1. 基于FBX SDK的FBX模型解析与加载 -(三)

    http://blog.csdn.net/bugrunner/article/details/7229416 6. 加载Camera和Light 在FBX模型中除了几何数据外较为常用的信息可能就是Ca ...

  2. STL<queue>的使用

    队列是一种基本的线性数据结构.它满足先进先出(First In ,First Out)的原则. 我们可以应用这种数据结构实现很多复杂的问题.但每次要手写队列的相关函数并不省事,我们便可以应用STL中的 ...

  3. Nginx系列篇三:linux中Nginx+keepalived做一个高可用的主从配置

    建议:先阅读搭建Nginx负载均衡之后再看此篇 备注: Nginx+keepalived的高可用有两种方式 一.主从配置 二.双主热备配置[下一篇] 准备: 标配四台服务器 Master:192.16 ...

  4. django网页渲染

    模板标签种类 {% for blog in blog_list %} <h2>{{ blog.title }}</h2> <p>{{ blog.timestamp ...

  5. Python %s和%r的区别

    %s 用str()方法处理对象 %r 用rper()方法处理对象,打印时能够重现它所代表的对象(rper() unambiguously recreate the object it represen ...

  6. Qt容器类之三:通用算法

    在<QtAlgorithm>头文件中,Qt提供了一些全局的模板函数,这些函数是可以使用在容器上的十分常用的算法.我们可以在任何提供了STL风格迭代器的容器类上用这些算法,包括QList.Q ...

  7. codeforces 615 B. Longtail Hedgehog (DFS + 剪枝)

    题目链接: codeforces 615 B. Longtail Hedgehog (DFS + 剪枝) 题目描述: 给定n个点m条无向边的图,设一条节点递增的链末尾节点为u,链上点的个数为P,则该链 ...

  8. Educational Codeforces Round 20 B

    Description You are given the array of integer numbers a0, a1, ..., an - 1. For each element find th ...

  9. 递推DP URAL 1244 Gentlemen

    题目传送门 /* 题意:给出少了若干卡片后的总和,和原来所有卡片,问少了哪几张 DP:转化为少了的总和是否能有若干张卡片相加得到,dp[j+a[i]] += dp[j]; 记录一次路径,当第一次更新的 ...

  10. RHEL6.5----LVS(NAT)

    主机名 IP  所需软件  master 192.168.30.130(Nat) 192.168.17.130(VMnet4) ipvsadm  node-1 192.168.17.131  http ...