SpringCloud the Ribbon load balancing entry operations

Use Ribbon load balancing

Before using the Ribbon, we first think of a problem before, before we will service providers registered into the eureka registry, but in the end consumer, restTemplate we used to call the time in which to write or http: // localhost : 8001 such calls way, is not some wrong with that? Is not apply as dubbo as using the service name to call it? Otherwise, we use the registry what use is it?

Do good, let's keep this thought. To enter Ribbon learning

What is Ribbon?

Ribbon [rɪbən], is SpringCloud Netflix in about a client load balancing plugin.

The main explanation is as follows:

  • The main client is the mean service consumers, that is to say, this plug is used in the consumer side, which they will, according to some algorithm providers of the same service (ie application.name these service providers to be the same ) to be screened to decide what I want to access a service provider.
  • And Nginx is load balancing across servers, browsers when accessing devices such requests come in, it will be based on its own configuration, choose a route services.

Ribbon Integration (client, namely consumers)

Said above, the load balancing is performed by the consumer end, so to modify cousumer end, but in order to facilitate learning, I created a new project, change demo3-ribbon-consumer, and before the code is not much. Moreover, there must be more of the same-name service providers to perform load balancing can select multiple service providers before you? It's like, you became the emperor, but the Queen's Palace is only one person, that there is election it? Haha, this car is a little faster ah!

Again, we want to make calls using the service name, instead of using localhost were called. It is also not integrated eureka registration center? Only in this way can be found in the registry ah

  • Before using a two SpringCloud said assembly, the first step, the introduction of dependencies (note, is Consumer)
<!--ribbon-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>

<!--下边这两个是ribbon所依赖的-->
<!--eureka的客户端-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

<!--SpringCloud的配置-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
  • Add the corresponding notes on startup class
@EnableEurekaClient //eureka的客户端
  • Modify application.yml
server:
  port: 80

#  下边加eureka的配置就是为了能够在eureka注册中心中发现服务提供者
eureka:
  client:
    register-with-eureka: false
    service-url:
#    使用的eureka集群,上一次配置过的
      defaultZone: http://eureka1:7001/eureka/,http://eureka2:7002/eureka/,http://eureka3:7003/eureka/
  • Open load balancing configuration RestTemplate the bean class configuration
    @Bean
    @LoadBalanced //开启负载均衡
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
  • Contoller class temporarily modified as follows:
    @GetMapping("demo3/consumer/hello/{id}")
    public String hello(@PathVariable("id") String id){
        //远程调用provider中的接口,这回使用的这个提供者的名称
        return restTemplate.getForObject("http://demo3-ribbon-provider/demo3/provider/hello/"+id,String.class);
    }

Create multiple service providers of the same name

Said above, this is a must have in. Only when you have a choice, you can make a choice. This step too simple, write only one code, and the rest will be able to copy themselves (module name demo3-ribbon-provider-800x).

  • pom.xml
<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>
  • application.yml
server:
#每个服务提供者对应的端口号肯定要不同
  port: 8001

spring:
  application:
#  这个应用的名称,用来注册在注册中心的名称
    name: demo3-ribbon-provider

eureka:
  client: #客户端注册进eureka服务列表内
    service-url:
#       eureka集群的注册地址
      defaultZone: http://eureka1:7001/eureka,http://eureka2:7002/eureka,http://eureka3:7003/eureka
  instance:
    instance-id: demo3-ribbon-provider-8001
    prefer-ip-address: true     #访问路径可以显示IP地址
  • Controller class
@RestController
public class HelloController {

    @GetMapping("demo3/provider/hello/{id}")
    public String hello(@PathVariable("id") Integer id) {
        return "这是来自提供者的一条消息,你传过来的消息" + id+"\n\t你这次访问的服务提供者8001";
    }
}
  • Classes with the corresponding master boot annotations
@SpringBootApplication
@EnableEurekaClient //本服务启动后会自动注册进eureka服务中
@EnableDiscoveryClient//这个表明这个服务能被发现,也就是消费者可以调用它
public class Demo3RibbonProvider_8001 {
    public static void main(String[] args) {
        SpringApplication.run(Demo3RibbonProvider_8001.class, args);
    }
}

Finally, we live together. carry out testing

Start three eureka clusters, service provider three of the same name, a consumer. A total of seven service, seven processes, no, my computer I have started to protest, and look at my computer's memory. My memory is 8G, Idea has accounted for more than my 3G.

  • Eureka clusters of pictures

  • Visit three consecutive http://localhost/demo3/consumer/hello/99results

The results can be seen from the top, the results of three consecutive visit turned out to be not the same, this is the ribbon to start the load balancing algorithm ah. As for why is this?

Ribbon built-in load balancing algorithm

Ribbon polling algorithm is used by default, that is polled one by one to the same service provider, so that you can make each service provider have been visited once. Of course, Ribbon load balancing algorithm also supports custom, the next issue to say myself!

More about SpringCloud knowledge and project code, please pay attention to micro-channel public number "small fish and Java" get

Guess you like

Origin www.cnblogs.com/Lyn4ever/p/12370200.html