SpringCloud客户端的负载均衡Ribbon的实现

网友投稿 221 2023-01-28


SpringCloud客户端的负载均衡Ribbon的实现

前言:微服务架构,不可避免的存在单个微服务有多个实例,那么客户端如何将请求分摊到多个微服务的实例上呢?这里我们就需要使用负载均衡了

一、Ribbon简介

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

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

而sjpjl在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

xmlns:xsi="http://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">

4.0.0

com.cn

eureka-ribbon-server

1.0-SNAPSHOT

UTF-8

1.8

org.springframework.boot

spring-boot-starter-parent

1.5.13.RELEASE

org.springframework.

org.springframework.cloud

spring-cloud-starter-eureka-server

org.springframework.cloud

spring-cloud-dependencies

Edgware.SR3

pom

import

org.springframework.boot

spring-boot-maven-plugin

xmlns:xsi="http://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">

4.0.0

com.cn

eureka-ribbon-server

1.0-SNAPSHOT

UTF-8

1.8

org.springframework.boot

spring-boot-starter-parent

1.5.13.RELEASE

org.springframework.

org.springframework.cloud

spring-cloud-starter-eureka-server

org.springframework.cloud

spring-cloud-dependencies

Edgware.SR3

pom

import

org.springframework.boot

spring-boot-maven-plugin

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

xmlns:xsi="http://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">

4.0.0

com.cn

eureka-ribbon-client

1.0-SNAPSHOT

UTF-8

1.8

org.springframework.boot

spring-boot-starter-parent

1.5.13.RELEASE

org.springframework.cloud

spring-cloud-starter-ribbon

org.springframework.cloud

spring-cloud-starter-eureka

org.springframework.cloud

spring-cloud-dependencies

Edgware.SR3

pom

import

org.springframework.boot

spring-boot-maven-plugin

xmlns:xsi="http://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">

4.0.0

com.cn

eureka-ribbon-client

1.0-SNAPSHOT

UTF-8

1.8

org.springframework.boot

spring-boot-starter-parent

1.5.13.RELEASE

org.springframework.cloud

spring-cloud-starter-ribbon

org.springframework.cloud

spring-cloud-starter-eureka

org.springframework.cloud

spring-cloud-dependencies

Edgware.SR3

pom

import

org.springframework.boot

spring-boot-maven-plugin

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

sjpjl

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/),如下:

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

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

至此,Ribbon已经入门,是不是很简单,但是这只是最简单的应用,九牛一毛尔...学无止境乎!

示例代码: https://gitee.com/lfalex/springcloud-example


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java虚拟机内存溢出及泄漏实例
下一篇:共享文件系统图片大全(电脑共享图片)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~