spring Cloud--搭建Eureka服务注册中心

首先创建一个 Maven 项目,取名为 eureka-server,在 pom.xml 中配置 Eureka 的依赖信息,代码如下所示。
  1. <!-- Spring Boot -->
  2. <parent>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-parent</artifactId>
  5. <version>2.0.6.RELEASE</version>
  6. <relativePath />
  7. </parent>
  8.  
  9. <dependencies>
  10. <!-- eureka -->
  11. <dependency>
  12. <groupId>org.springframework.cloud</groupId>
  13. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  14. </dependency>
  15. </dependencies>
  16.  
  17. <!-- Spring Cloud -->
  18. <dependencyManagement>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.cloud</groupId>
  22. <artifactId>spring-cloud-dependencies</artifactId>
  23. <version>Finchley.SR2</version>
  24. <type>pom</type>
  25. <scope>import</scope>
  26. </dependency>
  27. </dependencies>
  28. </dependencyManagement>

创建一个启动类 EurekaServerApplication,代码如下所示。

 
  1. @EnableEurekaServer
  2. @SpringBootApplication
  3. public static void main(String[] args) {
  4. SpringApplication.run(EurekaServer Application.class, args);
  5. }
  6. }

这里所说的启动类,跟我们之前讲的 Spring Boot 几乎完全一样,只是多了一个 @EnableEurekaServer 注解,表示开启 Eureka Server。

接下来在 src/main/resources 下面创建一个 application.properties 属性文件,增加下面的配置:

spring.application.name=eureka-server
server.port=8761
# 由于该应用为注册中心, 所以设置为false, 代表不向注册中心注册自己
eureka.client.register-with-eureka=false
# 由于注册中心的职责就是维护服务实例, 它并不需要去检索服务, 所以也设置为 false
eureka.client.fetch-registry=false

eureka.client.register-with-eureka 一定要配置为 false,不然启动时会把自己当作客户端向自己注册,会报错。

接下来直接运行 EurekaServerApplication 就可以启动我们的注册中心服务了。我们在 application.properties 配置的端口是 8761,则可以直接通过 http://localhost:8761/ (http://localhost%EF%BC%9A8761/) 去浏览器中访问,然后便会看到 Eureka 提供的 Web 控制

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值