• <menu id="sssag"></menu>
  • <menu id="sssag"></menu>
  • 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  技術那點事兒  閱讀(137)  評論(0編輯  收藏  舉報
    国产在线码观看超清无码视频,人妻精品动漫H无码,十大看黄台高清视频,国产在线无码视频一区二区三区,国产男女乱婬真视频免费,免费看女人的隐私超爽,狠狠色狠狠色综合久久蜜芽