Start a spring Cloud project - the first step is to create the registration center Eureka Server

Start a spring Cloud project

First create a registration center required by spring Cloud

Open IDEA and choose to create a new project

1. The first step is to create a parent project

Create a maven project as the parent project.
Insert image description here
Create a good name.
The project name can be whatever you want...

Then add spring boot and spring cloud dependencies (springCloud must depend on spring boot)
and add the following dependencies in the pom.xml file of the parent project:

   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.7.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
<!-- 加入springCloud 相关依赖 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2. The second step is to create a sub-project based on the parent project as the registration center project.

Right-click the parent project, then select New->Module-->Select maven-->Then continue until it is completed,
just like the following (the sub-project name is best related to eureka. Because this sub-project is used as the registration center):
Insert image description here
In the sub- project In the project's pom.xml file, add the dependency of the registration center Eureka:
as follows:

 <!-- 加入注册中心依赖 -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>
    </dependencies>

ps: This eureka server dependency is based on the springCloud project introduced by the parent project, and they are related.

Then create a new application.yml file in the resource folder of the subproject for the configuration of Eureka Server
Insert image description here

The content of the yml file is as follows:

server:
  port: 8761
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    server-url:
      defaultZone: Http://localhost:8761/eureka/

yml configuration instructions:

  • server-port: is the port number of the registration center
  • eureka-client-register-with-eureka: false Whether to register the current Eureka Server service as a client
  • eureka-client-fetch-registry: false Whether to obtain data from other Eureka Server services
  • eureka-client-server-url-defaultZone: access address of the registration center

3. Create a startup class in the subproject and start the subproject (that is, start the registration center)

Create a new package under the java folder of the subproject, and then create a new java file ending with application in the package
Insert image description here

The content is added as follows:

package com.southwind;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
//声明该类是springboot服务的入口
@SpringBootApplication
//声明该类是Eureka Server 微服务 提供服务注册和服务发现功能,即注册中心
@EnableEurekaServer
public class EurekaServerApplication {
    
    
    public static void main(String[] args){
    
    
        SpringApplication.run(EurekaServerApplication.class);
    }
}

4. Start the subproject, and then enter the address of the registration center written in yml:

Insert image description here
Enter the address: http://localhost:8761/

Insert image description here

In this way, the registration center project required by spring Cloud is created, and then we will continue to update other functions of spring Cloud.

This sentence appears on the website:
Instances currently registered with Eureka

It refers to the service registered here. There is currently no service registration, so it is empty. When eureka-client-register-with-eureka: false in yml is set to true, you can see your new sub-project here ( eureka) registered here

Guess you like

Origin blog.csdn.net/chenmaolin928/article/details/109120339