1.Ribbon meaning
- Spin cloud ribbon is a set of client load balancing tools based on Netfix
- Ribbon is a set of open source projects released by Netfix. Its main function is to provide the client software load balancing method to connect the middle tier services of Netfix. Ribbon's client provides a series of complete configuration items: connection timeout, Retry, etc. in short, it lists loadbalance (LB) in the configuration file, Ribbon will help you connect to the server based on certain rules.
- Load balancing is to evenly distribute user requests to multiple servers, so as to achieve HA (high availability)
- Common load balancing software: Ngix
- Both Dubbo and spring cloud provide load balancing. The load balancing provided by spring cloud can be customized.
- Simple classification of load balancing: centralized: independent load balancing facilities, such as Ngix reverse proxy, are provided at the service consumers and providers. The facility is responsible for forwarding access requests to the service providers through certain policies.
- Simple classification of load balancing: Programming: centralize the load balancing logic to the consumer. The consumer knows which addresses are available from the service registry, and then selects an appropriate server from the addresses, such as Ribbon. It is just a class library integrated into the consumer process, through which the consumer obtains the address of the service provider
2. Import dependent packages, write configurations, and open annotations
Because the occurs on the client, it should be written in the service consumer and imported into the ribbon and Eurek clients
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> <version>1.4.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.4.6.RELEASE</version> </dependency>
eureka: client: register-with-eureka: false service-url: defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7001.com:7001/eureka/
3. Configure load balancing to implement template
The address should be a variable, which can be modified to randomly access the server through the service name:
After the integration of Ribbon and Eureka, the client can call directly, regardless of IP address and port number.
4. Achieve cluster load balancing
3.1 configuring multiple service providers
Register the service in Eureka:
3.2 start eureka, test by multiple service providers and consumers,
Test result: polling access to three service providers.
5. Custom load balancing algorithm
The load balancing algorithm implements the IRule interface. There are several load balancing algorithms:
- Roundrobin rule: polling
- RandomRule: random
- Availabilityfiltering rule: first filter out the services with trip access failure, and poll the remaining services.
- RetryRule: it will poll first. If it fails to obtain the specified service, it will try to obtain it again within a certain time.
Custom Ribbon client
5.1 add annotations and configure your own algorithm class
Name = "service name", configuration = "custom algorithm class"
Note: the custom algorithm class should not be in the same directory as the startup class, otherwise it will be scanned by componentScan. Please put it in a separate and non overlapping package.
Feign – load balancing
brief introduction
feign is a declarative web service client. You only need to create an interface and add an annotation. feign mainly faces the community and calls two methods of micro services
- Microservice Name: ribbon
- Interface and annotation: feign
Feign aims to make it easier to write Http clients. Under the implementation of feign, you only need to create an interface and add an annotation. Similarly (Mapper annotation is marked on dao interface, and now a micro service interface is marked with feign annotation), you can complete the binding of the service provider's interface,
1. Add feign dependency
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> <version>1.4.6.RELEASE</version> </dependency>
2. Create a new service
@Component @FeignClient(value = "SPRINGCLOUD-PROVIDER") public interface DeptClientService { @GetMapping("/dept/get/{id}") public Dept queryById(@PathVariable("id") Long id); @GetMapping("/dept/getAll") public List<Dept> queryAll(); @PostMapping("/dept/add") public boolean addDept(Dept dept); }
3. Remote call
@RestController public class DeptConsumerController { @Autowired private DeptClientService deptClientService; @GetMapping("/dept/get/{id}") public Dept queryById(@PathVariable("id") Long id){ return deptClientService.queryById(id); } @GetMapping("/dept/getAll") public List<Dept> queryAll(){ return deptClientService.queryAll(); } @PostMapping("/dept/add") public boolean addDept(Dept dept){ return deptClientService.addDept(dept); } }
2. Open annotation