文章目录
前情提要
开发工具:IDEA
JDK:1.8
Maven:3.6.3
Spring Boot:2.1.7.RELEASE
Spring Cloud:Greenwich.SR2
创建项目
IDEA - Create New Project
配置pom文件
添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
完整版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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.demo</groupId>
<artifactId>eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</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>
</project>
配置application.yml文件
修改resource目录下的application.properties的后缀为yml
application.yml
spring:
application:
name: eureka
logging:
level:
root: info
---
spring:
profiles: 7901
server:
port: 7901
eureka:
instance:
hostname: eureka-7901
client:
service-url:
defaultZone: http://localhost:7902/eureka/,http://localhost:7903/eureka/
---
spring:
profiles: 7902
server:
port: 7902
eureka:
instance:
hostname: eureka-7902
client:
service-url:
defaultZone: http://localhost:7903/eureka/,http://localhost:7901/eureka/
---
spring:
profiles: 7903
server:
port: 7903
eureka:
instance:
hostname: eureka-7903
client:
service-url:
defaultZone: http://localhost:7901/eureka/,http://localhost:7902/eureka/
@EnableEurekaServer
在启动类上增加@EnableEurekaServer注解
package com.demo.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
在IDEA里配置不同端口的eureka实例
Name:随便填,一般写端口用于区分不同实例
Main class:这里选择添加了@SpringBootApplication注解的启动类
Program arguments:
--spring.profiles.active=7901
7901是application.yml文件中profiles对应的值
其他2个同理
7902:
7903:
此时IDEA会检测到你有多份Spring Boot的配置文件,问你要不要一起管理。选择 Show run configurations in Services。
修改hosts文件
将以下加入hosts文件
127.0.0.1 eureka-7901
127.0.0.1 eureka-7902
127.0.0.1 eureka-7903
一键启动
启动7901的时候会报错,不用管它,是因为其他两台Eureka没启。
com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect
在浏览器输入
http://localhost:7901/
出现如下界面则说明集群部署成功
7901:
7902:
7903:
欢迎交流
代码仓库
GitHub:https://github.com/xiuqiang1995/Spring-Cloud