springcloud-provider-consumer-register
作者:纯洁的微笑
出处:http://www.ityouknow.com/
版权归作者所有,转载请注明出处
上一篇文章我们介绍了eureka服务注册中心的搭建,这篇文章介绍一下如何使用eureka服务注册中心,搭建一个简单的服务端注册服务,客户端去调用服务使用的案例。
案例中有三个角色:服务注册中心、服务提供者、服务消费者,其中服务注册中心就是我们上一篇的eureka单机版启动既可,流程是首先启动注册中心,服务提供者生产服务并注册到服务中心中,消费者从服务中心中获取服务并执行。
服务提供
我们假设服务提供者有一个hello方法,可以根据传入的参数,提供输出“hello xxx,this is first messge”的服务
1、pom包配置
创建一个springboot项目,pom.xml中添加如下配置:
<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
2、配置文件
application.properties配置如下:
spring.application.name=spring-cloud-producer
server.port=9000 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
参数在上一篇都已经解释过,这里不多说。
3、启动类
启动类中添加@EnableDiscoveryClient
注解
@SpringBootApplication
@EnableDiscoveryClient
public class ProducerApplication { public static void main(String[] args) { SpringApplication.run(ProducerApplication.class, args); } }
4、controller
提供hello服务
@RestController
public class HelloController { @RequestMapping("/hello") public String index(@RequestParam String name) { return "hello "+name+",this is first messge"; } }
添加@EnableDiscoveryClient
注解后,项目就具有了服务注册的功能。启动工程后,就可以在注册中心的页面看到SPRING-CLOUD-PRODUCER服务。
到此服务提供者配置就完成了。
服务调用
1、pom包配置
和服务提供者一致
<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
2、配置文件
application.properties配置如下:
spring.application.name=spring-cloud-consumer
server.port=9001 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
3、启动类
启动类添加@EnableDiscoveryClient
和@EnableFeignClients
注解。
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
@EnableDiscoveryClient
:启用服务注册与发现@EnableFeignClients
:启用feign进行远程调用
Feign是一个声明式Web Service客户端。使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。
4、feign调用实现
@FeignClient(name= "spring-cloud-producer") public interface HelloRemote { @RequestMapping(value = "/hello") public String hello(@RequestParam(value = "name") String name); }
- name:远程服务名,及spring.application.name配置的名称
此类中的方法和远程服务中contoller中的方法名和参数需保持一致。
5、web层调用远程服务
将HelloRemote注入到controller层,像普通方法一样去调用即可。
@RestController
public class ConsumerController { @Autowired HelloRemote HelloRemote; @RequestMapping("/hello/{name}") public String index(@PathVariable("name") String name) { return HelloRemote.hello(name); } }
到此,最简单的一个服务注册与调用的例子就完成了。
测试
简单调用
依次启动spring-cloud-eureka、spring-cloud-producer、spring-cloud-consumer三个项目
先输入:http://localhost:9000/hello?name=neo
检查spring-cloud-producer服务是否正常
返回:hello neo,this is first messge
说明spring-cloud-producer正常启动,提供的服务也正常。
浏览器中输入:http://localhost:9001/hello/neo
返回:hello neo,this is first messge
说明客户端已经成功的通过feign调用了远程服务hello,并且将结果返回到了浏览器。
负载均衡
以上面spring-cloud-producer为例子修改,将其中的controller改动如下:
@RestController
public class HelloController { @RequestMapping("/hello") public String index(@RequestParam String name) { return "hello "+name+",this is producer 2 send first messge"; } }
在配置文件中改动端口:
spring.application.name=spring-cloud-producer
server.port=9003 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
打包启动后,在eureka就会发现两个服务提供者,如下图:
然后在浏览器再次输入:http://localhost:9001/hello/neo
进行测试:
第一次返回结果:hello neo,this is first messge
第二次返回结果:hello neo,this is producer 2 send first messge
不断的进行测试下去会发现两种结果交替出现,说明两个服务中心自动提供了服务均衡负载的功能。如果我们将服务提供者的数量在提高为N个,测试结果一样,请求会自动轮询到每个服务端来处理。
示例代码
转载于:https://www.cnblogs.com/honger/p/6891410.html
springcloud-provider-consumer-register相关推荐
- dubbo(provider,consumer)点到点直连配置
2019独角兽企业重金招聘Python工程师标准>>> dubbo(provider,consumer)点到点直连配置 博客分类: dubbo 1.服务端接口配置(providr样例 ...
- SpringCloud 2020版本教程3:使用sentinel作为熔断器
点击关注公众号,Java干货及时送达 什么是sentinel Sentinel,中文翻译为哨兵,是为微服务提供流量控制.熔断降级的功能,它和Hystrix提供的功能一样,可以有效的解决微服务调用产生的 ...
- springcloud RestTemplate操作 笔记
0 环境 系统环境: win10 编辑器: IDEA 1 简介 RestTemplate spring3.0开始支持 Http请求工具 该工具与springboot或springcloud无关 提供常 ...
- 第六篇:微服务框架(SpringBoot、SpringCloud)
目录 一. 微服务框架 1. 微服务架构概念 2. 微服务的利与弊(为什么要用微服务) 二. SpringBoot 1. SpringBoot是什么? 2. SpringBoot核心注解是什么? 3. ...
- SpringCloud 2020版本教程4:使用spring cloud sleuth+zipkin实现链路追踪
点击关注公众号,Java干货及时送达 Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案,并且兼容支持了 zipkin,你只需要在pom文件中引入相应的依赖即可. 微服 ...
- Laravel Service Provider 概念详解
https://learnku.com/articles/6189/laravel-service-provider-detailed-concept-收藏一下 我们知道, Container 有很多 ...
- redis 集群 实操 (史上最全、5w字长文)
文章很长,建议收藏起来慢慢读! 总目录 博客园版 为大家准备了更多的好文章!!!! 推荐:尼恩Java面试宝典(持续更新 + 史上最全 + 面试必备)具体详情,请点击此链接 尼恩Java面试宝典,34 ...
- 2020最新java面试题库(杭州)
目 录 1.常见的集合有哪些?都有什么区别: 1 2.HashMap的底层原理: 1 3.sleep和wait的区别 1 4.run方法和start方法的区别: 1 5.Threadlocad的作用: ...
- Http请求之优雅的RestTemplate
前言 本篇博客为对RestTemplate总结 HttpURLConnection 在讲RestTemplate之前我们来看看再没有RestTemplate之前是怎么发送http请求的. privat ...
- dubbo学习过程、使用经验分享及实现原理简单介绍
一.前言 部门去年年中开始各种改造,第一步是模块服务化,这边初选dubbo试用在一些非重要模块上,慢慢引入到一些稍微重要的功能上,半年时间,学习过程及线上使用遇到的些问题在此总结下. 整理这篇文章差不 ...
最新文章
- 3438亿美元!互联网内容产业新机会
- 《松本行弘的程序世界》中文版原作者序
- Facebook开源内存数据库Beringei,追求极致压缩率
- Jerry Wang的微信公众号开发系列文章
- hdu 1257最少拦截系统(贪心)
- Python-OpenCV快速教程
- Activity生命周期Android,横屏切换不重新创建Activity, Activity的四种launchMode
- ubuntu基础和来源
- 9.企业应用架构模式 --- 领域逻辑模式
- C#:在u3d中操作sqlite的数据库
- 湖南省长沙市谷歌高清卫星地图下载
- 盘点10款逆天级效率工具,能帮创业公司节省50%时间成本
- jconsole使用
- 01- SA8155P QNX LA/LV 启动(01) - startup
- 极飞C2000分析报告
- IT行业招聘技巧--JD分析篇
- .dll反编译工具Reflector的使用
- ZYNQ RapidIO IP核协议与使用
- 如何贴计算机屏幕膜,怎么把电脑显示器上的帖膜去掉
- autojs调用java识字,在js中,用auto.js实现一个按键精灵。
热门文章
- [蓝桥杯]试题 基础练习 高精度加法
- [leetcode]62. 不同路径
- C#基础12.1:Object类
- 牛客网暑期ACM多校训练营(第三场): E. Sort String(KMP)
- bzoj 3611: [Heoi2014]大工程(虚树+树形DP)
- 莫比乌斯反演(bzoj 2301: [HAOI2011]Problem b)
- C++文件读写 ifstream ofstream 完成复制文件功能
- [paper reading] Faster RCNN
- js排序算法详解-归并排序
- 安卓蓝牙bluetooth开发全解