负载均衡

最近更新时间:2021-09-13 16:24:37

查看PDF

准备工作

开始实践负载均衡功能前,请确保已完成了 SDK 下载。

负载均衡依赖consul作为注册中心和配置中心,需要本地安装consul,consul下载和安装参考官网https://www.consul.io。

一、创建服务端

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

图片.png

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。

图片.png

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),也可以选择其他策略。

63.png

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

文档内容是否对您有帮助?

根本没帮助
文档较差
文档一般
文档不错
文档很好

在文档使用中是否遇到以下问题

内容不全,不深入
内容更新不及时
描述不清晰,比较混乱
系统或功能太复杂,缺乏足够的引导
内容冗长

更多建议

0/200

评价建议不能为空

提交成功!

非常感谢您的反馈,我们会继续努力做到更好!

问题反馈