Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

With the Eureka service center mentioned above, it is time to introduce the following inter-service calls.

1. What is a client

Each business function service of a microservice is called a client. That is, each function module service that is split out.

2. Set up a client

The client is just a normal Springboot project, and adding some annotations to it is called a microservice client.

1. Create a SpringBoot project

Create a SpringBoot project using the traditional method.

2. The modified pom

Pom file <? 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 https://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.2.5. RELEASE</version>
        <relativePath/> <! -- lookup parent from repository -->
    </parent>
    <groupId>com.baocl</groupId>
    <artifactId>eureka-client</artifactId>
    <version>0.0.1 - the SNAPSHOT</version>
    <name>eureka-client</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR1</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.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </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>
Copy the code
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
Copy the code

There is a pit !!!! The latest client is this JAR package. The previous package is different from the present one. If it is not registered in eureka, let’s see if this is the reason.

3. Modify EurekaClientApplication

Here the @enableDiscoveryClient annotation indicates that it is the client of Spring Cloud.

package com.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {

	public static void main(String[] args){ SpringApplication.run(EurekaClientApplication.class, args); }}Copy the code

4. Write interfaces

And then let’s write an interface here, nothing to say.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/aaa")
public class DcController {
	@Value("${server.port}")
	String port;

	@GetMapping("/dc")	
	public String dc(@RequestParam(value = "dk") String dk) {
// try {
// Thread.sleep(100L);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
/ /}
		//String services = "Services: " + discoveryClient.getServices();
		//System.out.println(services);
		// HttpServletRequest request = ((ServletRequestAttributes)
		// (RequestContextHolder.getRequestAttributes()));
		// ServletRequestAttributes se =
		// (ServletRequestAttributes)(RequestContextHolder.getRequestAttributes());
		// HttpServletRequest request = se.getRequest();
		return "Service provider port is" + port + "... The consumer port is"+ dk; }}Copy the code

5. Modify the application. The properties

1. Required configuration

Finally, let’s focus on configuration files. The following is an essential configuration.

spring.profiles.active=dev
application-dev.properties

spring.application.name=eureka-client
server.port=2001Designated service port # eureka. Client. ServiceUrl. DefaultZone = HTTP://localhost:1001/eureka/
Copy the code

2. Resolve other configurations

The idle time, that is, the expiration time of service renewal (90s by default), is the timeout time for the Eureka server to wait for the next heartbeat after receiving the last heartbeat from the client. # If the eureka server does not receive the next heartbeat within this time, the instance will be removed. #eureka.instance.lease-expiration-duration-in-seconds=10Heartbeat time, namely, service renewal interval (default: 30s) #eureka.instance.lease-renewal-interval-in-seconds = 5Eureka-server-connect-timeout-seconds = specifies the timeout period for connecting to the Eureka Server10The interval between retrieving registration information from the Eureka server, in seconds default30
#eureka.client.registery-fetch-interval-seconds=1000Start service registration by defaulttrue
#eureka.client.register-with-eureka = trueWhether to filter the instance when getting the instance, only keep the default of the instance in UP statetrue
#eureka.client.filter-only-up-instances = trueOpen the health check (need to spring - the boot - starter - physical dependence) # eureka. Client. Healthcheck. Enabled =true#eureka.client.fetch-registry=true
Copy the code

Another point here is that we can change application.properties to determine which configuration file we launch at startup, and we can also modify application.properties after starting a project so that we can create a clustered environment ourselves.

Then, we start three times (the three configurations need different port numbers), so we have created a cluster, and then we can see our client information on Eureka, as shown below!