springCloud Finchley microservice architecture from entry to mastery [2] Eureka service registration and discovery

1. Description of development tools

To prevent the code from running incorrectly, it is recommended to use a consistent version:

  • Development tools: Eclipse : Version: Neon.3 Release (4.6.3) It
    is recommended to use idea or the STS tools officially provided by spring to develop spring cloud applications. The author chose Eclipse to install the STS plug-in due to the company's internal usage habits and other reasons.

  • STS plug-in download address: http://spring.io/tools/sts/all/
    Note here: STS plug-in should choose the version corresponding to Eclipse:

write picture description here

  • JDK : jdk1_8_131

  • MAVEN : maven 3.5

2. Create the Eureka registry

1. Create a Maven main project

write picture description here

Delete the src package in its directory, leaving only pom.xml

Here, the package selects pom, and finally its sub-projects have repeated dependencies and are uniformly extracted here. This is the main role of the main project.

2. Create a springboot project on the main project

write picture description here

Select Eureka Server , the project is created

write picture description here

At this point pom.xml is:

<?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>

    <groupId>com.mayi.springcloud</groupId>
    <artifactId>commonservice-eureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>commonservice-eureka</name>
    <description>eureka 注册中心</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.M9</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>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>


</project>

3 、 配置 bootstrap.yml

Rename application.properties in the resources directory to bootstrap.yml (recommended to use formatted ymal language, think about the benefits yourself ^_^), ymal verification is relatively strict, mine is a line break and two spaces, keep it uniform, and the editor is also with hints

server:
  port: 8761
spring:
  application:
    name: service-registry
eureka:
  instance:
    prefer-ip-address: true
  client:
    fetch-registry: false
    register-with-eureka: false
  server:
    wait-time-in-ms-when-sync-empty: 0

explain:

  • 配置为server :
    fetch-registry: false
    register-with-eureka: false

4. Startup class configuration

Just add the @EnableEurekaServer annotation

package com.mayi.springcloud;


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

@SpringBootApplication
@EnableEurekaServer
public class CommonserviceEurekaApplication {

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

Start with SpringBootApp, access address: localhost:8761

write picture description here

Open the above webpage normally, indicating that the Eureka registration center is successfully configured.

3. Create a client project

1. pom.xml is the same as above, and the simple code of bootstrap.yml is as follows:

spring:
  application:
    name: service-eureka-client
server:
  port: 8800

2. Add the @EnableEurekaClient annotation to the startup class

It is recommended to use the @EnableEurekaClient annotation to declare the client, because we are using the Eureka registry. If you use zookeeper or other registries, use @EnableDiscoveryClient , and for the Eureka registry, both can be

package com.mayi.springcloud;

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

@SpringBootApplication
@EnableEurekaClient
public class CommonserviceEurekaClientApplication {

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

3. Start the project

As shown in the figure, the client registration is successful:

write picture description here

Next, I will revise new articles in turn until the whole structure is completed. If you are interested, add me on WeChat and join the spring cloud community group.

write picture description here

Or pay attention to WeChat public account: java architect practice

write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326436463&siteId=291194637