负载均衡

最近更新时间:2020-11-10 10:11:48

准备工作
开始实践负载均衡功能前,请确保已完成了 SDK 下载。
负载均衡依赖consul作为注册中心和配置中心,需要本地安装consul,consul下载和安装参考官网https://www.consul.io。

一、创建服务端 

  1. 创建lb-server项目
    从微服务平台下载一个项目,命名为lb-server。

负载均衡

2.依赖项
修改pom.xml中dependency依赖如下:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

3.修改配置
在bootstrap.yml中添加如下配置。

server:
  port: 8080
spring:
  application:
    name: lb-server
  cloud:
    host: http://127.0.0.1
    port: 8500
    discovery:
      healthCheckPath: /actuator/health
      healthCheckInterval: 15s
      register: true
      service-name: lb-server
      enabled: true
      instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}
      tag: version=v0

4.代码调整
启动类添加注解。

@SpringBootApplication
@EnableDiscoveryClient
public class DemoApplication {

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

新增接口供客户端调用。

package com.demo.x.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping({"/account"})
@RestController
public class AccountController {

    private static final Logger log = LoggerFactory.getLogger(AccountController.class);

    @Value("${spring.cloud.discovery.instanceId}")
    private String instanceId;

    @GetMapping({"/{id}"})
    public String account(@PathVariable("id") Integer id) {
        log.info("调用account " + id);
        return id + ":"+instanceId;
    }
}

二、创建客户端
1.创建lb-client项目 从微服务平台下载一个项目,命名为lb-client。

负载均衡

2.依赖项
修改pom.xml中dependency依赖如下:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.ksyun.kmse</groupId>
            <artifactId>spring-cloud-kmse-loadbalancer-core</artifactId>
            <version>${version}</version>
        </dependency>
        <dependency>
            <groupId>com.ksyun.kmse</groupId>
            <artifactId>spring-cloud-kmse-starter-loadbalancer</artifactId>
            <version>${version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

3.修改配置
在bootstrap.yml中添加如下配置。

server:
  port: 8081
spring:
  application:
    name: lb-client
  cloud:
    host: http://127.0.0.1
    port: 8500
    discovery:
      healthCheckPath: /actuator/health
      healthCheckInterval: 15s
      register: true
      service-name: lb-client
      enabled: true
      instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}
      version=v0
ksyun:
  cloud       
    loadbalancer:
      destinationRule:
      - host: lb-server
        subsets:
        - labels:
            version: v0
          name: v0
          trafficPolicy:
            loadBalancer:
              simple: rr
        trafficPolicy:
          loadBalancer:
            simple: rr

4.代码调整
启动类添加注解。

@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class DemoApplication {

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

新增接口调用服务端

package com.demo.x.client;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient("lb-server")
public interface OrderClient {

    @GetMapping({"/account/{id}"})
    String getById(@PathVariable Integer id);

}

三、服务注册中心下发配置
在consul的kv中新建名为/lb/lb-client/v0/data的key,并添加如下配置,lb-server为服务名称,v0为服务的版本,rr为轮询策略,目前共支持三种负载均衡策略, 分别是随机(random)、轮询(rr)、响应时间权值(wr),也可以选择其他策略。

负载均衡

ksyun:
  cloud:
    loadbalancer:
      destinationRule:
      - host: lb-server
        subsets:
        - labels:
            version: v0
          name: v0
          trafficPolicy:
            loadBalancer:
              simple: rr
        trafficPolicy:
          loadBalancer:
            simple: rr

金山云,开启您的云计算之旅

免费注册