【微服务架构 - 08 - Spring Cloud】02 服务注册与发现

冷不防 2022-03-22 14:52 299阅读 0赞

pom.xml


pom.xml 文件配置:

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  4. </dependency>

Application

启动一个服务注册中心,只需要一个注解 @EnableEurekaServer

  1. package com.yuu.hello.spring.cloud.eureka;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. /**
  6. * @Classname EurekaApplication
  7. * @Date 2019/1/28 21:12
  8. * @Created by Yuu
  9. */
  10. @SpringBootApplication
  11. @EnableEurekaServer
  12. public class EurekaApplication {
  13. public static void main(String[] args) {
  14. SpringApplication.run(EurekaApplication.class, args);
  15. }
  16. }

application.yml

Eureka 是一个高可用的组件,它么有后端缓存,每一个实例注册之后需要向注册中心发送心跳(因此可以再内存中完成),在默认情况下 Erueka Server 也是一个 Eureka Client,必须要指定一个 Server。

  1. spring:
  2. application:
  3. name: hello-spring-cloud-eurka
  4. server:
  5. port: 8761
  6. eurka:
  7. instance:
  8. hostname: localhost
  9. client:
  10. # 表示是否将自己注册到 Eureka, 因为要构建集群环境,需要将自己注册到集群,所以应该开启
  11. registerWithEurka: false
  12. # 表示是否从 Eureka 获取注册信息,如果是单一节点,不需要同步其他 Eureka 节点,则可以设置为 false, 但此处为集群,应该设置为 true
  13. fetchRegistry: false
  14. serviceUrl:
  15. defaultZone: http://${eurka.instance.hostname}:${server.port}/eurka/

通过 eureka.client.registerWithEureka:falsefetchRegistry:false 来表明自己是一个 Eureka Server。

操作界面


Eureka Server 是有界面得,启动工程,打开浏览器访问:

http://localhost:8761

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,299人围观)

还没有评论,来说两句吧...

相关阅读