首先先在C:\Windows\System32\drivers\etc\host文件中添加如下Host
127.0.0.1 eurekaB 127.0.0.1 eurekaA
先建springcloudParent.java的项目
pom.xml文件如下
pom.xml
<?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>org.example</groupId> <artifactId>springcloudParent</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> </modules> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> </project>
在建一个eurekademo的maven module
pom.xml文件如下:
pom.xml
<?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> <groupId>com.zk</groupId> <artifactId>eurekademo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eurekademo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>2.3.7.RELEASE</spring-boot.version> <spring-cloud.version>Hoxton.SR9</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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.3.7.RELEASE</version> <configuration> <mainClass>com.zk.eurekademo.EurekademoApplication</mainClass> </configuration> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
EurekademoApplication.java
package com.zk.eurekademo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekademoApplication { public static void main(String[] args) { SpringApplication.run(EurekademoApplication.class, args); } }
application.yml
spring: application: # 应用名字,eureka 会根据它作为服务id name: eureka server: # 服务端口 port: 8761 eureka: instance: hostname: eurekaA client: register-with-eureka: false # 不向eureka server 注册自己 fetch-registry: false # 不向eureka server 获取服务列表 service-url: defaultZone: http://eurekaB:8762/eureka/
复制一份eurekademo为eurekademo2
仅更改application.yml
spring: application: # 应用名字,eureka 会根据它作为服务id name: eureka server: # 服务端口 port: 8762 eureka: instance: hostname: eurekaB client: register-with-eureka: false # 不向eureka server 注册自己 fetch-registry: false # 不向eureka server 获取服务列表 service-url: defaultZone: http://eurekaA:8761/eureka/
启动时勾选Allow parallel run
两个springboot application启动
访问http://localhost:8761/