Learn to use Spring Cloud Netflix Eureka together

Preface

Sprcloud provides a set of technology components for interconnected enterprises to build micro services, and Eureka is the core of Spring Cloud system. Netfix is not a technical concept, it was originally the name of a foreign video website. The technology team of this video website has done a lot of practice in the direction of micro-service and provided a lot of technical components, Eureka is one of them. The author is also a beginner of Spring Cloud. Starting from creating project project project, this paper explains how to create Eureka server and client step by step, learn together and make progress together.

This article comes from my technology blog: http://51think.net

I. What is eureka

We often see that some Internet companies use the term Eureka when describing their technical architecture. We have also heard that Eureka is used for service registration discovery. It is not clear how it is integrated into the application layer. From the point of view of code structure, Eureka is a large set of jar packages. maven introduces this set of jar packages and makes simple configuration in the application layer to realize service discovery. That is to say, Spring Cloud has encapsulated the relevant functions very well, just refer to them directly, which is also in line with the purpose of various popular frameworks, to minimize the non-business workload, so that programmers are more focused on implementing their own business functions. Eureka is a component that can not run independently. It needs Springboot as the application carrier. It can run only when it is deployed on the virtual machine.

Establishment of Eureka Registry

Registry, as its name implies, is similar to zookeeper. It provides service registration discovery function, that is, the service address of the server is exposed to the client through the registry, and the load balance is realized by the client. Next, we use the idea tool to create related projects.
1. Create a maven Master Project
2. Create a model under the main project, named spring-cloud-eureka in this case

3. Fill in Group and Artifact

4. Check Eureka Server

5. The pom file after creation

<?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.example</groupId>
    <artifactId>spring-cloud-eureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud-eureka</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.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>
        </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>

There is only one key spring-cloud-starter-netflix-eureka-server package in the pom file, which actually relies on many subpackages, as shown below:

6. Find the Springboot startup class with the @EnableEurekaServer annotation
@ Enable Eureka Server represents the spring boot application as a registry.

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

}

7. Configure application.yml

server:
  port: 8010

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

Two of the attributes registerWithEureka: false fetchRegistry: false indicate that the application is Eureka Server, not Client. defaultZone is used to declare the address of the registry, which is also required when creating the Eureka Client end to register with the registry.

Creating a Service Producer Application

Here we will create a springboot application as a producer of services and be able to register services in the registry. For the whole system, we create a server-side application for client invocation. For the Eureka registry, except that the registry is a Server role, all the other roles are Eureka Client roles. The specific process is as follows:
1. The process of creating model is similar to creating Eureka Server, named spring-cloud-eureka-myservice.
2. pom files

<?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.example</groupId>
    <artifactId>spring-cloud-eureka-myservice</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud-eureka-myservice</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.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>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>

You can compare the difference between this pom file and the previous pom file. The spring-cloud-starter-netflix-eureka-clien component corresponds to the spring-cloud-starter-netflix-eureka-server in the previous registry pom file. spring-boot-starter-web components are used to provide web access capabilities, and we can access back-end services through browsers.
3. Use @EnableEurekaClient to tag your identity

@EnableEurekaClient
@SpringBootApplication
public class MyServiceApplication {

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

}

4. Configure application.yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8010/eureka/
server:
  port: 8011
spring:
  application:
    name: myservice

This configuration item spring.application.name is very important to indicate the application name of the application in the microservice architecture, which can be used to access the service in subsequent cases.

5. Start registry application spring-cloud-eureka
6. Start service producers to apply spring-cloud-eureka-myservice
7. Access the Eureka panel
Eureka provides a web access page through which we can see the list of registered services and the status of registry applications. Browser access http://localhost 8010, the following pages will be displayed:

The part labeled in the red box is the spring-cloud-eureka-myservice application that we just launched with the service name myservice.
So far, Eureka Server and Client have been successfully deployed.

Source address: https://github.com/huangyubia...

Keywords: Java Spring Maven Apache

Added by EPJS on Sat, 27 Jul 2019 14:29:47 +0300