###################使用默认的负载均衡(轮询)#############################

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.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>eureka-client-ribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-client-ribbon</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-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</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: 8665
user:
userServicePath: http://localhost:8663/user/ spring:
application:
name: eureka-client-ribbon
eureka:
instance:
hostname: localhost
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:8661/eureka

3、对RestTemplate添加注解

package com.test.eurekaclientribbon;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
@EnableEurekaClient
public class EurekaClientRibbonApplication { @Bean
@LoadBalanced //使restTemplate具备负载均衡的作用
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(EurekaClientRibbonApplication.class, args);
} }

4、使用restTemplate对象远程调用服务

package com.test.eurekaclientribbon.controller;

import com.test.eurekaclientribbon.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; /**
* Ribbon使用步骤:
* 全局配置
* 1、添加依赖
* 2、配置application.yml文件
* 3、对启动类中的RestTemplate添加@LoadBalanced注解,使得RestTemplate对象具备负载均衡能力
* 4、请求路径为:被调用方的地址,但必须将被调用方的ip地址改为virtualIp
* 例如 restTemplate.getForObject("http://eureka-client-user/user/"+id, User.class);
* 被调用方URL:http://192.168.137.1:8663/user/
* 现为:http://eureka-client-user/user/
*/
@RestController
public class MovieController {
@Autowired
private RestTemplate restTemplate; @Value("${user.userServicePath}")
private String userServicePath; /* @Autowired
private LoadBalancerClient loadBalancerClient;*/ @GetMapping("/movie/{id}")
public User findById(@PathVariable Long id) {
//请用虚拟ip
return restTemplate.getForObject("http://eureka-client-user/user/"+id, User.class);
} /**
* 配置单一的方法进行轮询
* @return
*/
/* @GetMapping("/test")
public String test() {
//请用虚拟ip
ServiceInstance serviceInstance =loadBalancerClient.choose("eureka-client-user");
System.out.println(serviceInstance.getHost()+":"+serviceInstance.getPort());
return "1";
}*/
}

5、访问

http://localhost:8661/  #查看eureka
http://192.168.137.1:8663/user/2 #确保自己调用可以访问
http://192.168.137.1:8665/movie/4 #远程调用测试

###################配置对单一的方法进行轮询(使用loadBalancerClient)########################

1、针对第一个步骤中的(4),进行修改

package com.test.eurekaclientribbon.controller;

import com.test.eurekaclientribbon.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; /**
* Ribbon使用步骤:
* 全局配置
* 1、添加依赖
* 2、配置application.yml文件
* 3、对启动类中的RestTemplate添加@LoadBalanced注解,使得RestTemplate对象具备负载均衡能力
* 4、请求路径为:被调用方的地址,但必须将被调用方的ip地址改为virtualIp
* 例如 restTemplate.getForObject("http://eureka-client-user/user/"+id, User.class);
* 被调用方URL:http://192.168.137.1:8663/user/
* 现为:http://eureka-client-user/user/
*/
@RestController
public class MovieController {
/* @Autowired
private RestTemplate restTemplate;
*/
@Value("${user.userServicePath}")
private String userServicePath; @Autowired
private LoadBalancerClient loadBalancerClient; /* @GetMapping("/movie/{id}")
public User findById(@PathVariable Long id) {
//请用虚拟ip
return restTemplate.getForObject("http://eureka-client-user/user/"+id, User.class);
}*/ /**
* 配置单一的方法进行轮询
* @return
*/
@GetMapping("/test")
public String test() {
//请用虚拟ip
ServiceInstance serviceInstance =loadBalancerClient.choose("eureka-client-user");
System.out.println(serviceInstance.getHost()+":"+serviceInstance.getPort());
return "1";
}
}

###################全局修改负载均衡方式########################

1、针对(2)application.yml文件添加下面配置

server:
port: 8665
user:
userServicePath: http://localhost:8663/user/ spring:
application:
name: eureka-client-ribbon
eureka:
instance:
hostname: localhost
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:8661/eureka
eureka-client-user: #远程服务虚拟主机名
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule #随机方式
#可以通过IRule类查看负载均衡方式
#RoundRobinRule 轮询
#RandomRule 随机

###################编写Configuration文件指定负载均衡方式########################

1、创建一个配置类

package com.test.eurekaclientribbon.config;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class RibbonConfig {
@Bean
public IRule ribbonRule() {
return new RandomRule();
}
}

2、在启动类上添加注解

package com.test.eurekaclientribbon;

import com.test.eurekaclientribbon.config.RibbonConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "eureka-client-user",configuration = RibbonConfig.class) //表示eureka-client-user主机使用这个规则
public class EurekaClientRibbonApplication { @Bean
@LoadBalanced //使restTemplate具备负载均衡的作用
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(EurekaClientRibbonApplication.class, args);
} }

