Spring Cloud第二篇 创建一个Eureka Server

在Spring Cloud实现一个Eureka Server是一件非常简单的事情。下面我们来写一个Eureka Server DEMO。

编码

(1) 首先创建一个Maven工程,添加内容如下:

 
    
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0 </modelVersion>
<groupId>com .itmuch .cloud </groupId>
<artifactId>microservice-discovery-eureka </artifactId>
<version>0.0.1-SNAPSHOT </version>
<packaging>jar </packaging>
<!-- 引入spring boot的依赖 -->
<parent>
<groupId>org .springframework .boot </groupId>
<artifactId>spring-boot-starter-parent </artifactId>
<version>1.4.1.RELEASE </version>
</parent>
<properties>
<project.build.sourceEncoding>UTF- 8 </project.build.sourceEncoding>
<java.version>1.8 </java.version>
</properties>
<dependencies>
<dependency>
<groupId>org .springframework .cloud </groupId>
<artifactId>spring-cloud-starter-eureka-server </artifactId>
</dependency>
</dependencies>
<!-- 引入spring cloud的依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org .springframework .cloud </groupId>
<artifactId>spring-cloud-dependencies </artifactId>
<version>Camden.SR1 </version>
<type> pom </type>
<scope>import </scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 添加spring-boot的maven插件 -->
<build>
<plugins>
<plugin>
<groupId>org .springframework .boot </groupId>
<artifactId>spring-boot-maven-plugin </artifactId>
</plugin>
</plugins>
</build>
</project>

(2) 编写启动类,在启动类上添加@EnableEurekaServer 注解。

 
    
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication .run( EurekaApplication .class, args);
}
}

(3) 编写配置文件application.yml

 
    
server:
port: 8761
eureka:
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://localhost:8761/eureka/

这样就完成了一个简单的Eureka Server。简要说明一下application.yml中的配置项:

eureka.client.registerWithEureka :表示是否将自己注册到Eureka Server,默认为true。由于当前这个应用就是Eureka Server,故而设为false。
eureka.client.fetchRegistry :表示是否从Eureka Server获取注册信息,默认为true。因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,故而设为false。
eureka.client.serviceUrl.defaultZone :设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔。

Eureka的配置类所在类:

 
    
org .springframework .cloud .netflix .eureka .EurekaInstanceConfigBean
org .springframework .cloud .netflix .eureka .EurekaClientConfigBean
org .springframework .cloud .netflix .eureka .server .EurekaServerConfigBean

测试

启动工程后,访问:http://localhost:8761/ 。我们会发现此时还没有服务注册到Eureka上面,如下图:

eureka

该页面展示了Eureka的系统状态、当前注册到Eureka Server上的服务实例、一般信息、实例信息等。我们可以看到,当前还没有任何服务被注册到Eureka Server上。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值