SpringCloud之Ribbon核⼼组件IRule的使⽤及⾃定义负载均衡算法⼀、IRule 常见的API
Ribbon核⼼组件IRule中常见的⼏个算法API,⼀般情况下,这⼏个API已经可以满⾜实际的业务需求了,如下:API说明
RoundRobinRule⼴为⼈知和最基本的负载平衡策略,即轮询算法。(是Ribbon默认的负载均衡机制)RandomRule⼀种随机分配现有流量的负载平衡策略,即随机访问算法
RetryRule 先按照 RoundRobinRule 的策略访问服务,如果访问的服务宕机或者出现异常的情况,则在指定时间内会进⾏重试,访
问其它可⽤的服务
BestAvailableRule⾸先会先过滤掉由于多次访问故障⽽处于断路器跳闸状态的服务,然后选择⼀个并发量最⼩的服务访问ZoneAvoidanceRule默认规则,复合判断server所在区域的性能和server的可⽤性选择服务器
AvailabilityFilteringRule ⾸先会先过滤掉由于多次访问故障⽽处于断路器跳闸状态的服务,还有并发的连接数量超过阈值的服务,然后对剩余的服
务列表按照轮询策略进⾏访问
WeightedResponseTimeRule 根据平均响应时间计算所有服务的权重,响应时间越快服务权重越⼤被选中的概率越⾼。刚启动时如果统计信息不⾜,则使⽤RoundRobinRule策略,等统计信息⾜够,会切换到WeightedResponseTimeRule
Ribbon 默认的是 RoundRobinRule 轮询算法
⼆、实现随机访问策略 RandomRule ,⼀种随机分配现有流量的负载平衡策略1、新建Eureka服务注册容器项⽬ springcloud-deepeureka
1.1、l依赖
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"  xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.qihang.cloud</groupId>
<artifactId>springcloud-deepeureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcloud-deepeureka</name>
<url></url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
&porting.outputEncoding>UTF-8</porting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!--spring cloud依赖管理-->
<!-- 引⼊spring cloud的依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.RELEASE</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>
1.2、启动类
@SpringBootApplication
@EnableEurekaServer
public class DeppEurekaApplication
{
public static void main( String[] args )
{
SpringApplication.run(DeppEurekaApplication.class, args);
}
}
1.3、配置⽂件
server:
port: 8081
eureka:
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: localhost:8081/eureka/
2、数据提供服务 springcloud-user
2.1、l
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"  xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.qihang.cloud</groupId>
<artifactId>springcloud-user</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
springboot推荐算法
<name>springcloud-user</name>
<url></url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
&porting.outputEncoding>UTF-8</porting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-solr</artifactId>    </dependency> -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2、数据库实体对象
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)  private Long id;
@Column
private String username;
@Column
private String name;
@Column
private Short age;
@Column
private BigDecimal balance;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Short getAge() {
return this.age;
}
public void setAge(Short age) {
this.age = age;
}
public BigDecimal getBalance() {
return this.balance;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
}
2.3、Repository数据库层

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。