Spring Cloud负载均衡

  1. 云栖社区>
  2. 博客>
  3. 正文

Spring Cloud负载均衡

xiaoshuaiv5 2019-03-22 20:29:35 浏览470
展开阅读全文

Spring Cloud负载均衡

  • 客户端负载均衡
  • 服务端负载均衡
  • 调度算法
  • 特性

客户端负载均衡

  • 优势
    稳定性高
  • 不足
    升级成本高

q1

服务端负载均衡

  • 优势
    统一维护,成本低
  • 不足
    一旦故障,影响大

q2

调度算法

  • 先来先服务(First Come First Served)
  • 最早截止时间优先(Earliest deadline first)
  • 最短保留时间优先(Shortest remaining time first)
  • 固定优先级(Fixed Proority)
  • 轮训(Round-Robin)
  • 多级别队列(Multilevel Queue)

非对称负载(Asymmetric load)

  • 分布式拒绝服务攻击保护
  • 直接服务返回
  • 健康检查
  • 优先级队列
    q3

配置ribbon client与service provider直连方式

1.service provider端
我的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>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.1.3.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.example</groupId>
   <artifactId>spring-cloud-lesson-6-service-provider</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>spring-cloud-lesson-6-service-provider</name>
   <description>Demo project for Spring Boot</description>

   <properties>
      <java.version>1.8</java.version>
      <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
   </properties>

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

</project>

2.配置application.properties文件


##服务提供方
spring.application.name=spring-cloud-service-provider

##服务端口
server.port=9090
##关闭注册
eureka.client.enabled=false

3.创建测试类

package com.example.web.controller;

import com.example.domain.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 服务提供方
 */
@RestController
public class ServiceProviderController {
    @GetMapping("/greeting")
    public String greeting(@RequestParam User user){
        return "Greeting,"+user;

    }
}

ribbon client端配置

1.我的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>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.1.3.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.example</groupId>
   <artifactId>spring-cloud-lesson-6</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>spring-cloud-lesson-6</name>
   <description>Demo project for Spring Boot</description>

   <properties>
      <java.version>1.8</java.version>
      <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
   </properties>

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

</project>

2.配置application.properties

##服务提供方
spring.application.name=spring-cloud-ribbon-client

##服务端口
server.port=8080
##关闭注册
eureka.client.enabled=false

##服务提供方主机
service-provider.host=localhost
##提供方端口
service-provider.port=9090

service-provider.name=spring-cloud-service-provider

###配置服务提供方的ribbon
spring-cloud-service-provider.ribbon.listOfServers=\
  http://${service-provider.host}:${service-provider.port}

3.创建连接测试类

package com.example.web.Controller;

import com.example.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ClientController {
    @Autowired
    private RestTemplate restTemplate;
    @Value("${service-provider.host}")
    private String serviceProviderHost;
    @Value("${service-provider.port}")
    private Integer serviceProviderPort;

    @GetMapping("")
    public String index(){
        User user=new User();
        user.setId(1L);
        user.setName("ss");
        return restTemplate.postForObject("http://"+serviceProviderHost+":"+serviceProviderPort+"/greeting",
                user,String.class);
    }
}

Netflix Ribbon

1.引入Maven依赖

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

2.激活Ribbon客户端
@RibbonClients({@RibbonClient(name="spring-cloud-service-provider")})
3.配置Ribbon客户端
application.properties



##服务提供方
spring.application.name=spring-cloud-ribbon-client

##服务端口
server.port=8080
##关闭注册
eureka.client.enabled=false

##服务提供方主机
service-provider.host=localhost
##提供方端口
service-provider.port=9090

service-provider.name=spring-cloud-service-provider

###配置服务提供方的ribbon
spring-cloud-service-provider.ribbon.listOfServers=\
  http://${service-provider.host}:${service-provider.port}

网友评论

登录后评论
0/500
评论
xiaoshuaiv5
+ 关注