创建一个Eureka Server
创建一个子模块microserver-discovery-eureka,该模块属于microserver-spring-cloud
(看第一篇)。
pom.xml
<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">
<parent>
<artifactId>microserver-spring-cloud</artifactId>
<groupId>com.tommy.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.tommy.springcloud</groupId>
<artifactId>microservice-discovery-eureka</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>microservice-discovery-eureka</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
注意:microserver-spring-cloud的pom.xml中增加springcloud依赖:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
应用启动类
package com.tommy.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* Hello world!
*
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication
{
/**
* 注意:
* 1.启动之后访问的地址是:http://localhost:8761/,不是http://localhost:8761/eureka。
* 2.注意父pom.xml中spring-cloud-dependencies的版本,可能导致子工程中找不到对应版本。这里使用的版本Edgware.SR1是Ok的。
* @param args
*/
public static void main( String[] args )
{
SpringApplication.run(EurekaApplication.class,args);
}
}
通过@EnableEurekaServer注解启动一个服务注册中心
application.yml
server:
port: 8761
eureka:
client:
# 表示是否将自己注册到Eureka Server,默认为true.由于当前应用就是Eureka Server,故而设置为false.
register-with-eureka: false
# 表示是否从Eureka Server获取注册信息,默认为true.因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,这里设置为false
fetch-registry: false
service-url:
# 设置Eureka Server的地址,查询服务和注册服务都需要依赖这个地址.默认是http://localhost:8761/eureka/;多个地址可使用','风格.
defaultZone: http://localhost:8761/eureka
运行EurekaApplication,在浏览器输入http://localhost:8761/,可以看到下面的页面,还没有任何服务。