Client load balancing Spring Cloud Ribbon (b) the use Notes

Service Ribbon consumption and load balancing

Add module in spring-cloud-demo project: demo-service-ribbon
join rely on pom.xml file:

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-ribbon</artifactId>
    </dependency>

</dependencies>

Increase application.properties file in the resource directory, add the following:

################## 服务配置 ###############
server.port=9136
spring.application.name=demo-service-ribbon
#注册到注册中心
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/,http://localhost:8762/eureka/

Creating a startup class: ServiceRibbonApplication.java, add the following:

@SpringBootApplication
@EnableDiscoveryClient
public class ServiceRibbonApplication {

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

    @Bean
    @LoadBalanced ////负载均衡配置
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

Add Service category: DemoRibbonService.java, add the following:

@Service
public class DemoRibbonService {

    @Autowired
    RestTemplate restTemplate;

    public String port() {
        return restTemplate.getForObject("http://demo-service/port", String.class);
    }
}

Add Controller class: DemoRibbonController.java, add the following:

@RestController
public class DemoRibbonController {

    @Autowired
    DemoRibbonService demoRibbonService;

    @RequestMapping("hello")
    public String port() {
        return demoRibbonService.port();
    }
}

Were used 9806,9807 port demo-service project built before running
the run demo-service-ribbon project, visit: http: // localhost: 9136 / hello, show the following:
the I AM demo-service, the I'm from Port: 9806
visit again http: // localhost: 9136 / hello, show the following:
the I AM Demo-Service, the I'm from Port: 9807

Description demo-service-ribbon project added @LoadBalanced annotated restTemplate use the Ribbon of load balancing in the demo-service access service

Ribbon load balancing policy configuration

Ribbon load balancing common strategies

  1. RandomRule: a service instance randomly selected from the list of services

  2. RoundRobinRule: in round-robin fashion from the list of services from example

  3. RetryRule: According maxRetryMillis maximum retry retry time parameter to obtain service instances when obtaining a service instance failed

  4. WeightedResponseTimeRule
    The strategy is an extension of RoundRobinRule increase is calculated according to the example of the operation of the weight, and to select examples according to the weight, to achieve a better distribution effect

  5. ClientConfigEnabledRoundRobinRule
    This strategy RoundRobinRule with the same function, its role is to rewrite this strategy may inherit the Choose () method, when the selection policy can not be customized policies can be implemented as an alternative to the parent class

  6. BestAvailableRule
    the policy inherited from ClientConfigEnabledRoundRobinRule, in implementation it into a statistics object LoadBalancerStats load balancer, as well as to meet the requirements of selected examples of instances of the use of statistical information stored in LoadBalancerStats choose a specific algorithm. From the source code, all service instances by traversing the load balancer it maintained, will filter out the failed instance, and find the minimum number of concurrent requests made, so characteristic of this strategy is to select the least busy instance .

  7. PredicateBasedRule
    an abstract strategy, based on Google Guava Collection filter condition interface policy Predicate implemented, the basic realization of the "first filter list, and then select polling"
  8. AvailabilityFilteringRule
    PredicateBasedRule achieved using a linear select, based on the use of eligible, not eligible to find the next, unlike the parent need to traverse all services calculated reselection
  9. ZoneAvoidanceRule
    to be supplemented. .

The configuration procedure

To configure the policy, for example random
method of adding the startup class file ServiceRibbonApplication.java in:

//新增随机策略
@Bean
public IRule ribbonRule() {
    return new RandomRule();    //这里选择随机策略,对应配置文件
}

9806,9807 ports were used to build the demo-service project before you run
and then run the demo-service-ribbon project, visit: http: // localhost: 9136 / hello, you will find several multi-access port is the return of a random
I Demo-Service-AM, from the I'm Port: 9806
the I AM-Demo-Service, the I'm from Port: 9807
the I AM-Demo-Service, the I'm from Port: 9806
the I AM-Demo-Service, the I'm from Port: 9806

Guess you like

Origin www.cnblogs.com/yhongyin/p/11183850.html