第六章 SpringCloud之Ribbon负载均衡的更多相关文章

  1. spring-cloud配置ribbon负载均衡

    spring-cloud配置ribbon负载均衡 ribbon提供的负载均衡就是开箱即用的,简单的不能再简单了 为了顺利演示此demo,你需要如下 需要提前配置eureka服务端,具体看 https: ...

  2. SpringCloud系列——Ribbon 负载均衡

    前言 Ribbon是一个客户端负载均衡器,它提供了对HTTP和TCP客户端的行为的大量控制.我们在上篇(猛戳:SpringCloud系列——Feign 服务调用)已经实现了多个服务之间的Feign调用 ...

  3. 浅谈SpringCloud (三) Ribbon负载均衡

    什么是负载均衡 当一台服务器的单位时间内的访问量越大时,服务器压力就越大,大到超过自身承受能力时,服务器就会崩溃.为了避免服务器崩溃,让用户有更好的体验,我们通过负载均衡的方式来分担服务器压力. 我们 ...

  4. SpringCloud:Ribbon负载均衡

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

  5. 四(2)、springcloud之Ribbon负载均衡

    2.Ribbon负载均衡 ​ Ribbon在工作时分成两步第一步先选择 EurekaServer ,它优先选择在同一个区域内负载较少的server. 第二步再根据用户指定的策略,在从server取到的 ...

  6. Spring-cloud之Ribbon负载均衡的使用及负载均衡策略配置(与Eurka配合使用)

    什么是Ribbon,ribbon有什么用,个人先总结一下(不正确请提出讨论):Ribbon是基于客户端的负载均衡器,为我们提供了多样的负载均衡的方案,比如轮询,最小的并发请求的server,随机ser ...

  7. SpringCloud之Ribbon负载均衡及Feign消费者调用服务

    目的: 微服务调用Ribbon Ribbon负载均衡 Feign简介及应用 微服务调用Ribbon Ribbon简介 1. 负载均衡框架,支持可插拔式的负载均衡规则 2. 支持多种协议,如HTTP.U ...

  8. SpringCloud之Ribbon负载均衡配置

    一.负载均衡解决方案分类及特征 业界主流的负载均衡解决方案有: 1.1 集中式负载均衡 即在客户端和服务端之间使用独立的负载均衡设施(可以是硬件,如F5, 也可以是软件,如nginx), 由该设施负责 ...

  9. SpringCloud学习笔记(六):Feign+Ribbon负载均衡

    简介 官网解释: http://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-feign Feign是一个声明式WebS ...

随机推荐

  1. 2019.9.19HTML基础

    html:超文本标记语言,不是编程语言,是标签语言,显示数据. 有双标签和单标签 双标签:有开始有结束,<body></body> 单标签:只有一个.<img src=# ...

  2. 解决Redis中文乱码问题

    启动客户端的时候添加 --raw 选项即可 wangyulong@code-local:~$ redis-cli 127.0.0.1:6379> set key1 '上海' OK 127.0.0 ...

  3. zabbix 邮件报警事件:Zabbix discoverer processes more than 75% busy

    Problem has been resolved at :: on Problem name: Zabbix discoverer processes more than % busy Host: ...

  4. idea运行web项目乱码

    windows下idea中web项目乱码,主要原因是服务器端乱码(执行webservlet的时候,编码格式改变),导致客户端的编码格式与webservlet传递过的编码格式不一致. 前端网页的编码,通 ...

  5. super运行错误解决方法

    自己实践: 要是下面的不成功,可能的原因是: 目录/var/log/supervisor//var/log/supervisor/ /var/log/supervisor/ /var/log/supe ...

  6. oracle order by 自定义

    我们通常需要根据客户需求对于查询出来的结果给客户提供自定义的排序方式,那么我们通常sql需要实现方式都有哪些,参考更多资料总结如下(不完善的和错误望大家指出): 一.如果我们只是对于在某个程序中的应用 ...

  7. 基于Redis实现分布式锁(转载)

    原文地址:http://blog.csdn.net/ugg/article/details/41894947 Redis命令介绍使用Redis实现分布式锁,有两个重要函数需要介绍 SETNX命令(SE ...

  8. mongdb的优势和不足

    l  面向文档的数据库. l  一个介于关系型数据库和非关系型数据库之间的产品,是非关系系数据库中功能最丰富,最像关系型数据库的. l  特征是模式自由,schema-free.无需定义表结构. l  ...

  9. mysql细说show slave status参数详解(最全)

    1. Slave_IO_State 这里显示了当前slave I/O线程的状态(slave连接到master的状态).状态信息和使用show processlist | grep "syst ...

  10. vue cli3.0快速搭建项目详解(强烈推荐)

    这篇文章主要介绍下vue-cli3.0项目搭建,项目结构和配置等整理一下,分享给大家. 一.介绍 Vue CLI 是一个基于 Vue.js 进行快速开发的完整系统.有三个组件: CLI:@vue/cl ...