Soft Load Balancing of Client in Eureka Learning

Keywords: Spring Maven Apache Java

Client Soft Load Balancing for Eureka Learning (3)

This article mainly introduces how Eureka Client achieves soft load balancing (Demo demo demonstrates non-source level). My personal habits, first run a whole Demo, and then study the source code. Source code is placed in the final learning while recording.

1. Eureka Client multi-node

There is no record of how to create Server. Interested students can see my first two Eureka learning articles. General micro-service architecture will split services according to business. Most deployment methods for single-server and multi-node scenarios are as follows

Business module application name Port number
user management service-user 20001-20010
Departmental management service-dept 20011-20020
... ... ...

Now let's do some actual combat operations.

1.1 Create Eureka Client Service-user

I used the project created by Idea Spring Initializr and stopped taking screenshots. File-New-project selects the Spring Initializr plug-in.

  • Adding Eureka Discovery Client dependencies
  • Add Spring Web starter dependencies.
  • Add OpenFeign dependencies
  1. Maven configuration
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.blackhold</groupId>
    <artifactId>spring-cloud-eureka-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud-eureka-consumer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

  1. To configure Properties, we don't need to configure server.port because of the different nodes
# Eureka Client Slogan
# server.port=20001
# Application name
spring.application.name=provider-user
# Eureka related configuration
# 1. Register address to Eureka Server service
eureka.client.service-url.defaultZone=http://localhost:10001/eureka/
  1. Writing interface Controller to facilitate the viewing of the results of the soft load, adding the print of port s in the startup environment variables
@RestController
public class UserController {

    @Autowired
    Environment environment;

    @GetMapping("/getUser/{id}")
    public String getUser(@PathVariable() String id){
        return "port: " + environment.getProperty("server.port") + " | This is User [id = " + id + "]";
    }
}
  1. Configure the environment variables of Springboot started by Idea, set the startup port s to 2001 and 20002 respectively, and then start the application

  1. Start the class to add annotations. If you need to use the Feign function, add the @EnableFeignClients annotation
@SpringBootApplication
@EnableDiscoveryClient
public class SpringCloudEurekaProviderApplication {

    public static void main(String[] args) {

        SpringApplication.run(SpringCloudEurekaProviderApplication.class, args);
    }

}
  1. Looking at the registry after startup, you can see that an instance registered two services on ports 2001 and 2002, respectively.

Test interface http://localhost:20001/getUser/2 Return port: 20001 | This is User [id = 2]

Test interface http://localhost:20002/getUser/1 Return port: 20002 | This is User [id = 1]

1.2 Create Eureka Client service-dept

The method of creating a project is the same as above. The port number defines 20011, 20012. Look at the code directly.

  1. Maven configuration, ibid.
  2. properties configuration, ibid.
# Eureka Client Slogan
# server.port=20011
# Application name
spring.application.name=service-dept
# Eureka related configuration
# 1. Register address to Eureka Server service
eureka.client.service-url.defaultZone=http://localhost:10001/eureka/
  1. Adding interfaces
// Note that the value here is equivalent to the application.name of the service provider.
@FeignClient(value = "service-user")
public interface UserService {
    @RequestMapping(value = "/getUser/{id}", method = RequestMethod.GET)
    String getUser(@PathVariable("id") String id);
}
  1. Add Controller Call Interface
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/getUser/{id}")
    public String getUser(@PathVariable String id) {
				//Test soft load calls 100 times
        for (int i = 0; i < 100; i++) {
            System.out.println(userService.getUser(id));
        }
        return "ok";
    }
}
  1. run method startup project
@SpringBootApplication
@EnableFeignClients  //If you use FeignClient to invoke a microservice, you need to add this annotation and it will be automatically registered with Eureka.
public class SpringCloudEurekaConsumerApplication  {

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

}
  1. After successful startup, you can see the registry, registering service-user services and service-dept services.

  1. Test call http://localhost:20011/getUser/2 or http://localhost:20012/getUser/2 will output

port: 20001 | This is User [id = 2]
port: 20002 | This is User [id = 2]
port: 20001 | This is User [id = 2]
port: 20002 | This is User [id = 2]
port: 20001 | This is User [id = 2]

... Eliminate 95 lines

The output is very average. Basically, a 1:1 call is a request that goes 20001 and a request that goes 20002:

3. Summary

The service cluster can be invoked by the application-name name name, and Eureka automatically implements the soft load during the invocation process.

3. Reference

[Spring Cloud netflix English official website][https://spring.io/projects/spring-cloud-netflix]

Posted by Saphod on Fri, 02 Aug 2019 04:45:59 -0700