(1) Registration of Spring Cloud Eureka Services in Discovery, and Part of the Analysis

Keywords: Spring Maven Apache Java

Spring Cloud is a distributed system that provides more flexible configuration and use. The function is very rich.

Eureka Server will provide service registration service, after each service node starts, it will register in Eureka Server, so that Eureka Server has the information of all service nodes, and Eureka has a monitoring page, which can intuitively see the situation of all registered services. At the same time, Eureka has a heartbeat mechanism. When a node service does not send a heartbeat signal within a specified time, Eureka will remove the service node from the service registry. Eureka also provides a client-side caching mechanism. Even if all Eureka Servers are deactivated, the client can still use the information in the cache to invoke the service of the service node. Eureka is generally used in conjunction with Ribbon. Ribbon provides the client load balancing function. Ribbon uses the service information read from Eureka to load the service provided by the service node reasonably when calling the service provided by the service node. (Ribbon will explain in the next article)
Eureka guarantees high availability and flexibility of the system through heartbeat detection, health check, client cache and other mechanisms.

In this article, we will make a brief analysis of Spring Cloud Eureka's service registration, service management and the introduction of its construction.

As shown in the figure, we have an Eureka Server service. Each of our application services is deployed on Client. After deployment, we register ourselves on Service through Eureka, so that the load balancing and configuration of future applications can be obtained and dispatched through Service.

Pum.xml of Spring Service

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.zx</groupId>
  <artifactId>SpringCloudOne</artifactId>
  <version>0.0.1-SNAPSHOT</version>
	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>   <!--Coordination spring cloud Edition-->
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>   
        <!--Setting character encoding and java Edition-->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.7</java.version>
    </properties>

    <dependencies>
        <!--increase eureka-server Dependence-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <!--For testing, this example can be omitted-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!--Dependency management for management spring-cloud Dependence-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-parent</artifactId>
                <version>Brixton.SR3</version>   <!--Official website Angel.SR4 Version, but I always make mistakes when I use it.-->
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <!--Packing with this plug-in-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>   
</project>

Then create the entry class (a[pplication.class)

@EnableEurekaServer//Start the Eureka service
@SpringBootApplication
public class ApplicationServer implements EmbeddedServletContainerCustomizer {
    public static void main(String[] args) {
    	//Loading Bean s through Annotations
    	SpringApplication.run(ApplicationServer.class, args);
//    	Loading Bean s from Spring Configuration Files
//    	SpringApplication.run("classpath:applicationContext.xml", args);
//    	SpringApplication app=new SpringApplication(Application.class);
//    	app.run(args);
    }

	public void customize(ConfigurableEmbeddedServletContainer container) {
		// TODO Auto-generated method stub
		container.setPort(8761);
	}
}

Add application.yml or use the properties file. No compulsion

Note that the Eureka configuration file is so far away that the server-port should be consistent with the port where your service is located. The official recommendation is 8761.

server:
  port: 8761     #Specify service ports
eureka:
  client:
    registerWithEureka: false  #Is eureka itself registered as an application in the eureka registry?
    fetchRegistry: false       
    #When true, it can be started, but an exception is reported: Cannot execute request on any known server

The service can then be started. Visit localhost:8761 to see Eureka's default home page

Client Configuration:

The pom file is as follows:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.zx</groupId>
  <artifactId>SpringCloudClient</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.5.RELEASE</version>
    <relativePath/>
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.7</java.version>
  </properties>

  <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-parent</artifactId>
            <version>Brixton.SR3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
      <!--Here is the difference.-->
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-eureka</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
      </dependency>
       <!-- Client Load Balancing jar package -->
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-ribbon</artifactId>
      </dependency>
      <!-- Cross-Service Request Pack -->
      <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
<!--      <dependency>-->
<!--            <groupId>org.apache.httpcomponents</groupId>-->
<!--            <artifactId>httpclient</artifactId>-->
<!--        </dependency>-->
<!--        <dependency>-->
<!--            <groupId>com.netflix.feign</groupId>-->
<!--            <artifactId>feign-httpclient</artifactId>-->
<!--            <version>${feign-httpclient}</version>-->
<!--        </dependency>-->
  </dependencies>

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

</project>

Then create the entry class:

@EnableDiscoveryClient //Allow services to be discovered
@SpringBootApplication
@EnableFeignClients //Allow calls between services (as we will see later)
public class ApplicationClientOne {
//public class Application implements EmbeddedServletContainerCustomizer {
    public static void main(String[] args) {
    	//Loading Bean s through Annotations
    	SpringApplication.run(ApplicationClientOne.class, args);
//    	Loading Bean s from Spring Configuration Files
//    	SpringApplication.run("classpath:applicationContext.xml", args);
//    	SpringApplication app=new SpringApplication(Application.class);
//    	app.run(args);
    }

//	public void customize(ConfigurableEmbeddedServletContainer container) {
//		// TODO Auto-generated method stub
//		container.setPort(9000);
//	}
	
}
@RestController
class ServiceInstanceRestController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping("/service-instances/{applicationName}")
    public List<ServiceInstance> serviceInstancesByApplicationName(@PathVariable String applicationName) {
        return this.discoveryClient.getInstances(applicationName);
    }
}

