美文网首页我爱编程
Spring-Cloud系列 Ribbon(二)

Spring-Cloud系列 Ribbon(二)

作者: 苏村的南哥 | 来源:发表于2018-05-25 16:45 被阅读62次

什么是Ribbon

Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients.

    上述是官方文档对Ribbon的解释,意思是Ribbon是一个客户端负载均衡器,它可以有效的控制HTTP和TCP客户端的访问。上述一段话有个很关键的词“客户端负载均衡器”,我们常用的负载均衡模式就是nginx反向代理模式,这种模式我们是不知道服务究竟请求的是那台机器的。这种负载均衡模式是服务端负载均衡。
而客户端负载均衡就是在发起请求是有明确的目标,请求精确指向集群中的某台机器。
</br>    Ribbon的负载均衡默认是依靠Eureka来实现的,Ribbon通过Eureka客户端定期拉取注册服务,通过客户端的轮训、随机等算法来实现负载均衡。

快速开始

这个案例依赖Eureka-Server,请准备好server服务。Eureka相关查看Spring-Cloud系列(一) Eureka

添加依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.13.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>Dalston.SR5</spring-cloud.version>
</properties>

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

    看上面的依赖有些同学可能会奇怪,为什么没有引入Ribbon依赖,只引入了Eureka客户端。下面我们来看一下Eureka客户端的依赖关系图就明白了。


Eureka客户端依赖

    从上图中我们明显可以看出来,Eureka客户端的依赖是包含Ribbon依赖的。所以我们不需要添加依赖。

代码

初始化bean

/**
 * @LoadBalanced Ribbon整合注解,Rebbon本身依赖在EurekClient中存在。所以不需要继续引入
 * Ribbon是客户端负载均衡实现之一,具有轮训,随机等等多种算法。
 * @return
 */
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
    return new RestTemplate();
}

使用方式

@RestController
public class RibbonControlller {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/url")
    public String postUser() {
        return this.restTemplate.getForObject("http://eureka-client-one/url", String.class);
    }
}

application配置

server:
</br>  port: 8082
</br>spring:
</br>  application:

</br>    name: ribbon-one
</br>eureka:
</br>  instance:
</br>    prefer-ip-address: true
</br>    instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}
</br>  client:
</br>    serviceUrl:
</br>      defaultZone: http://user:Pass123456@localhost:8761/eureka

启动效果

启动效果

自定义RibbonClient

代码自定义

定义配置

@Configuration
public class RibbonConfiguration {

    @Autowired
    private IClientConfig config;

    @Bean
    public IRule ribbonRule(IClientConfig config){
        return new RandomRule();
    }
}

The FooConfiguration has to be @Configuration but take care that it is not in a @ComponentScan for the main application context, otherwise it will be shared by all the @RibbonClients. If you use @ComponentScan (or @SpringBootApplication) you need to take steps to avoid it being included (for instance put it in a separate, non-overlapping package, or specify the packages to scan explicitly in the @ComponentScan).

注意Configuration类不能和主类平级,也不能在主类的子包下,否则该配置会覆盖本项目下所有的Ribben配置

代码配置

配置注入

@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "eureka-client-one",configuration = RibbonConfiguration.class)
public class RibbonApplication {

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

使用方式

@Autowired
private LoadBalancerClient loadBalancerClient;

@GetMapping("/test")
public String test() {
    ServiceInstance instance = this.loadBalancerClient.choose("eureka-client-one");
    return instance.toString();
}

可被重写的其他配置

    上面的代码配置我们改变了IRule配置,更多例子参考官方文档。以下配置可以通过代码修改:

  • IClientConfig ribbonClientConfig: DefaultClientConfigImpl
  • IRule ribbonRule: ZoneAvoidanceRule
  • IPing ribbonPing: NoOpPing
  • ServerList<Server> ribbonServerList: ConfigurationBasedServerList
  • ServerListFilter<Server> ribbonServerListFilter: ZonePreferenceServerListFilter
  • ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer
  • ServerListUpdater ribbonServerListUpdater: PollingServerListUpdater

配置文件自定义

配置文件

users:#即需要调用的应用名
</br>  ribbon:
</br>    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule

使用方式

@Autowired
private LoadBalancerClient loadBalancerClient;

@GetMapping("/test")
public String test() {
    ServiceInstance instance = this.loadBalancerClient.choose("eureka-client-one");
    return instance.toString();
}

注意!
</br>Ribbon的配置优先级顺序是 yml>javaConfig>default

其他配置选项

上面的配置文件我们改变IRule配置,更多例子参考官方文档。以下配置可以通过配置文件修改

  • NFLoadBalancerClassName: should implement ILoadBalancer
  • NFLoadBalancerRuleClassName: should implement IRule
  • NFLoadBalancerPingClassName: should implement IPing
  • NIWSServerListClassName: should implement ServerList
  • NIWSServerListFilterClassName should implement ServerListFilter

Ribbon的其他配置

Ribbon禁用Eureka

ribbon:
</br>  eureka:
</br>    enabled: false
</br>users:#即需要调用的应用名
</br>  ribbon:
</br>    listOfServers: localhost:8080,localhost:8081 #服务IP List

延迟加载Ribbon

ribbon:
</br>  eager-load:
</br>    enabled: true
</br>    clients: client1, client2, client3

    本文参考Spring-Cloud-RibbonRibbon延迟加载

相关文章

网友评论

    本文标题:Spring-Cloud系列 Ribbon(二)

    本文链接:https://www.haomeiwen.com/subject/xmjujftx.html