- 自己注册自己
- 将自己注册给别人
- 将别人注册进来
* 相关代码及配置如下:
POM文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<finalName>ROOT</finalName>
</build>
主函数类
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class ServerApplication_First extends SpringBootServletInitializer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(ServerApplication_First.class);
}
public static void main(String[] args) {
new SpringApplicationBuilder(ServerApplication_First.class).web(true).run(args);
}
}
配置文件(我的案例是两个注册中心的使用,配置文件有三个)
主配置文件*application.properties
# 制定你要使用的配置文件,选择不同的配置文件生成不同的jar包
spring.profiles.active=server-first
一号注册中心的配置文件
# spring cloud server 一号注册中心,实现注册中心的高可用
spring.application.name=springcloudserver
server.port=9090
eureka.instance.hostname=springcloudserver-first
eureka.client.serviceUrl.defaultZone=http://springcloudserver-two:9091/eureka/
## 不向注册中心注册自己
#eureka.client.register-with-eureka=false
## 设置不去检索服务
#eureka.client.fetch-registry=false
二号注册中心的配置文件
# spring cloud 二号注册中心相关配置,实现注册中心的高可用
spring.application.name=springcloudserver
server.port=9091
eureka.instance.hostname=springcloudserver-two
eureka.client.serviceUrl.defaultZone=http://springcloudserver-first:9090/eureka/
## 不向注册中心注册自己
#eureka.client.register-with-eureka=false
## 设置不去检索服务
#eureka.client.fetch-registry=false
注意:修改host文件,添加如下内容
127.0.0.1 springcloudserver-first springcloudserver-two
码云上的项目地址
// 项目名spring-cloud-server
https://git.oschina.net/fushengruomengzhang_admin/Study_Work_Space/tree/master/Spring_Cloud_Server