第三章:SpringCloud2.1.x,Ribbon负载均衡

一)Ribbon简介

Ribbon是一个客户端负载平衡器,支持对HTTPTCP客户端的行为进行大量控制。

Ribbon是通过RestTemplate方式实现的

 

缺点:微服务之间的交互,会需要把Rest地址写死,维护会稍微麻烦一点。

 

Ribbon负载均衡模式:

1、RoundRobinRule:默认规则,通过简单的轮询服务列表来选择服务器

2、AvailabilityFilteringRule:可用性筛选规则

3、WeightedResponseTimeRule:为每个服务器赋予一个权重值,服务器的响应时间越长,该权重值就越少,这个规则会随机选择服务器,权重值有可能会决定服务器的选择

4、ZoneAvoidanceRule:该规则以区域、可用服务器为基础进行服务器选择,使用区域(Zone)对服务器进行分类

5、BestAvailableRule:忽略"短路"的服务器,并选择并发数较低的服务器

6、RandomRule:随机选择可用服务器

7、RetryRule:含有重试的选择逻辑,如果使用 RoundRobinRule 选择的服务器无法连接,那么将会重新选择服务器

 

二)Ribbon案例

第一步:创建一个Eureka注册中心

pom.xml内容:



    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-server

 

application.yml配置:

注:spring.application.name应用名称不能包含下划线。

# 注册中心的默认端口号, 可以随意指定一个未使用的端口
server:
    port: 8761

# 应用名称, 如不指定, 会随意默认一个名称
spring:
    application:
        name: oysept-eureka

# Erueka注册中心配置
eureka:
  instance:
    # IP地址
    hostname: localhost
  client:
    # 表示是否将自己注册到Eureka Server中,默认为true, 由于当前应用就是 Eureka Server, 故而设置为false
    registerWithEureka: false
    # 表示是否从 Eureka Server中获取注册信息, 默认为true, 因为这是一个单点的 Eureka Server, 不需要同步其它的 Eureka Server 节点的数据, 故而设置为 false
    fetchRegistry: false
    serviceUrl:
      # 设置与Eureka Server交互的地址, 查询服务和注册服务都需要依赖这个地址. 默认http://localhost:8761/eureka; 多个地址可以使用","分隔
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

 

Eureka注册中心main方法启动类:

package com.oysept;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * Eureka服务注册中心
 * 地址: http://localhost:8761/eureka/
 * @author ouyangjun
 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

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

启动EurekaApplication中的main方法,然后可在浏览器中输入:http://localhost:8761查看。

注:启动Eureka注册中心之后,不需要把服务停止,后序会使用到注册中心

 

第二步:创建一个服务提供者

pom.xml内容:


    org.springframework.boot
    spring-boot-starter-web

		


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client

        


    org.springframework.cloud
    spring-cloud-starter-netflix-hystrix

 

application.yml配置:

server:
    port: 8762
    
spring:
    application:
        name: oysept-server
        
eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      # 表示需要把服务注册到Eureka注册中心上, 这里是Eureka注册中心的地址
      defaultZone: http://localhost:8761/eureka/

 

服务提供者main方法启动类:

package com.oysept;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;

/**
 * 服务提供者
 * @author ouyangjun
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class ServerApplication {

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

可先用8762的端口,启动EurekaApplication中的main方法,应用不要停止。然后在application.yml中修改一下端口为8763,再次启动EurekaApplication中的main方法。这样就相当于有两个服务端了。

 

在浏览器中输入:http://localhost:8761查看

第三章:SpringCloud2.1.x,Ribbon负载均衡_第1张图片

 

第三步:Ribbon负载均衡案例实现

pom.xml中引入jar:


    org.springframework.boot
    spring-boot-starter-web

		


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client

        


    org.springframework.cloud
    spring-cloud-starter-netflix-ribbon

完整的pom.xml内容:


    4.0.0
    com.oysept
    oysept_ribbon
    0.0.1-SNAPSHOT
    jar
  
    
    
        1.8
    
    
    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.4.RELEASE
    
    
    
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Greenwich.SR5
                pom
                import
            
        
    
	
    
        
            org.springframework.boot
            spring-boot-starter-web
        
		
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-ribbon
        
    
    
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

 

application.yml配置:

server:
    port: 8764
    
spring:
    application:
        name: oysept-ribbon
        
eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      # 表示需要把服务注册到Eureka注册中心上, 这里是Eureka注册中心的地址
      defaultZone: http://localhost:8761/eureka/

 

先创建一个RibbonConfig自定义配置类:

package com.oysept.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

import com.netflix.loadbalancer.BestAvailableRule;
import com.netflix.loadbalancer.IPing;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.PingUrl;

@Configuration
public class RibbonConfig {

    // 检验url是否畅通
    @Bean
    public IPing ribbonPing() {
        return new PingUrl();
    }
	
    // 负载均衡模式, 默认是轮询负载
    @Bean
    public IRule ribbonRule() {
        return new BestAvailableRule();
    }
	
    // Rest服务访问方式, 支持http和tcp通信协议
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

 

创建一个Controller类,测试Ribbon负载均衡:

package com.oysept.controller;

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;

@RestController
@RequestMapping("/ribbon")
public class RibbonController {

    @Autowired
    private RestTemplate restTemplate;
	
    // 访问地址: http://localhost:8764/ribbon/testRibbon
    @RequestMapping(value = "/testRibbon")
    public String testRibbon() {
        String msg = restTemplate.getForObject("http://OYSEPT-SERVER/demo/sendMsg?msg=testRibbon", String.class);
        System.out.println("==> testRibbon return msg: " + msg);
        return msg;
    }
}

 

Ribbon负载均衡main方法启动类:

package com.oysept;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;

import com.oysept.config.RibbonConfig;

/**
 * @RibbonClient: 负载均衡注解, 表示使用RibbonConfig自定义的配置
 * @author ouyangjun
 */
@SpringBootApplication
@EnableDiscoveryClient
@RibbonClient(name="custom", configuration=RibbonConfig.class)
public class RibbonApplication {

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

启动EurekaApplication中的main方法

在浏览器中输入:http://localhost:8764/ribbon/testRibbon查看

第三章:SpringCloud2.1.x,Ribbon负载均衡_第2张图片

 

识别二维码关注个人微信公众号

本章完结,待续,欢迎转载!
 
本文说明:该文章属于原创,如需转载,请标明文章转载来源!

你可能感兴趣的:(第三章:SpringCloud2.1.x,Ribbon负载均衡)