Spring-Cloud-Netflix-Ribbon&Feigen

Load balancing Ribbon

Load Balancing Overview

  1. The actual environment, we tend to turn a number of goods-service clusters. At this point there is a list of services that we acquired will be more, in the end of the visit, which one do
  2. How from among multiple servers, balanced call

Spring Cloud Ribbon

  1. Spring Cloud Ribbon is based on a set of client load balancing tool Netflix Ribbon implemented.
  2. Ribbon is Netflix released open source project, the main function is to provide the client software load balancing algorithm, an intermediate layer connecting the Netflix service together.
  3. Ribbon is Netflix released a load balancer, Ribbon default load balancing algorithm provides a lot of us,
  4. Polling e.g., randomly. Of course, we can also achieve load balancing algorithm customized for the Ribbon. In Spring
  5. Cloud, when used in conjunction with Eureka and Ribbon, Ribbon can automatically obtain service provider address list from Eureka Server,
  6. And based on the load balancing algorithm, which requests a service provider instance. Shows the architecture of the Ribbon when used in conjunction with Eureka.

Ribbon load balancing configuration

  1. Eureka which has been in the replacement of the Ribbon, so we do not need to add dependencies alone
    Here Insert Picture Description
  2. Add a comment @LoadBalanced on RestTemplatet the Bean
    Here Insert Picture Description
  3. Among the instance name is defined in the controller to call the service
    Here Insert Picture Description
    default is polling
    Here Insert Picture Description
    Here Insert Picture Description

Load Balancing Algorithm

  1. random
  2. polling
  3. hash: According to hash the address ip, ip value by the number of the service mold, thereby determining the access authentication
  4. Minimum Access

These algorithms do not need to write to us, using a direct operation Robbin

Core components IRule

IRule Yes Yes Ribbon for load balancing strategy implemented interfaces

IRule default class implements the interface:

  1. RoundRobinRule: poll
  2. RandomRule: Random
  3. AvailabilityFilteringRule: filter will first access failure due to multiple services in a circuit breaker trip state, and the connection data concurrent service exceeds a threshold value, then the remaining list of services selected according to the polling policy
  4. WeightedResponseTimeRule: calculation of all services based on the average response time weighting, the faster the response time greater the weight, just start If the statistics are insufficient, the use RoundRobinRule strategy, and so when statistics are enough, will switch to WeightedResponseTieRule
  5. RetryRule: first get a policy in accordance with RoundRobinRule service, if the service fails to obtain, in the context of the development time will be retried, and then get less, then give up
  6. BestAvailableRule: will first filter out due to multiple access failure, but the circuit breaker tripped in the service, and then select a minimum amount of concurrent service
  7. ZoneAvoidanceRule: default rules, in line with the availability of selected servers and server performance to determine your area of ​​Server

IRule configuration: Start adding classes
Here Insert Picture Description

	@Bean
    public IRule Irule(){
        return new RandomRule();//随机
    }

Different service set policies

step:

  1. And two new order order1 submodule
    Here Insert Picture Description
  2. Add dependent
	<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--Eureka的客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
  1. Copy the file to the goods order, order1 and change the name
    Here Insert Picture Description
  2. Modify the configuration file
    Here Insert Picture Description
    Here Insert Picture Description
  3. Config package to create, write and order goods load balancing configuration
    Here Insert Picture Description
    goodsconfig:
@Configuration
public class goodsConfig {
    @Bean
    public IRule goods(){
        return new RoundRobinRule();
    }
}

orderconfig:

@Configuration
public class OrderConfig {
    @Bean
    public IRule order(){
        return new RandomRule();
    }
}
  1. Add startup class
    Here Insert Picture Description
    deleted:
    Here Insert Picture Description

figs

Overview:

Directly RestTemplate problem: when requesting the interface,
the path is still a problem, only solved the service name, the service is not resolved, functional path problem, because there may be developed by others, you do not know what is behind the functional path

  1. What is Feign?
  1. Feign is a declarative WebService client. Feign designed to make writing Java Http client easier.
  2. Feign in RestTemplate made on the basis of further encapsulated by him to help us define and implement the service interface definitions depend
  3. Feign can request to hide, you do not have your own mosaic url, splicing operation parameters, etc., to do everything to Feign.
  4. When called, the use of load balancing, load balancing rule is the current client configuration

Steps for usage:

  1. Introduction rely on the client:
    Here Insert Picture Description
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

  1. In the startup class above add comments: @EnableFeignClients
    Here Insert Picture Description
  2. Create a service FeignClient, create a service folder, create an interface in the file, add @FeignClient interface plus () annotation parameter is the name of your micro-services
    Here Insert Picture Description
@FeignClient(name= "client-goods")
public interface goodsFeignClient {
    @RequestMapping("/getGoods.do")
    public Object getGoods();
}

  1. Call in which a controller
    Here Insert Picture Description
 	@Autowired
    goodsFeignClient goodsFeignClient;
 	@RequestMapping("/getGoods.do")
    public ResponseResult getGoods(){
        return ResponseResult.success("操作成功",
                goodsFeignClient.getGoods());
    }
  1. Start Access localhost: 5000 / getGoods.do
    Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/joker-dj/p/12664070.html