Technology Sharing

SpringCloud--Eureka cluster

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Eureka registration center cluster

Why cluster?

If there is only one registration center server, there will be a single point of failure and high concurrency processing will not be possible, so a cluster is required.

How to cluster

Prepare three EurekaServers to register with each other, that is, each EurekaServer needs to register with all EureakServers, including itself. Each EurekaServer acts as both a server and a client. Our other microservices (order, user) only need to point the registration address to all EurekaServers.

Code:

A Eureka module is used here to configure multiple yml files to achieve the cluster effect.

Startup Class

  1. package org.example;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. /**
  6. * @ClassName EurekaStart
  7. * @Author 23
  8. * @Date 2024/7/10 19:12
  9. * @Version 1.0
  10. * @Description TODO
  11. **/
  12. @SpringBootApplication
  13. @EnableEurekaServer
  14. public class EurekaStart {
  15. public static void main(String[] args) {
  16. SpringApplication.run(EurekaStart.class,args);
  17. }
  18. }

yml

  1. spring:
  2. profiles:
  3. active: peer3
  4. ---
  5. spring:
  6. profiles: peer1
  7. application:
  8. name: Eureka1
  9. eureka:
  10. instance:
  11. hostname: localhost
  12. client:
  13. serviceUrl:
  14. defaultZone: http://localhost:10070/eureka/
  15. server:
  16. port: 10070
  17. ---
  18. spring:
  19. profiles: peer2
  20. application:
  21. name: Eureka2
  22. eureka:
  23. instance:
  24. hostname: localhost
  25. client:
  26. serviceUrl:
  27. defaultZone: http://localhost:10071/eureka/
  28. server:
  29. port: 10071
  30. ---
  31. spring:
  32. profiles: peer3
  33. application:
  34. name: Eureka3
  35. eureka:
  36. instance:
  37. hostname: localhost
  38. client:
  39. serviceUrl:
  40. defaultZone: http://localhost:10072/eureka/
  41. server:
  42. port: 10072

pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <parent>
  5. <groupId>org.example</groupId>
  6. <artifactId>Springcloud-Netflix</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. </parent>
  9. <artifactId>Eureka-Service</artifactId>
  10. <packaging>jar</packaging>
  11. <name>Eureka-Service</name>
  12. <url>http://maven.apache.org</url>
  13. <properties>
  14. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  15. </properties>
  16. <dependencies>
  17. <dependency>
  18. <groupId>junit</groupId>
  19. <artifactId>junit</artifactId>
  20. <version>3.8.1</version>
  21. <scope>test</scope>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-web</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-test</artifactId>
  30. </dependency>
  31. <!-- Eureka服务端支持 -->
  32. <dependency>
  33. <groupId>org.springframework.cloud</groupId>
  34. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  35. </dependency>
  36. </dependencies>
  37. </project>

Service Load Balancing

Why do we need load balancing?

In order to provide concurrency, sometimes the same service provider can deploy multiple (commodity services). When calling, the client must complete the load call according to a certain load balancing strategy.

Service provider configuration

  1. spring:
  2. profiles:
  3. active: order2
  4. ---
  5. server:
  6. port: 10010
  7. spring:
  8. profiles: order1
  9. application:
  10. name: order-service
  11. eureka:
  12. client:
  13. service-url:
  14. defaultZone: http://localhost:10072/eureka
  15. instance:
  16. prefer-ip-address: true
  17. ---
  18. server:
  19. port: 10012
  20. spring:
  21. profiles: order2
  22. application:
  23. name: order-service
  24. eureka:
  25. client:
  26. service-url:
  27. defaultZone: http://localhost:10072/eureka
  28. instance:
  29. prefer-ip-address: true

Common load balancing implementation technologies

Ribbon

Use RestTmplate to complete the service call with URL

Code implementation (service consumer side):

  1. package org.example.Controller;
  2. import org.example.domain.Order;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.cloud.client.ServiceInstance;
  5. import org.springframework.cloud.client.discovery.DiscoveryClient;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.PathVariable;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RestController;
  10. import org.springframework.web.client.RestTemplate;
  11. import java.util.Arrays;
  12. import java.util.List;
  13. /**
  14. * @ClassName UserController
  15. * @Author 23
  16. * @Date 2024/7/10 18:52
  17. * @Version 1.0
  18. * @Description TODO
  19. **/
  20. @RestController
  21. @RequestMapping("/User")
  22. public class UserControllerrlb {
  23. @Autowired
  24. private RestTemplate restTemplate;
  25. // @Autowired
  26. // private DiscoveryClient discoveryClient;
  27. @GetMapping("/getOrder/{id}")
  28. public List<Order> getOrderById(@PathVariable("id") Integer id) {
  29. // Order[] order = restTemplate.getForObject("http://localhost:10010/OrderService/user/" + id, Order[].class);
  30. // return Arrays.asList(order);
  31. // List<ServiceInstance> instances=discoveryClient.getInstances("order-service");//通过服务的名字去获取服务实例
  32. // ServiceInstance serviceInstance=instances.get(0);//定死只获取第一个服务实例对象
  33. // String ip=serviceInstance.getHost();//获取服务对象ip
  34. // int port=serviceInstance.getPort();//获取获取的实例对象的服务端口号
  35. Order[] orders=restTemplate.getForObject("http://Order-Service/OrderService/user/"+id,Order[].class);
  36. return Arrays.asList(orders);
  37. }
  38. }

Ribbon load balancing call

Ribbon is an open source project for cloud middle-tier services released by Netflix. Its main function is to provide a client-side load balancing algorithm. The Ribbon client component provides a series of complete configuration items, such as connection timeout, retry, etc. Simply put, Ribbon is a client-side load balancer. We can list all the machines behind the load balancer in the configuration file. Ribbon will automatically help you connect to these machines based on certain rules (such as simple polling, random connection, etc.). It is also easy for us to use Ribbon to implement a custom load balancing algorithm. Ribbon is a client-side load balancer. It can complete the load balancing call of multiple service instances of a service according to certain rules. These rules also support customization.

Schematic:

OpenFeign Load Balancing

OpenFeign is a declarative Web Service client, and its purpose is to make Web Service calls easier. Feign provides an interface template for HTTP requests (with access addresses marked on it). By writing simple interfaces and inserting annotations, you can define the parameters, format, address and other information of HTTP requests. OpenFeign will completely proxy (dynamic proxy) HTTP requests. We only need to call it like calling a method to complete the service request and related processing. Feign integrates Ribbon and Hystrix (we will talk about Hystrix later), so we no longer need to use these two components explicitly.

Load balancing strategy

IRule

By configuring different IRule subclasses, you can select different load balancing strategies, that is, select a service from the service list with a specific strategy to complete the call. Of course, you can also customize it. So the load balancing strategy can be divided into built-in and custom.

Built-in load balancing strategy