1.Ribbon概述

实际环境中,我们往往会开启很多个itcast-service-provider的集群。此时我们获取的服务列表中就会有多个,到底该访问哪一个呢?

Eureka中已经帮我们集成了负载均衡组件:Ribbon,简单修改代码即可使用。

2.负载均衡入门案例

(1)启动两个LuckyServiceProviderApplication实例,一个8081,一个8082。

注意:启动第2个Application时需要修改yml文件中的端口配置。

Eureka监控面板:http://localhost:10086/

(2)开启负载均衡

因为Eureka中已经集成了Ribbon,所以我们无需引入新的依赖,直接修改代码。
修改lucky-service-consumer的引导类,在RestTemplate的配置方法上添加`@LoadBalanced`注解:

package lucky.service;

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 LuckyServiceConsumerApplication { @Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(LuckyServiceConsumerApplication.class, args);
} }

修改调用方式,不再手动获取ip和端口,而是直接通过服务名称调用:

package lucky.service.controller;

import lucky.service.domain.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate; import java.util.List; @Controller
@RequestMapping(path = "/consumer/user")
public class UserController {
@Autowired
private RestTemplate restTemplate; // @Autowired
// private DiscoveryClient discoveryClient; // eureka客户端,可以获取到eureka中服务的信息 @RequestMapping(path = "/queryUsersById")
@ResponseBody
public Users queryUserById(@RequestParam("id") Integer id){ /*// 根据服务名称,获取服务实例。有可能是集群,所以是service实例集合
List<ServiceInstance> instances = discoveryClient.getInstances("service-provider");
// 因为只有一个Service-provider。所以获取第一个实例
ServiceInstance instance = instances.get(0);*/ // 获取ip和端口信息,拼接成服务地址
String baseUrl = "http://SERVICE-PROVIDER/users/queryUsersById?id=" + id;
Users user = this.restTemplate.getForObject(baseUrl, Users.class);
return user;
} }

访问页面,查看结果:

008 SpringCloud 学习笔记4-----Ribbon负载均衡的更多相关文章

  1. spring cloud学习笔记二 ribbon负载均衡

    Ribbon是Netflix发布的负载均衡器,它有助于控制HTTP和TCP的客户端的行为.为Ribbon配置服务提供者地址后,Ribbon就可基于某种负载均衡算法,自动地帮助服务消费者去请求.Ribb ...

  2. spring-cloud: eureka之:ribbon负载均衡自定义配置(二)

    spring-cloud: eureka之:ribbon负载均衡自定义配置(二) 有默认配置的话基本上就是轮询接口,现在我们改用自定义配置,同时支持:轮询,随机接口读取 准备工作: 1.eureka服 ...

  3. spring-cloud: eureka之:ribbon负载均衡配置(一)

    spring-cloud: eureka之:ribbon负载均衡配置(一) 比如我有: 一个eureka服务:8761 两个user用户服务: 7900/7901端口 一个movie服务:8010 1 ...

  4. SpringCloud系列五:Ribbon 负载均衡(Ribbon 基本使用、Ribbon 负载均衡、自定义 Ribbon 配置、禁用 Eureka 实现 Ribbon 调用)

    1.概念:Ribbon 负载均衡 2.具体内容 现在所有的服务已经通过了 Eureka 进行了注册,那么使用 Eureka 注册的目的是希望所有的服务都统一归属到 Eureka 之中进 行处理,但是现 ...

  5. SpringCloud学习笔记(2)——Ribbon

    参考SpringCloud官网第16.17章 16. Client Side Load Balancer: Ribbon Ribbon是一个客户端的负载均衡器,它提供对大量的HTTP和TCP客户端的访 ...

  6. SpringCloud与微服务Ⅵ --- Ribbon负载均衡

    一.Ribbon是什么 Sping Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡的工具. 简单的说,Ribbon是Netflix发布的开源项目,主要功能是提供客户 ...

  7. SpringCloud微服务之Ribbon负载均衡(一)

    什么是微服务?什么是SpringCloud? 微服务是一种架构的模式,它提倡将一个应用程序划分成很多个微小的服务,服务与服务之间相互协调.相互配合.每个服务运行都是一个独立的进程,服务与服务之间采用轻 ...

  8. Spring Cloud微服务开发笔记5——Ribbon负载均衡策略规则定制

    上一篇文章单独介绍了Ribbon框架的使用,及其如何实现客户端对服务访问的负载均衡,但只是单独从Ribbon框架实现,没有涉及spring cloud.本文着力介绍Ribbon的负载均衡机制,下一篇文 ...

  9. Web负载均衡学习笔记之实现负载均衡的几种实现方式

    0x00 概要 负载均衡(Load Balance)是集群技术(Cluster)的一种应用.负载均衡可以将工作任务分摊到多个处理单元,从而提高并发处理能力.目前最常见的负载均衡应用是Web负载均衡.根 ...

随机推荐

  1. Python 弹出框代码

      from ctypes import * user32 = windll.LoadLibrary('user32.dll')#调用dll文件 #a是得到弹出框的选择按钮的值 user32.Mess ...

  2. [Java] Spring boot2 整合 Thymeleaf 后 去除模板缓存

    Spring boot2 整合 Thymeleaf 后 去除模板缓存 网上好多文章只是简单粗暴的说,在 application.properties  做如下配置即可: #Thymeleaf cach ...

  3. 简述tcp三次握手

    第一次握手:建立连接时,客户端向服务端发送SYN(同步序列编号),其中包含客户端的初始序号seq(序列号)=x,并进入SYN_SENT(请求连接)状态,等待服务器确认. 第二次握手:服务器收到请求后, ...

  4. OJ笔记

    1.未考虑程序没有输出导致的格式错误: 原代码:(即使没有输出,ans集合元素为0,也输出了空格) set<int>::iterator it=ans.begin(); while(it! ...

  5. luogu P3327 [SDOI2015]约数个数和 莫比乌斯反演

    题面 我的做法基于以下两个公式: \[[n=1]=\sum_{d|n}\mu(d)\] \[\sigma_0(i*j)=\sum_{x|i}\sum_{y|j}[gcd(x,y)=1]\] 其中\(\ ...

  6. nginx 访问控制之deny allow

    Nginx的deny和allow指令是由ngx_http_access_module模块提供,Nginx安装默认内置了该模块. 除非在安装时有指定 --without-http_access_modu ...

  7. go - helloword

    package mainimport "fmt" //一定要双引号func main() {/* test hello word *///test fmt.Println(&quo ...

  8. 划水嘶吼misc

    划水嘶吼misc [题目描述]: 开局一张图,flag全靠编 [题目writeup]: 瞅半天,瞅到二维码 然后用potplayer按帧查找到skr二维码 通过PS自动调整对比度,扫出key1,2,3 ...

  9. yum -y install pip No package pip available. Error: Nothing to do

    centos下安装pip时失败: [root@wfm ~]# yum -y install pipLoaded plugins: fastestmirror, refresh-packagekit, ...

  10. ip rule实现源IP路由,实现一个主机多IP(或多网段)同时通(外部看是完全两个独立IP)

    利用ip rule实现基于源地址区分路由表,实现一个主机多IP网段同时通.(外部的一个主机无论访问哪个网段都可以访问通)实际应用:创建路由表table200ip route add 192.168.1 ...