Spring Cloud Micro Services: Service Registration and Discovery

Keywords: Programming Spring snapshot Java Maven

Eureka is Netflix's open source service registration and discovery component.

Common Service Registration and Discovery Components

Zookeeper,Eureka

Eureka Service Registration and Discovery Principles

  1. Services are registered with the registry
  2. Get additional registry service configurations

Create Eureka Server

Create a project using Spring Initializr

Create a project




Check Dependencies

After creating the project, check the version number in the generated pom

<?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.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>proj.ms</groupId>
  <artifactId>registry</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <name>registry</name>
  <description>registry center</description>

  <properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-server</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>



adding annotations

Add the @EnableEureka Server annotation on the RegistryApplication class to enable the default configuration of Eureka

package proj.ms.registry;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class RegistryApplication {

    public static void main(String[] args) {

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

}


Add Configuration

Modify the application.yml file to add the following:

server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
  application:
    name: eureka

Start Eureka Server

Click the Launch button in the upper right corner of the development tool to launch Eureka Server.Visit http://localhost:8761/

Eureka Cluster

Eureka Cluster Deployment Architecture


When a microservice is registered, it is registered to multiple Eureka at the same time, even if one of the nodes is unavailable, it will not cause the service registration to fail.

Eureka Cluster Synchronization Principle


Eureka nodes automatically synchronize their data to ensure high availability of the cluster.

Eureka Cluster Configuration

Modify host file

Add the following to the host file:

127.0.0.1      peer1
127.0.0.1      peer2
127.0.0.1      peer3

application.yml

---
spring:
  profiles: peer1
  application:
    name: eureka
server:
  port: 8761
eureka:
  instance:
    hostname: peer1
  client:
    service-url:
      defaultZone: http://peer2:8762/eureka/,http://peer3:8763/eureka/
---
spring:
  profiles: peer2
  application:
    name: eureka
server:
  port: 8762
eureka:
  instance:
    hostname: peer2
  client:
    service-url:
      defaultZone: http://peer1:8761/eureka/,http://peer3:8763/eureka/
---
spring:
  profiles: peer3
  application:
    name: eureka
server:
  port: 8763
eureka:
  instance:
    hostname: peer3
  client:
    service-url:
      defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/

Eureka cluster startup

In Idea, multiple applications are configured simultaneously to activate different profiles through startup parameters.

Similarly, configure three applications, corresponding to different profile s.

Eureka cluster validation

Access in Browser http://peer1:8761/

Visit http://peer2:8762/,http://peer3:8763/ You will find that the three Eureka nodes have finished registering with each other.

Package Deployment

Pack

Package with the maven command:

mvn clean package -Dmaven.test.skip=true

After packaging, two files, registry-1.0.0-SNAPSHOT.jar and registry-1.0.0-SNAPSHOT.jar.original, will be generated in the project target directory.Where registry-1.0.0-SNAPSHOT.jar contains all the dependent jar packages for the project.

Function

Start Eureka with the following command:

java -jar -Dspring.profiles.active=peer1 registry-1.0.0-SNAPSHOT.jar proj.ms.registry.RegistryApplication

Similarly, start all nodes in the cluster by specifying different configuration files.

java -jar -Dspring.profiles.active=peer1 registry-1.0.0-SNAPSHOT.jar proj.ms.registry.RegistryApplication
java -jar -Dspring.profiles.active=peer2 registry-1.0.0-SNAPSHOT.jar proj.ms.registry.RegistryApplication
java -jar -Dspring.profiles.active=peer2 registry-1.0.0-SNAPSHOT.jar proj.ms.registry.RegistryApplication

Service Producer and Consumer Testing

Question Summary

ConnectException in cluster environment startup log

com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect

Reason for the problem: Due to the different starting order of different nodes in the cluster, the registered Eureka Service has not finished starting when registering with each other during the start-up process.If peer1 starts before peer2 has finished, the above exception will occur when peer1 registers itself with peer2.
Solution: This exception log is a normal phenomenon in the cluster. After all nodes in the cluster have finished starting up, they will successfully complete registration with each other. The above exception will not appear in the log.

In a clustered environment, nodes are in the unavailable-replicas list

Reason for problem: Eureka registers with host name by default
Solution: Host name specified in eureka.instance.hostname, and the correct host can be found by modifying the host file or DNS.

Posted by kankersite on Thu, 19 Dec 2019 21:06:38 -0800