Article Directory
1. Project construction
Create the parent project
pom.xml file
- The packaging method is
pom
- Delete
src
folder - Add SpringBoot parent project
- Add the version of SpringCloud
- Can be added to skip the test when packaging
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<!-- 换成2.3.3版本 eureka会启动失败-->
<!-- <version>2.3.3.RELEASE</version>-->
</parent>
<!--springcloud的版本控制-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!--打包的时候跳过测试-->
<skipTests>true</skipTests>
</properties>
Second, create Eureka module
Create a submodule under the parent project
pom.xml file
<?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">
<parent>
<artifactId>***</artifactId>
<groupId>***</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>******</artifactId>
<!--eureka的服务端 注册中心-->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
application.yml configuration file
server:
port: 7001
eureka:
instance:
hostname: 127.0.0.1
client:
register-with-eureka: false #是否将自己注册到eureka中
fetch-registry: false #是否从eureka中获取信息
service-url:
defaultZone: http://${
eureka.instance.hostname}:${
server.port}/eureka/
spring:
application:
name: eureka
Main start class
package com.mychanggou.goods;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @author :LiuShihao
* @date :Created in 2020/8/11 3:36 下午
* @desc :注册中心启动类
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class);
}
}
Three, other modules
1. Add'in the application.yml of each service
eureka:
client:
service-url:
defaultZone: http://localhost:6868/eureka
2. Add eureka client dependency to pom.xml file
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
3. Add @EnableEurekaClient
annotations to the main startup class of each service
Start the eureka microservice and visit it in the browser.