Spring Boot Eureka服务注册简单理解

Spring Cloud Eureka (英 [,jʊ(ə)'riːkə])是 Spring Cloud Netflix 微 服 务 套 件 中 的 一 部 分, 它 基 于 Netflix Eureka 做 了 二 次 封 装, 主 要 负 责 完 成 微 服 务 架 构 中 的 服 务 治 理 功 能.

 

Eureka Server是一个包含所有客户端服务应用程序信息的应用程序。 每个Micro服务都将注册到Eureka服务器,Eureka服务器知道在每个端口和IP地址上运行的所有客户端应用程序。 Eureka Server也称为发现服务(Discovery Server)。

构建Eureka Server

  • pom.xml倒入对应的依赖 --
 <dependencies>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-eureka-server</artifactId>
  </dependency>
  • application.yml 文件如下 --
spring:
   application:
       name: eureka-server
server:
   port:8761


eureka:
   client:
      registerWithEureka: false  // 实例在eureka上的注册信息是否被其他服务发现,默认为false,即不在注册中心注册自己
      fetchRegistry: false  // client端是否从eureka注册表中读取注册表信息,默认为true,30S同步一次
      serviceUrl:
        defaultZone: http://localhost:8761/eureka/
   instance:
      prefer-ip-address: true  // 猜测主机名时,优先显示ip地址,默认为false
   server:
      enableSelfPreservation: false  // 此eureka服务器中关闭自我保护模式.所谓自我保护模式是指,出现网络分区、eureka在短时间内丢失过多客户端时,会进入自我保护模式,即一个服务长时间没有发送心跳,eureka也不会将其删除。默认为true.
  • 在生成并下载项目文件后,需要在启动类添加  @EnableEurekaServer 注解  --  
@SpringBootApplication
@EnableEurekaServer
public class EurekaserverApplication {
   public static void main(String[] args) {
      SpringApplication.run(EurekaserverApplication.class, args);
   }
}

服务注册

  • pom.xml导入对应依赖  --
<dependency>
<groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
  • application.yml 文件如下  --

spring:
   application:
      name: eurekaclient

   server:
      port: 8888
eureka:
  client:
    serviceUrl:
      defaultZone:  http://localhost:8761/eureka/  // 同server端一致
  instance:
    preferIpAddress: true
    instance-id: ${ipAddress.client}:${server.port}


ipAddress:
  client: 192.0.0.0
  • 启动类添加 @EnableEurekaClient  注解  --

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

 

猜你喜欢

转载自blog.csdn.net/MR_L_0927/article/details/83861056