创建eureka
创建一个maven
删掉src,再把iml拉入idea
新建一个module
选择Spring Initializr
建一个eureka(服务发现与注册中心)
选择Eureka Server
修改父项目pom.xml
(加入Spring Boot的父级依赖)
<?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>weitest</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.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>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
eureka端application.yml
spring:
application:
name: eureka
server:
port: 8081
eureka:
instance:
hostname: locahost
client:
fetchRegistry: false
registerWithEureka: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
eureka端EurekaApplication中加上@EnableEurekaServer
启动项目
访问http://localhost:8081/
eureka创建完成
创建服务端
创建与eureka端类似
application.yml
没有eureka端的false
defaultZone填eureka端的端口
spring:
application:
name: client
server:
port: 8761
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8081/eureka/
EurekaApplication中加上@EnableDiscoveryClient