SpringCloud(三):服务消费以及负载均衡(RestTemplate+Ribbon)
一、什么是Ribbon:
Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法。默认是使用轮询。
方法1:可以在springboot启动类之外定义一个配置类(@RibbonClient)配置对应的算法。
方法2:在配置文件yml中配置
相关链接:https://www.cnblogs.com/fengyuduke/p/10712569.html
怎么使用Ribbon实现负载均衡?
相关链接: https://www.cnblogs.com/xing-12/p/9889153.html
1.你要有两个服务,一个是服务消费方,一个是服务提供方,并且服务提供方要有两个实例,也就是xing-user有两个实例,分别运行在8070和8071端口。
2.所有服务注册到eureka server中。
3.通过注入bean: 用RestTemplate调用restTemplate.getForObject("http://xing-user/user/findByName/"+name, User.class)方法。
二、Eureka服务提供者集群
先启动上篇文章中的注册中心eureka-server:8001, 以及hello-service:8011,接着修改hello-service项目配置文件中的端口,改成8012,再次运行用户服务项目。
执行成功,查看Eureka注册中心,可以看到有用户服务应用有两个端口对应。
(如果是IDEA用户,需要修改Application.java的执行方式,默认是单实例运行,所以你在运行8011的项目,修改端口无法再次运行。)
三、RestTemplate+Ribbon消费者: 新建一个maven服务
pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
application.properties:
spring.application.name=hello-consumer-ribbon
server.port=8021
eureka.client.serviceUrl.defaultZone=http://localhost:8001/eureka/spring.application.name=hello-consumer-ribbon
server.port=8021
eureka.client.serviceUrl.defaultZone=http://localhost:8001/eureka/
启动类:
package cn.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
@EnableDiscoveryClient
@RestController
public class HelloConsumerRibbonApplication { public static void main(String[] args) {
SpringApplication.run(HelloConsumerRibbonApplication.class, args);
} @Bean
@LoadBalanced
public RestTemplate restTemplate () {
return new RestTemplate();
} @Autowired
private RestTemplate restTemplate; //获取服务实例,作用为之后console显示效果;"http://USER-SERVICE/hello?name= 标识注册的服务。USER-SERVICE在eureka注册的服务名
@RequestMapping("hello")
public ResponseEntity<String> hello (String name) {
return restTemplate.getForEntity("http://USER-SERVICE/hello?name=" + name, String.class);
} }
四、测试
测试服务消费
http://localhost:8021/hello?name=ribbon
测试负载均衡:
我们在hello-service hello方法中加上日志打印,然后再分别启动 hello-service:8012,8002,然后多次访问 http://localhost:8021/hello?name=ribbon,可以看到两个hello-service项目的控制台都有日志输出,表示实现了负载均衡。
使用RestTemplate+Ribbon必须指定调用服务名称,如上面的HELLO-SERVICE,为了方便使用,SpringCloud还集成了Feign消费方式。
————————————————
SpringCloud(三):服务消费以及负载均衡(RestTemplate+Ribbon)的更多相关文章
- Spring Cloud系列(三):服务消费与负载均衡
上一篇介绍了服务提供者,有了注册中心和服务提供者,我们就可以进行服务消费了.Spring Cloud可以通过RestTemplate+Ribbon和Feign这两种方式消费服务. 我们仍然在上一篇的项 ...
- Spring Cloud ---- 服务消费与负载均衡(Rest + Ribbon )
上一篇主要写了基于Eurake的服务的注册,主要就是创建注册中心,创建服务者,将服务者注册到注册中心,完成服务的暴露.这一篇主要写服务的消费与服务消费的负载均衡. 服务的调用方式有两种,Rest + ...
- spring cloud(服务消费者(利用ribbon实现服务消费及负载均衡)——初学二)
Ribbon是一个基于HTTP和TCP客户端的负载均衡器,利用ribbon实现服务消费,并实现客户端的负载均衡. 一.准备工作(利用上一节的内容) 启动服务注册中心 启动computer-servic ...
- 微服务SpringCloud之服务调用与负载均衡
上一篇我们学习了服务的注册与发现,本篇博客是在上一篇的基础上学习服务的调用.上一博客主要创建了Eureka的服务端和一个Client,该Client包含了一个Controller用来提供对外服务供外部 ...
- Spring Cloud ---- 服务消费与负载均衡(feign)
feign是一个声明式的伪客户端,只需要创建一个接口并且注解,它具有可插拔的特性.feign集合了Ribbon,再与Eurake结合实现服务的注册发现与负载均衡.结合Hystrix,具有熔断功能. 1 ...
- spring cloud(服务消费者(利用feign实现服务消费及负载均衡)——初学三)
Feign是一个声明式的Web Service客户端,我们只需要使用Feign来创建一个接口并用注解来配置它既可完成. 它具备可插拔的注解支持,包括Feign注解和JAX-RS注解.Feign也支持可 ...
- 学习一下 SpringCloud (三)-- 服务调用、负载均衡 Ribbon、OpenFeign
(1) 相关博文地址: 学习一下 SpringCloud (一)-- 从单体架构到微服务架构.代码拆分(maven 聚合): https://www.cnblogs.com/l-y-h/p/14105 ...
- spring cloud --- Ribbon 客户端负载均衡 + RestTemplate ---心得【无熔断器】
spring boot 1.5.9.RELEASE spring cloud Dalston.SR1 1.前言 了解了 eureka 服务注册与发现 的3大角色 ,会使用RestTem ...
- spring cloud --- Ribbon 客户端负载均衡 + RestTemplate + Hystrix 熔断器 [服务保护] ---心得
spring boot 1.5.9.RELEASE spring cloud Dalston.SR1 1.前言 当超大并发量并发访问一个服务接口时,服务器会崩溃 ,不仅导致这个接口无法 ...
随机推荐
- React组件略讲
React是前端组件化开发的开山鼻祖,这种开发方式彻底解决了的前端组件复用的痛点.今天,就来研究一下React组件开发. 前端同学一般都会从Vue入门,因为Vue使用的<template> ...
- CentOS 7 Cobbler 安装
Cobbler介绍 Cobbler是一个Linux服务器快速网络安装的服务,而且在经过调整也可以支持网络安装windows. 使用python开发,小巧轻便(才15k行python代码),可以通过网络 ...
- react修改端口
react修改端口 在react官网根据文档安装好项目之后,发现新项目没有了scripst文件夹 之前版本是在scripts文件夹中的starts.js中修改 新版本修改port发现移入到了依赖里面 ...
- 系统默认的alert弹出框总会带有域名
最近在开发Hybrid APP时发现用系统默认的alert弹出框总会带有域名,用户体验就比较不好了.想了一种办法来解决就是覆盖alert的方法. (function(){ window.a ...
- ViewGroup dispatchTouchEvent方法中 mFirstTouchTarget标志是否为空的含义
在ViewGroup dispatchTouchEvent方法中首次出现mFirstTouchTarget的语句为: if (actionMasked == MotionEvent.ACTION_DO ...
- Python基础之第三方库gevent安装
安装gevent库: 想要安装gevent库,我们需要确定pip版本: 使用 pip3 list: 我们可以发现pip版本为19.3.1,如果你们的pip版本不是最新版可以使用命令python -m ...
- 成功build Maven但eclipse中依然显示该工程有错误
在mac pro的控制台中,成功执行 mvn package,但该工程在eclipse仍然显示有错误. 解决办法:右键该工程 -> Maven -> Update project,勾选 F ...
- DataFrame 链式赋值
在运行以下Python代码时,Pandas抛出SettingWithCopyWarning警告: row_data = df_pred.loc[key] row_data['col'] = new_v ...
- LNMP架构的搭建
第9章 LNMP架构的搭建 9.1 什么是LNMP 9.1.1 LNMP的组成 L linux N nginx:实现静态的服务处理 M ...
- ARTS-S centos查看端口被哪个进程占用
netstat -tunlp | grep 80 或者 lsof -i:80