跟我学SpringCloud | 第三篇:服务的提供与Feign调用
SpringCloud系列教程 | 第三篇:服务的提供与Feign调用
Springboot: 2.1.6.RELEASE
SpringCloud: Greenwich.SR1
如无特殊说明,本系列教程全采用以上版本
上一篇,我们介绍了注册中心的搭建,包括集群环境吓注册中心的搭建,这篇文章介绍一下如何使用注册中心,创建一个服务的提供者,使用一个简单的客户端去调用服务端提供的服务。
本篇文章中需要三个角色,分别是服务的提供者,服务的消费者,还有一个是上一篇文章的主角——注册中心Eureka(使用单机版本即可,本篇的示例也会使用单机版本的Eureka)。
整体流程为:
- 先启动注册中心Eureka
- 启动服务的提供者将提供服务,并将服务注册到注册中心Eureka上
- 启动服务的消费者,在注册中心中找到服务并完成消费
1. 服务提供者
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springcloud</groupId>
<artifactId>producer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>producer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</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-netflix-eureka-client</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>
2. 配置文件application.yml
server:
port: 8080
spring:
application:
name: spring-cloud-producer
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
3. 启动类ProducerApplication.java
增加@EnableEurekaClient,如果是其他注册中心可以使用注解@EnableDiscoveryClient来进行服务的注册
package com.springcloud.producer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
}
4. Controller
package com.springcloud.producer.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* Created with IntelliJ IDEA.
*
* @Date: 2019/7/2
* @Time: 0:02
* @email: inwsy@hotmail.com
* Description:
*/
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(@RequestParam String name) {
return "hello "+name+",producer is ready";
}
}
先在可以先启动上一篇当中单机版的Eureka,再启动我们刚写好的producer服务提供者,启动成功后,访问链接http://localhost:8761/,可以看到我们的的服务提供者producer已经成功注册在注册中心上了。

至此,服务的提供者已经配置完成。
2. 服务消费者
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springcloud</groupId>
<artifactId>consumers</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>consumers</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</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>
spring-boot-starter-web: 这个包是通用的web开发包,里面包含了spring-web、spring-webmvc等包
spring-cloud-starter-openfeign: 这个包是springcloud对于Feign的封装,Feign是一个声明式的Web服务客户端。它支持Feign本身的注解、JAX-RS注解以及SpringMVC的注解。Spring Cloud集成Ribbon和Eureka以在使用Feign时提供负载均衡的http客户端。
2. 配置文件application.yml
server:
port: 8081
spring:
application:
name: spring-cloud-consumers
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
3. 启动类ConsumersApplication.java
同上,增加@EnableEurekaClient,如果是其他注册中心可以使用注解@EnableDiscoveryClient来进行服务的注册
package com.springcloud.consumers;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ConsumersApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumersApplication.class, args);
}
}
@EnableFeignClients: 这个注解是通知SpringBoot在启动的时候,扫描被 @FeignClient 修饰的类,@FeignClient这个注解在进行远程调用的时候会用到。
4. Feign远程调用
Feign是一个声明式Web Service客户端。使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。
创建一个remote接口
package com.springcloud.consumers.remote;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* @Author: shiyao.wei
* @Date: 2019/7/2 11:14
* @Version: 1.0
* @Desc:
*/
@FeignClient(name= "spring-cloud-producer")
public interface HelloRemote {
@RequestMapping(value = "/hello")
String hello(@RequestParam(value = "name") String name);
}
- name:远程服务名,及spring.application.name配置的名称
- 此类中的方法和远程服务中contoller中的方法名和参数需保持一致
5. web层调用远程接口 Controller
package com.springcloud.consumers.controller;
import com.springcloud.consumers.remote.HelloRemote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: shiyao.wei
* @Date: 2019/7/2 11:25
* @Version: 1.0
* @Desc:
*/
@RestController
public class HelloController {
@Autowired
HelloRemote helloRemote;
@RequestMapping("/hello/{name}")
public String index(@PathVariable("name") String name) {
return helloRemote.hello(name);
}
}
现在,一个最简单的服务注册和调用的例子就完成了。
3. 测试
简单调用
顺次启动eureka、producer、consumer三个项目
启动成功后,先在浏览器输入http://localhost:8080/hello?name=springcloud
可以看到页面显示:hello springcloud,producer is ready
证明我们的producer已经正常启动,提供的服务也正常
接下来,我们测试服务消费者,在浏览器中输入:http://localhost:8081/hello/spring
可以看到页面显示:hello spring,producer is ready
说明客户端已经成功的通过feign调用了远程服务hello,并且将结果返回到了浏览器。
负载均衡
将上面的producer复制一份,修改名称为producer2,修改pom.xml中的<name></name>为producer2,修改其中的Controller:
package com.springcloud.producer.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* Created with IntelliJ IDEA.
*
* @Date: 2019/7/2
* @Time: 0:02
* @email: inwsy@hotmail.com
* Description:
*/
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(@RequestParam String name) {
return "hello "+name+",producer2 is ready";
}
}
修改application.yml配置文件启动端口为8082
启动我们刚复制好的producer2,这时可以看一下注册中心Eureka,我们现在已经有两个producer服务了。

