SpringCloud Ribbon实战之自定义负载均衡策略(五)

x33g5p2x  于2021-12-21 转载在 其他  
字(5.2k)|赞(0)|评价(0)|浏览(252)

下面我们继续讲Ribbon自定义的负载均衡策略

在启动微服务的时候就能去加载我们自定义的Ribbon配置类,从而使配置生效,

例如使用新的注解@RibbonClient:@RibbonClient(name="MICROSERVICECLOUD-DEPT",configuration=MySelfRule.class)

下面我们就开始吧

1,修改消费者客户端的主启动类,在主启动类上加上新的注解@RibbonClient

package com.atguigu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;

@SpringBootApplication
@EnableEurekaClient
//在启动该微服务的时候就能去加载我们的自定义Ribbon配置类,从而使配置生效
//@RibbonClient(name="MICROSERVICECLOUD-DEPT",configuration=MySelfRule.class)
@RibbonClient(name="MICROSERVICECLOUD-DEPT",configuration=MySelfRule.class)
public class DeptConsumer80_App {
	public static void main(String[] args) {
		SpringApplication.run(DeptConsumer80_App.class, args);
	}
}

这里意思就是要对"MICROSERVICECLOUD-DEPT"微服务实现自定义的负载均衡策略 ,自定义的算法配置:MySelfRule

注意:这个自定义的配置类不能放在@ComponentScan所扫描的包以及当前的子包下,否则我们自定义的这个配置类就会被所有的Ribbon客户端所共享,也就是说达不到我们特殊化定制的目的了

说到这里是不是有点不理解啊,不理解没有关系,下面我就为大家分析一波:这个注解**@ComponentScan好像从开始到现在就没有写过啊,是不是那就可以随便放那个包下,不是这样的,下面我们来看下主启动类下的**@SpringBootApplication注解

下面我们就来看一下这个@SpringBootApplication注解有什么特殊,我点进这个注解类源码如下:

/*
 * Copyright 2012-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.autoconfigure;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.TypeExcludeFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.core.annotation.AliasFor;

/**
 * Indicates a {@link Configuration configuration} class that declares one or more
 * {@link Bean @Bean} methods and also triggers {@link EnableAutoConfiguration
 * auto-configuration} and {@link ComponentScan component scanning}. This is a convenience
 * annotation that is equivalent to declaring {@code @Configuration},
 * {@code @EnableAutoConfiguration} and {@code @ComponentScan}.
 *
 * @author Phillip Webb
 * @author Stephane Nicoll
 * @since 1.2.0
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
		@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {

	/**
	 * Exclude specific auto-configuration classes such that they will never be applied.
	 * @return the classes to exclude
	 */
	@AliasFor(annotation = EnableAutoConfiguration.class, attribute = "exclude")
	Class<?>[] exclude() default {};

	/**
	 * Exclude specific auto-configuration class names such that they will never be
	 * applied.
	 * @return the class names to exclude
	 * @since 1.3.0
	 */
	@AliasFor(annotation = EnableAutoConfiguration.class, attribute = "excludeName")
	String[] excludeName() default {};

	/**
	 * Base packages to scan for annotated components. Use {@link #scanBasePackageClasses}
	 * for a type-safe alternative to String-based package names.
	 * @return base packages to scan
	 * @since 1.3.0
	 */
	@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
	String[] scanBasePackages() default {};

	/**
	 * Type-safe alternative to {@link #scanBasePackages} for specifying the packages to
	 * scan for annotated components. The package of each class specified will be scanned.
	 * <p>
	 * Consider creating a special no-op marker class or interface in each package that
	 * serves no purpose other than being referenced by this attribute.
	 * @return base packages to scan
	 * @since 1.3.0
	 */
	@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
	Class<?>[] scanBasePackageClasses() default {};

}

通过上面的源码我们可以发现

那这个注解是属于@SpringBootApplication注解类的,这个@SpringBootApplication注解是在主启动类上是,这个主起动类在

com.atguigu.springcloud包下的,那也就是我们自定义的这个配置策略类不能再com.atguigu.springcloud包下也就是不能喝主启动类的包统计和子包下面。

听了我的分析应该清楚了,那我们就新建一个包com.atguigu.myrule

MySelfRule类自动配置类配置的是随机策略

package com.atguigu.myrule;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import com.netflix.loadbalancer.RetryRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MySelfRule {
    @Bean
    public IRule myRule() {
        //return new RoundRobinRule();
        return new RandomRule();//达到的目的,用我们重新选择的随机算法替代默认的轮询。

    }
}

下面我们来启动测试下(启动微服务参考:https://blog.csdn.net/ywl470812087/article/details/102638054)

通过测试结果已经达到了随机的负载均衡的策略,我们自定义负载均衡就已经完成

到此是不是觉得Ribbon负载均衡学习也很简单

如果觉得哪里写的不好,欢迎在评论去提出指正,我看到会第一时间回复

相关文章