Under this class, I define a method service Instances ByApplicationName, which obtains relevant information by the name of the application.

The return parameters are as follows:

[{"metadata":{},"uri":"http://PMOMIS-PC.dayang.com:8080","secure":false,"serviceId":"ONE-CLOUD-CLIENT","instanceInfo":{"instanceId":"PMOMIS-PC.dayang.com:One-cloud-client:8080","app":"ONE-CLOUD-CLIENT","appGroupName":null,"ipAddr":"192.168.6.23","sid":"na","homePageUrl":"http://PMOMIS-PC.dayang.com:8080/","statusPageUrl":"http://PMOMIS-PC.dayang.com:8080/info","healthCheckUrl":"http://PMOMIS-PC.dayang.com:8080/health","secureHealthCheckUrl":null,"vipAddress":"One-cloud-client","secureVipAddress":"One-cloud-client","countryId":1,"dataCenterInfo":{"@class":"com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo","name":"MyOwn"},"hostName":"PMOMIS-PC.dayang.com","status":"UP","leaseInfo":{"renewalIntervalInSecs":30,"durationInSecs":90,"registrationTimestamp":1491381396464,"lastRenewalTimestamp":1491381396464,"evictionTimestamp":0,"serviceUpTimestamp":1491381395840},"isCoordinatingDiscoveryServer":false,"metadata":{},"lastUpdatedTimestamp":1491381396464,"lastDirtyTimestamp":1491381395750,"actionType":"ADDED","asgName":null,"overriddenStatus":"UNKNOWN"},"host":"PMOMIS-PC.dayang.com","port":8080}]

This method will be used later when we call another application service.

Add configuration file application.yml

server:
  port: 8080
spring:
  application:
    name: One-cloud-client    #Name your application, which will be registered with the eureka registry
eureka:
  client:
    serviceUrl:
      defaultZone: http://Localhost: 8761/eureka/  Eureka service address, the same machine can be ignored

After starting the service, the application registers itself according to the Eureka server address in the configuration file.

Similarly, we will find such an application on the Eureka homepage.

This completes the registration and discovery of the entire service.

Note:

1: When a client registers with eureka, it will provide metadata about its own port, address, health monitoring url, home page, etc., and erueka will receive heartbeat information from each instance. If the heartbeat fails within the configured time, the instance is usually removed from the registry.  

2: The default heartbeat time of registration service is 30 seconds. When a server is unavailable, it takes three heartbeats to make the metadata of the server and client identical. You can use eureka. instance. leaseRenewal IntervalInSeconds to speed up the process. Default configuration is best used in production environments. However, this configuration is not recommended to be changed. Eureka will use this parameter to determine whether the service is down on a large scale and take relevant measures (I haven't implemented it here, to be discussed).

Relevant information, articles and sources:

        http://blog.csdn.net/zhuchuangang/article/details/51202307

        http://blog.csdn.net/lc0817/article/details/54375802

Posted by devxtech on Fri, 12 Jul 2019 19:37:38 -0700