创建服务消费者(Ribbon)
概述
在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于 http restful 的。Spring cloud 有两种服务调用方式,一种是 ribbon + restTemplate,另一种是 feign。在这一篇文章首先讲解下基于 ribbon + rest。
Ribbon简介
Ribbon 是一个负载均衡客户端,可以很好的控制
http和tcp的一些行为。
准备工作
启动服务提供者
启动Eureka注册中心
创建服务消费者(POM)
<!--spring cloud starter ribbon-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<!--spring cloud starter eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Application
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaRibbonClientApplication { //使用注解@LoadBalanced标记RestTemplate
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(EurekaRibbonClientApplication.class, args);
}
}
application.yml
server:
port: 9002
spring:
application:
name: eureka-client-ribbon
eureka:
client:
serviceUrl:
defaultZone: http://localhost:1111/eureka/
instance:
lease-renewal-interval-in-seconds: 10 #服务续约
lease-expiration-duration-in-seconds: 15 #服务剔除>服务续约时间
创建测试的Controller
import lombok.extern.slf4j.Slf4j;
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.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
@RestController
@RequestMapping("/api")
@Slf4j
public class RibbonController {
@Autowired
private RestTemplate restTemplate;
//loadbalanced客户端
@Autowired
private LoadBalancerClient loadBalancerClient;
@GetMapping("/ribbon/hello")
public String hello() {
String result = restTemplate.getForObject("http://eureka-provider/hello",String.class);
return result;
}
}
在IDEA中配置一个工厂启动多个实例
步骤一
点击 Run -> Edit Configurations...

#步骤二
选择需要启动多实例的项目并去掉 Single instance only 前面的勾

#步骤三
通过修改 application.yml 配置文件的 server.port 的端口,启动多个实例,需要多个端口,分别进行启动即可。
创建服务消费者(Ribbon)的更多相关文章
- springcloud-Netflix创建服务消费者
目录 springcloud-Netflix创建服务消费者 Ribbon 创建服务消费者-Ribbon方式 ribbon的架构 Feign 创建包和基本项目结构 创建Feign访问服务的接口和访问co ...
- 创建服务消费者(Feign)
概述 Feign 是一个声明式的伪 Http 客户端,它使得写 Http 客户端变得更简单.使用 Feign,只需要创建一个接口并注解.它具有可插拔的注解特性,可使用 Feign 注解和 JAX-RS ...
- SpringCloud-创建服务消费者-Ribbon方式(附代码下载)
场景 SpringCloud-服务注册与实现-Eureka创建服务注册中心(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...
- 7、服务发现&服务消费者Ribbon
公众号: java乐园 在<服务注册&服务提供者>这一篇可能学习了这么开发一个服务提供者,在生成上服务提供者通常是部署在内网上,即是服务提供者所在的服务器是与互联网完全隔离的.这篇 ...
- Spring Cloud (3) 服务消费者-Ribbon
在上一篇中使用LoadBalancerClient接口实现了获取某个服务的具体实例,并根据实例信息发起服务接口消费请求.但是这样的做法需要我们手工的区编写服务选取.连接拼接等繁琐的工作,对于开发人员来 ...
- 「 从0到1学习微服务SpringCloud 」04服务消费者Ribbon+RestTemplate
系列文章(更新ing): 「 从0到1学习微服务SpringCloud 」01 一起来学呀! 「 从0到1学习微服务SpringCloud 」02 Eureka服务注册与发现 「 从0到1学习微服务S ...
- springCloud学习-服务消费者(rest+ribbon)
1.ribbon简介 spring cloud的Netflix中提供了两个组件实现软负载均衡调用:ribbon和feign. Ribbon 是一个基于 HTTP 和 TCP 客户端的负载均衡器 它可以 ...
- SpringCloud-使用熔断器防止服务雪崩-Ribbon和Feign方式(附代码下载)
场景 SpringCloud-服务注册与实现-Eureka创建服务注册中心(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...
- SpringCloud-创建服务消费者-Feign方式(附代码下载)
场景 SpringCloud-服务注册与实现-Eureka创建服务注册中心(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...
随机推荐
- android:layout_gravity和android:gravity属性的差别
gravity的中文意思就是"重心",就是表示view横向和纵向的停靠位置 android:gravity:是对view控件本身来说的,是用来设置view本身的文本应该显示在vie ...
- vue 使用ztree
1 全局引入jquery , 不明白的看我上一个的随笔 , 特别简单 2 我没有封装组件 , 项目里面这个效果的只用了一次 , 没有必要 在你的<script>标签下面引入这俩东西 , 前 ...
- HBase 查找版本
直接使用hbase shell命令进入shell时间会告诉版本: 进shell后.关键在version命令.能够查看版本: # hbase shell HBase Shell; enter 'help ...
- so文件成品评论【整理】
这是我的 @布加迪20 AZ在一篇文章中写道:<汉化so文件的心得>中的技术附件做的简洁性整理.原来的看起来不是非常方便.一起分享学习.. 正文 SO文件汉化心得 --By布加迪20 ...
- protobuf反射详解
本文主要介绍protobuf里的反射功能,使用的pb版本为2.6.1,同时为了简洁,对repeated/extension字段的处理方法没有说明. 最初是起源于这样一个问题: 给定一个pb对象,如何自 ...
- 手把手教你启用Win10的Linux子系统(超详细)
原文:手把手教你启用Win10的Linux子系统(超详细) 版权声明:转载请保留出处,谢谢! https://blog.csdn.net/zhangdongren/article/details/82 ...
- 简述WPF中的图像像素格式(PixelFormats)
原文:简述WPF中的图像像素格式(PixelFormats) --------------------------------------------------------------------- ...
- React路由配置
React路由简单配置 //入口文件index.js import React from 'react'; import ReactDom from 'react-dom'; import { Rou ...
- 入骨三分的“我们是谁”IT行业版
这组漫画用简单的台词.粗犷的线条,把很多人心中对甲方(客户)的不满彻底地发泄了一通,并且出现了很多变种版本,引发了传播热潮. http://news.sina.com.cn/c/nd/2017-08- ...
- Myeclipse2014 激活 (包括方法和工具)
课程要求Myeclipse做各种各样的实验,对,当各种插头井.突然Myeclipse提示:使用过期,你可知道按那些个插件收了多少挫折么,怎能刚安好就用不了.可是又不想buy,所以就上网找破解咯,当中发 ...