05-LoadBalancer负载均衡

1、介绍

目前主流的负载方案分为以下两种:

  • 集中式负载均衡,在消费者和服务提供方中间使用独立的代理方式进行负载,有硬件的(比如 F5),也有软件的(比如 Nginx)。
  • 客户端根据自己的请求情况做负载均衡,LoadBalancer、Ribbon 就属于客户端自己做负载均衡。

2、快速开始

废话不多少,上代码。

pom.xml

maven依赖配置文件,如下:

<?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.mindasoft</groupId>
	<artifactId>spring-cloud-loadbalancer-consumer</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>spring-cloud-loadbalancer-consumer</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.6.4</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<spring-cloud.version>2021.0.1</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</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>

	<repositories>
		<repository>
			<id>nexus-aliyun</id>
			<name>Nexus aliyun</name>
			<url>https://maven.aliyun.com/repository/public</url>
			<layout>default</layout>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
			<releases>
				<enabled>true</enabled>
			</releases>
		</repository>
	</repositories>
	<pluginRepositories>
		<pluginRepository>
			<id>nexus-aliyun</id>
			<name>Nexus aliyun</name>
			<url>https://maven.aliyun.com/repository/public</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
			<releases>
				<enabled>true</enabled>
			</releases>
		</pluginRepository>
	</pluginRepositories>

</project>

上面配置文件,我们没有新增LoadBalancer的配置,其实spring-cloud-starter-netflix-eureka-client已经依赖了一个具体实现,在老版本的是Ribbon,新的版本是Spring自己实现的loadbalancer,如下:。

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

所以不需要格外添加配置。

application.properties

服务配置文件,也可以用yml,如下:

server.port=9003

# 服务注册中心地址
eureka.client.service-url.defaultZone=http://localhost:9000/eureka/

# 服务名称
spring.application.name=loadbalancer-customer

LoadBalancerConsumerApplication

服务启动类,如下:

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;

@EnableDiscoveryClient // Eureka Discovery Client 标识
@SpringBootApplication
public class LoadBalancerConsumerApplication {

	@Bean
	@LoadBalanced  		// 开启负载均衡的功能
	RestTemplate restTemplate() {
		return new RestTemplate();
	}

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

上面代码中,@LoadBalanced 注解开启了RestTemplate的负载均衡,具体原理后面深入源码学习时再做了解。这里只需要了解该注解标记后,该http请求会有负载均衡效果。

ConsumerController

服务调用,

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * Created by huangmin
 */
@RestController
public class ConsumerController {

	private static final Logger LOGGER = LoggerFactory.getLogger(ConsumerController.class);
	
	@Autowired
	private RestTemplate restTemplate; // HTTP客户端

	@RequestMapping("/hello")
	public String hello() {
		String providerMsg = restTemplate.getForEntity("http://PROVIDER-SERVICE/hello",
				String.class).getBody();

		return "Hello,I'm Customer! msg from provider : <br/><br/> " + providerMsg;
	}
}

3、Ribbon

在2020版本之后,已经移出了Ribbon。

posted @ 2022-03-07 19:54  技术那点事儿  阅读(552)  评论(0编辑  收藏  举报