1、在主工程新建maven-module,名称为:mycloud-eureka
2、编辑pom.xml文件
引入:spring-cloud-starter-netflix-eureka-server
<?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.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>mycloud-eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mycloud-eureka</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR5</spring-cloud.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-test</artifactId>
<scope>test</scope>
</dependency>
<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>
3、建立包com.example.mycloud.run
创建EurekaServerApplication.java
package com.example.mycloud.run;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
4、在src/main/resources下创建application.yml
spring:
application:
name: eureka-server
server:
port: 8010
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
5、配置说明
eureka.instance.hostname:主机名称,可搭配hosts文件配置域名解析
eureka.client.registerWithEureka:是否向eureka注册服务,服务端不需要
eureka.client.fetchRegistry:是否检索服务拉取服务列表,服务端不需要
eureka.client.serviceUrl.defaultZone:申明注册中心的地址
那么这些配置哪里可以查到比较全的呢,在官网上没找到,所以从代码中查找
eureka.instance开头:在类EurekaInstanceConfigBean
eureka.client开头:在类EurekaClientConfigBean
eureka.server开头:在类EurekaServerConfigBean
6、运行springboot(EurekaServerApplication的main方法),访问http://localhost:8010/
控制台正常显示
参考资料:
http://51think.net/article/32
https://www.cnblogs.com/javastack/p/10760510.html
注:最新代码上传至https://github.com/csj50/mycloud