• 【SpringCloud负载均衡】【源码+图解】【一】LoadBalancer的HelloWorld体验


    1. HelloWorld

    如果使用Eureka作为服务发现,则默认情况下加入spring-cloud-starter-netflix-eureka-client依赖后会自动加入LoadBalance的功能,因此本文主要是分析默认情况下SpringCloud使用的负载均衡。下面以HelloWorld进入LoadBalance的世界

    1.1 Server

    1.1.1 依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
        dependency>
     dependencies>   
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    1.1.2 EurekaServer

    @SpringBootApplication
    // 为了简便我们使用EurekaServer
    @EnableEurekaServer
    public class EurekaServeApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(EurekaServeApplication.class, args);
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    启动server

    1.2 Product微服务

    1.2.1 依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>
     <dependencies>   
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    1.2.2 配置文件

    spring:
      application:
        name: product
    
    server:
      port: ${port}
      
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:3333/eureka/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    这里product我们要起两个实例,以端口不同作为区别:5001、5002。

    application-peer1.yml

    port: 5001
    
    • 1

    如果是Eclipse,在启动时添加-Dspring.profiles.active=peer1参数

    application-peer2.yml

    port: 5002
    
    • 1

    1.2.3 Controller

    @RestController
    @RequestMapping("/prod")
    public class ProductController {
    
        @GetMapping
        public int getProduct(HttpServletRequest request) {
            // 直接返回端口号以示区别
            return request.getLocalPort();
        }
        
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    1.3 User微服务

    1.3.1 依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>
     <dependencies>   
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    1.3.2 配置文件

    spring:
      application:
        name: user
    
    server:
      port: 2222
      
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:3333/eureka/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    1.3.3 RestTemplate

    @SpringBootApplication
    public class EurekaClientApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(EurekaClientApplication.class, args);
    	}
    	
    	@LoadBalanced // 负载均衡
    	@Bean
    	public RestTemplate restTemplate() {
    	    return new RestTemplate();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    1.3.4 Controller

    @RestController
    @RequestMapping("/user")
    public class UserController {
        @Autowired
        RestTemplate restTemplate;
    
        @GetMapping
        public void getProduct() {
            // 调用Product微服务获取值
            String result = restTemplate.getForObject("http://product/prod", String.class);
            System.out.println(result);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    所有服务都启动后,在浏览器输入http://localhost:2222/user,按下回车键。为了看到负载均衡的效果,我们连续按下10次回车,再看下输出结果

    5001
    5002
    5001
    5002
    5001
    5002
    5001
    5002
    5001
    5002
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    从结果我们看到默认情况下是轮询这调用Product微服务,那么它是如何实现的呢?答案是通过intercept被@LoadBalanced的restTemplate的请求。

    未完待续

  • 相关阅读:
    laravel事件监听和job有哪些区别
    GEE|typeof、ee.Algorithms.If、ee.Algorithms.IsEqual 语法
    2022最新解析最清晰 Java 系列面试题
    计算机毕业设计ssm基于SSM框架下水果篮子项目管理系统的设计与实现 c9pjx系统+程序+源码+lw+远程部署
    手把手教你如何安装 Elasticsearch
    OpenCV图像处理——创建、读取、无损保存图像(C++/Python)
    Etcd 概要 机制 和使用场景
    Python数据分析与机器学习38-Xgboost算法
    Structed Streaming入门--Scala篇
    基于Redis手工实现分布式锁
  • 原文地址:https://blog.csdn.net/Lanna_w/article/details/128176312