【SpringCloud】第三篇: 服务消费者(Feign)
前言:
必需学会SpringBoot基础知识
简介:
spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等。它运行环境简单,可以在开发人员的电脑上跑。
工具:
JDK8
apache-maven-3.5.2
IntelliJ IDEA 2017.3 x64
一、Feign简介
Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。
简而言之:
Feign 采用的是基于接口的注解
Feign 整合了ribbon
二、准备工作
1. 运行 eureka-server:8761;
2. 运行 eureka-client 两次,端口分别为8762 、8773
三、创建一个feign的服务
3.1 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.lwc</groupId>
<artifactId>eureka-feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>eureka-feign</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR2</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
3.2 通过 application.yml 跳转 application-dev.yml 配置程序名, 端口 和指定注册中心 如下:
spring:
application:
name: eureka-feign
profiles:
active: ${@profileActiv@:dev}
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8765
3.3 启动类添加@EnableFeignClients注解开启Feign的功能:
package com.lwc; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients; /**
* @author Eddie
*/
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaFeignApplication { public static void main(String[] args) {
SpringApplication.run(EurekaFeignApplication.class, args);
}
}
3.4 定义一个feign接口,通过@ FeignClient(“服务名”), 通过服务名找对应服务 如下:
package com.lwc.service; import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam; /**
* @author eddie.lee
* @Package com.lwc.service
* @ClassName FeignClientService
* @description
* @date created in 2018-03-27 10:43
* @modified by
*/
@FeignClient("eureka-client")
public interface FeignClientService { @GetMapping("/eureka/client")
String feignToClientByName(@RequestParam("name") String name); }
3.5 创建controller层,通过API接口,来消费Feign客户端服务. 如下:
package com.lwc.controller; import com.lwc.service.FeignClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; /**
* @author eddie.lee
* @Package com.lwc.controller
* @ClassName FeignController
* @description 服务消费者(Feign)
* @date created in 2018-03-27 10:50
* @modified by
*/
@RestController
@RequestMapping("/eureka")
public class FeignController { @Autowired
private FeignClientService feignClientService; @GetMapping("/feign")
public String feign(@RequestParam("name") String name) {
return feignClientService.feignToClientByName(name);
} }
四、参考资料
eureka-feign
http://localhost:8765/eureka/feign?name=eddie
标签
3-1
源码下载
https://github.com/eddie-code/SpringCloudDemo#springclouddemo
疑惑: eureka-ribbon, eureka-feign 两个功能相同, 区别在哪里?
答: feign是基于ribbon衍生出来, feign采用接口方式, 更简洁;
【SpringCloud】第三篇: 服务消费者(Feign)的更多相关文章
- 史上最简单的SpringCloud教程 | 第三篇: 服务消费者(Feign)
转载请标明出处: https://www.fangzhipeng.com/springcloud/2017/07/12/sc03-feign/ 本文出自方志朋的博客 最新Finchley版本请访问: ...
- SpringCloud教程 | 第三篇: 服务消费者(Feign)
上一篇文章,讲述了如何通过RestTemplate+Ribbon去消费服务,这篇文章主要讲述如何通过Feign去消费服务.一.Feign简介 Feign是一个声明式的伪Http客户端,它使得写Http ...
- SpringCloud教程 | 第三篇: 服务消费者(Feign)(Finchley版本)
上一篇文章,讲述了如何通过RestTemplate+Ribbon去消费服务,这篇文章主要讲述如何通过Feign去消费服务. 一.Feign简介 Feign是一个声明式的伪Http客户端,它使得写Htt ...
- 史上最简单的SpringCloud教程 | 第三篇: 服务消费者(Feign)(Finchley版本)
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f3-feign/ 本文出自方志朋的博客 上一篇文章,讲述了如 ...
- 第三篇: 服务消费者(Feign)
本文根据https://blog.csdn.net/forezp/article/details/81040965写出,修正了部分瑕疵,在此对那位博主表示感谢. 上一篇文章讲述通过RestTempla ...
- SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)
在上一篇文章,讲了服务的注册和发现.在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式,一种是ribbon+r ...
- SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)(Finchley版本)
在上一篇文章,讲了服务的注册和发现.在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式,一种是ribbon+r ...
- 史上最简单的SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)(Finchley版本)
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f2-ribbon/ 本文出自方志朋的博客 在上一篇文章,讲了 ...
- 史上最简单的SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)
在上一篇文章,讲了服务的注册和发现.在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式,一种是ribbon+r ...
随机推荐
- 把list(对象)集合中的(某个属性),放到数组中。
List<SpecialguardInfo> list=specialguardOrderService.findfreeSg(date1,date2);//得到list对象集合 Stri ...
- 443M衣架遥控arduino代码备档
] = {,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,}; ] = {,,,,,,,,,,,,,,,,,,,,,,, ...
- Jmeter--随机生成浮点数
post接口中需要随机生成目标对象的地理位置: 所以就使用了javaScript函数,如下所示: { "Location": { "Altitude": ${_ ...
- GIT 报错:Result too large 解决办法
在使用bower install命令下载前端依赖的js插件时,git出错了,报错信息如下: bower ECMDERR Failed to execute "git ls-remote -- ...
- Oracle模糊查询
通配符 % 匹配零个或更多的任意字符 _ 匹配一个任意字符 [ ] 匹配指定范围中的一个字符([a-z],[0-9]) [^ ] 不属于指定范围,不包含其中的字符 escape转义 --查询 ...
- 冒泡排序_c++
冒泡排序_c++ GitHub 文解 冒泡排序是采用类似气泡上升的方式对数据进行排序. 例如: 我们这里有10个元素,具体数值随意,对每个数值标记上 1~10 的标记. 首先将标记为 1 的数值与标记 ...
- c#随便聊聊数据库操作
最近在学习web后台以及Python,到了程序员的转折年纪了,哎.估计很久不会写博文了.言归正传. 在原理的数据库连接池HiKari项目上.我扩展了独立的3个库,说是3个库,其实原本该是一个库.先聊聊 ...
- 基于DCT的图片数字水印实验
1. 实验类别 设计型实验:MATLAB设计并实现基于DCT的图像数字水印算法. 2. 实验目的 了解基于DCT的图像数字水印技术,掌握基于DCT系数关系的图像水印算法原理,设计并实现一种基于DCT的 ...
- ABAP术语-Transaction
Transaction 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/19/1112804.html Logical process in ...
- Centos6_32位系统512M内存_如何安装gogs_Mysql_配置开机自启动
因为有很多人的Linux版本比较低,内存配置也较低,X86 ,32位系统的:所以这里推荐采用二进制安装gogs,并且使用Mysql:这个是傻瓜式的安装方案,适合绝大多数人(提及了centos7的安装思 ...