温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在SpringCloud客户端中实现Ribbon负载均衡

发布时间:2021-05-27 17:12:11 来源:亿速云 阅读:143 作者:Leah 栏目:编程语言

本篇文章为大家展示了怎么在SpringCloud客户端中实现Ribbon负载均衡,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

一、Ribbon简介

Ribbon是Netflix发布的负载均衡器,它有助于控制HTTP和TCP客户端的行为。为Ribbon配置服务提供者地址列表后,Ribbon就可基于某种负载均衡算法,自动地帮助服务消费者去请求。Ribbon默认为我们提供了很多的负载均衡算法,例如:轮询,随机等,也可自定义;

Ribbon的GitHub: https://github.com/Netflix/ribbon

而在SpringCloud中使用Ribbon和Eureka时,Ribbon会自动从EurekaServer中获取服务提供者地址列表,并基于负载均衡算法。

二、Ribbon实战

1、创建EurekaServer,EurekaClient1,EurekaClient2,之前已经说过了Eureka的使用,这里直接上代码:

EurekaServer:

ServerApplication.java

@SpringBootApplication
@EnableEurekaServer
public class ServerApplication {

  public static void main(String[] args) {
    SpringApplication.run(ServerApplication.class,args);
  }

}

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>

 <groupId>com.cn</groupId>
 <artifactId>eureka-ribbon-server</artifactId>
 <version>1.0-SNAPSHOT</version>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <java.version>1.8</java.version>
 </properties>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.13.RELEASE</version>
 </parent>

 <dependencies>
  <dependency>
   <groupId>org.springframework.</groupId>
   <artifactId></artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka-server</artifactId>
  </dependency>
 </dependencies>
 <dependencyManagement>
  <dependencies>
   <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>Edgware.SR3</version>
    <type>pom</type>
    <scope>import</scope>
   </dependency>
  </dependencies>
 </dependencyManagement>
 <!-- 添加spring-boot的maven插件 -->
 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>
</project>

application.properties

server.port=8761
#注意:这两个配置eureka默认为true,要改成false,否则会报错,connot connect server
#表示是否将自己注册在EurekaServer上
eureka.client.register-with-eureka=false
#表示是否从EurekaServer获取注册信息
eureka.client.fetch-registry=false

eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

EurekaClient1:

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>

 <groupId>com.cn</groupId>
 <artifactId>eureka-ribbon-client</artifactId>
 <version>1.0-SNAPSHOT</version>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <java.version>1.8</java.version>
 </properties>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.13.RELEASE</version>
 </parent>

 <dependencies>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-ribbon</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka</artifactId>
  </dependency>
 </dependencies>
 <dependencyManagement>
  <dependencies>
   <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>Edgware.SR3</version>
    <type>pom</type>
    <scope>import</scope>
   </dependency>
  </dependencies>
 </dependencyManagement>
 <!-- 添加spring-boot的maven插件 -->
 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>
</project>
server.port=8762
spring.application.name=client-8762
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

而在启动类中加入RestTemplate远程调用实例到容器中,并且 添加LoadBalanced注解,使RestTemplate具备负载均衡的能力 :

@SpringBootApplication
@EnableDiscoveryClient
public class ClientApplication {

  public static void main(String[] args) {
    SpringApplication.run(ClientApplication.class, args);
  }

  /** 
   * @Description: 加入@LoadBalanced注解,就可以为RestTemplate加入负载均衡的能力 
   * @Param:
   * @return: 
   * @Author: 
   * @Date: 2018/6/15 
   */ 
  @Bean
  @LoadBalanced
  public RestTemplate getRestTemplate() {
    return new RestTemplate();
  }

}

创建Controller,注入RestTemplate、LoadBalancerClient实例:

package com.cn.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

/**
 * @program: springcloud-example
 * @description:
 * @author: 
 * @create: 2018-06-15 15:55
 **/
@Controller
public class RibbonController {

  @Autowired
  private LoadBalancerClient loadBalancerClient;

  @Autowired
  private RestTemplate restTemplate;

  @GetMapping("/loadInstance")
  @ResponseBody
  public String loadInstance() {
    ServiceInstance choose = this.loadBalancerClient.choose("client-87");
    System.out.println(choose.getServiceId()+":"+choose.getHost()+":"+choose.getPort());
    return choose.getServiceId() + ":" + choose.getHost() + ":" + choose.getPort();
  }

}
package com.cn;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @program: springcloud-example
 * @description:
 * @author: 535504
 * @create: 2018-06-15 16:05
 **/
@SpringBootApplication
@EnableDiscoveryClient
public class ClientApplication {

  public static void main(String[] args) {
    SpringApplication.run(ClientApplication.class, args);
  }

}

EurekaClient2:

pom.xml与EurekaClient1中一致

application.xml:

server.port=8763
spring.application.name=client-87
eureka.client.service-url.defaultZone=http://localhost:8761/eureka

ClientController.java:

package com.cn.contorller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @program: springcloud-example
 * @description:
 * @author: 
 * @create: 2018-06-15 16:12
 **/
@Controller
public class ClientController {

  @GetMapping("/getUser")
  @ResponseBody
  public String getUser() {
    System.out.println("获取用户成功");
    return "获取用户成功";
  }

}

2、启动顺序:

①、依次启动EurekaServer =》 EurekaClient1 =》 EurekaClient2   ;

②、然后将EurekaClient2中的application.properties的server.port=8763改为server.port=8764,再次启动该项目;

③、打开EurekaServer的配置页面(http://localhost:8761/),如下:

怎么在SpringCloud客户端中实现Ribbon负载均衡

④、我们在地址栏输入http://localhost:8762/loadInstance,多刷新几次,会发现每次调用的端口实例都不同,如下图:

怎么在SpringCloud客户端中实现Ribbon负载均衡

怎么在SpringCloud客户端中实现Ribbon负载均衡

⑤、我们在看控制台,如图:

怎么在SpringCloud客户端中实现Ribbon负载均衡

上述内容就是怎么在SpringCloud客户端中实现Ribbon负载均衡,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI