Ribbon简介
Spring Cloud Ribbon 是基于 Netflix Ribbon 实现的一套客户端负载均衡工具,其主要功能是提供客户端的软件负载均衡算法,将 Netflix 的中间层服务连接在一起。
其运行原理如下图:
Ribbon 运行时分成 2 个步骤:
1) 先选择在同一个区域负载较少的 EurekaServer;
2) 再根据用户指定的策略,在从 EurekaServer 中获取注册列表中的服务信息进行调用。
其中,Ribbon 提供多种负载均衡策略:如轮询、随机、响应时间加权等。
Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign(Feign默认集成了ribbon)。在这一篇文章首先介绍ribbon+restTemplate。
Ribbon实践
在上一篇的基础上,新建一个Ribbon的maven子项目,项目结构
①pom.xml(添加 spring-cloud-starter-eureka包时,会自动添加 Ribbon 依赖包。)
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.yj</groupId>
<artifactId>SpringCloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>Ribbon</artifactId>
<name>Ribbon</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<finalName>${artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.yj.ribbon.RibbonApplication</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>lib</classpathPrefix>
</manifest>
<manifestEntries>
<Class-Path>./</Class-Path>
</manifestEntries>
</archive>
<excludes>
<exclude>config/**</exclude>
<exclude>**.xml</exclude>
<exclude>**.properties</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/build/package.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<testResources>
<testResource>
<directory>src/main/resources</directory>
</testResource>
</testResources>
</build>
</project>
②RibbonController
package com.yj.ribbon.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.yj.ribbon.service.RibbonService;
@RestController
public class RibbonController {
@Autowired
private RibbonService ribbonService;
@GetMapping(value = "/hiRibbon")
public String hiRibbon(@RequestParam String name) {
return ribbonService.hiRibbon(name);
}
}
③RibbonService
package com.yj.ribbon.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class RibbonService {
@Autowired
private RestTemplate restTemplate;
public String hiRibbon(String name) {
return restTemplate.getForObject("http://EUREKA-CLIENT/hiEureka?name=" + name, String.class);
}
}
④application.properties
server.port=8005
spring.application.name=ribbon
eureka.client.serviceUrl.defaultZone=http://admin:[email protected]:8001/eureka,http://admin:[email protected]:8002/eureka
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port}
eureka.instance.hostname=${spring.cloud.client.ipAddress}
⑤logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
<!-- 彩色日志格式 -->
<property name="CONSOLE_LOG_PATTERN"
value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}" />
<!-- 控制台打印日志的相关配置 -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}<!--%d{yyyy-MM-dd HH:mm:ss.SSS} [%level] [%class:%line] - %m%n--></pattern>
<charset>utf8</charset>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>debug</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>ACCEPT</onMismatch>
</filter>
</appender>
<!-- 文件保存日志的相关配置 -->
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/Ribbon.log</file>
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}<!--%d{yyyy-MM-dd HH:mm:ss.SSS} [%level] [%class:%line] - %m%n--></pattern>
<charset>utf8</charset>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>info</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>ACCEPT</onMismatch>
</filter>
<!-- 循环政策:基于时间创建日志文件 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logs/%d{yyyy-MM-dd}/Ribbon.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
<logger name="com.yj.ribbon" level="DEBUG" />
<!-- 基于dubug处理日志:具体控制台或者文件对日志级别的处理还要看所在appender配置的filter,如果没有配置filter,则使用root配置 -->
<root level="info">
<appender-ref ref="console" />
<appender-ref ref="file" />
</root>
</configuration>
⑥start.sh,stop.sh
#! /bin/bash
moduleName="Ribbon"
pidPath="./$moduleName-tpid"
rm -f $pidPath
java -jar ./$moduleName.jar &
echo $!>$pidPath
#! /bin/bash
moduleName="Ribbon"
tpid=`cat ./$moduleName-tpid | awk '{print $1}'`
tpid=`ps -aef | grep $tpid | grep -v grep | grep $moduleName |awk '{print $2}' `
if [ ${tpid} ];then
kill -9 $tpid
fi
⑦package.xml
<?xml version="1.0" encoding="UTF-8"?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3
http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>package</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>bin</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>/</outputDirectory>
<excludes>
<exclude>mapper/**</exclude>
</excludes>
</fileSet>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>${artifactId}.jar</include>
</includes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<scope>runtime</scope>
<excludes>
<exclude>${groupId}:${artifactId}</exclude>
</excludes>
</dependencySet>
</dependencySets>
</assembly>
⑧application.properties
server.port=8005
spring.application.name=ribbon
eureka.client.serviceUrl.defaultZone=http://admin:[email protected]:8001/eureka,http://admin:[email protected]:8002/eureka
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port}
eureka.instance.hostname=${spring.cloud.client.ipAddress}
⑨RibbonApplication(@LoadBalanced注解让restTemplate可以很容易的通过ribbon实现客户端的负载均衡功能)
package com.yj.ribbon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
public class RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
⑩部署启动,Ribbon项目也被注册到Eureka上面了
访问http://192.168.37.139:8005/hiRibbon?name=yj,交替显示hi yj ,i am from port:8003,hi yj ,i am from port:8004