这时我们再去访问:http://localhost:8081/hello/spring
第一次返回结果:hello spring,producer is ready
第二次返回结果:hello spring,producer2 is ready
连续刷新页面,两个结果会交替出现,说明注册中心提供了服务负载均衡功能。将服务数提高到N个,会发现测试结果一样,请求会自动轮询到每个服务端来处理。
好了,现在可以将代码打包扔到Github上去了,欢迎大家前往Github骚扰:)
系列回顾
跟我学SpringCloud | 第三篇:服务的提供与Feign调用的更多相关文章
- [老老实实学WCF] 第三篇 在IIS中寄存服务
老老实实学WCF 第三篇 在IIS中寄宿服务 通过前两篇的学习,我们了解了如何搭建一个最简单的WCF通信模型,包括定义和实现服务协定.配置服务.寄宿服务.通过添加服务引用的方式配置客户端并访问服务.我 ...
- 史上最简单的SpringCloud教程 | 第三篇: 服务消费者(Feign)
转载请标明出处: https://www.fangzhipeng.com/springcloud/2017/07/12/sc03-feign/ 本文出自方志朋的博客 最新Finchley版本请访问: ...
- 跟我学SpringCloud | 第十七篇:服务网关Zuul基于Apollo动态路由
目录 SpringCloud系列教程 | 第十七篇:服务网关Zuul基于Apollo动态路由 Apollo概述 Apollo相比于Spring Cloud Config优势 工程实战 示例代码 Spr ...
- 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/ 本文出自方志朋的博客 上一篇文章,讲述了如 ...
- 跟我学SpringCloud | 第十篇:服务网关Zuul高级篇
SpringCloud系列教程 | 第十篇:服务网关Zuul高级篇 Springboot: 2.1.6.RELEASE SpringCloud: Greenwich.SR1 如无特殊说明,本系列教程全 ...
- (转) [老老实实学WCF] 第三篇 在IIS中寄存服务
第三篇 在IIS中寄宿服务 通过前两篇的学习,我们了解了如何搭建一个最简单的WCF通信模型,包括定义和实现服务协定.配置服务.寄宿服务.通过添加服务引用的方式配置客户端并访问服务.我们对WCF的编程生 ...
- 史上最简单的SpringCloud教程 | 第九篇: 服务链路追踪(Spring Cloud Sleuth)(Finchley版本)
转载请标明出处: 原文首发于:>https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f9-sleuth/ 本文出自方志朋的博客 这篇文章主 ...
随机推荐
- 计时器timer的使用
https://www.cnblogs.com/ILoveSleep/archive/2013/06/12/3133322.html
- WPF扑克牌之红桃K
原文:WPF扑克牌之红桃K 有些什么用途呢?我想,如果你有兴趣,可用来制作WPF扑克牌游戏. 没有任何技术含量,需要做的是在Blend中绘图或者使用Illustrator,CoreDraw等矢图设计软 ...
- c#调用ffmpeg嵌入srt/ass字幕提示Cannot load default config file...
c#调用ffmpeg嵌入srt/ass字幕提示 Fontconfig error: Cannot load default config file[Parsed_subtitles_0 @ 00000 ...
- Go程序开发---Go环境配置:CentOS6.5+Go1.8标准包安装
1.Go安装 1.1Go的三种安装方式 Go有多种安装方式,可以选择自己习惯的方式进行,这里介绍三种安装方式: 1)Go源码安装 2)Go标准包安装 3)第三方工具安装 这里主要介绍下Go标准包在Ce ...
- NS2网络模拟(5)-homework01.tcl
1: #NS2_有线部分\homework01.tcl 2: 3: #创建两个结点,深圳到北京的TCP连接,图形将数据显示出来,计算吞吐率,画图分析 4: #tcp上层用ftp 5: #udp上层用c ...
- Android在putString和getString使用方法
函数: putString(String key,String value); 功能:将键为key的值为value. 详细的容器详细考虑比如对于Editor 的对象来讲: 代码例如以下: 首先新建一个 ...
- Get and Post(Unity3D六个发展)
猴子原创,欢迎转载. 转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢! 原文地址: http://www.cocos2dev.com/?p=565 unity3d中的www ...
- 使用 Capistrano 和写作 Ruby 迭代边缘部署
想边自己写ruby代码,边部署随时能够到处查看,heroku域名又不友好,速度在国内又慢.于是乎想起来capistrano,于是学起 ... capistrano 一点入门认知 https://www ...
- BZOJ 3594 Scoi2014 方波波麦田 树阵
标题效果:给定一个序列,能够选择k次每个部分的数量和在范围内+1,寻求操作后LIS最大值 我的做法是不是一个标准的解决方案. ..5E为什么跑飞的复杂性. . . 首先,显而易见的结论是,我们选择k右 ...
- Why aren't more desktop apps written with Qt?(quora.com系列文章)
As far as I know and have understood in my experience with Qt, it's a very good and easy to learn li ...