一.搭建Eureka Server服务命名为 yang-cloud-eureka
先创建父工程yang-parent中引入Spring Cloud 依赖。
Spring Cloud 是一个综合的项目,下面有很多子项目,比如eureka子项目
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> |
二. yang-cloud-eureka工程pom.xml中引入依赖
<dependencies> <!--Eureka server依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> |
注意:在父工程的pom文件中手动引入jaxb的jar,因为Jdk9之后默认没有加载该模块,EurekaServer使用到,所以需要手动导入,否则EurekaServer服务无法启动。
父工程引入如下依赖:
<!--引入Jaxb,开始--> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version>2.2.11</version> </dependency> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.2.11</version> </dependency> <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> <version>2.2.10-b140310.1920</version> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1.1</version> </dependency> <!--引入Jaxb,结束--> |
三. 在yml文件中配置Eureka server服务端口,服务名等信息
server: port: 9200 spring: application: name: yang-cloud-eureka eureka: client: #Eureka server本身也是eureka的一个客户端,因为在集群下需要与其他eureka server进行数据的同步 service-url: #定义eureka server url,如果是集群情况下defaultZone设置为集群下的别的Eureka Server的地址,多个地址使用“,”隔开 defaultZone: http://localhost:9200/eureka register-with-eureka: false # 表示是否向Eureka中心注册自己的信息,因为自己就是Eureka Server所以不进行注册,默认为true fetch-registry: false # 是否查询/拉取Eureka Server服务注册列表,默认为true instance: hostname: localhost # 当前eureka实例的主机名
|
四. 编写启动类,声明当前服务为Eureka注册中心
package com.yang.eureka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication 声明本项目是一个Eureka服务 @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class,args); } } |
五.访问http://127.0.0.1:9200,如果看到 Eureka注册中心后台界面,则表明EurekaServer发布成功。
ps:这是单例Eureka Server服务注册中心,根据自己的需求可在配置文件添加自己的配置搭建集群。最后欢迎大家探讨学习。