服务注册与发现

Spring Cloud使用演示版本

Edgware.SR5版本,Spring Cloud 1.xxx版本。如果使用Spring Cloud 2.xxx版本,请注意有些依赖的变化。从Finchley版本之后,进入Spring Cloud 2.xxx。

[Spring Cloud 管理依赖链接](https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies )

概述

需要用到组件是Spring Cloud Netflix的Eureka,Eureka是一个服务注册与发现模块

创建服务注册中心

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>            

启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
* Eureka registry center application
**/
@EnableEurekaServer
@SpringBootApplication
public class EurekaRegistryApplication {

   public static void main(String[] args) {
       SpringApplication.run(EurekaRegistryApplication.class, args);
  }
}

application.yml

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


server:
port: 1111


eureka:
server:
  enable-self-preservation: false #关闭保护机制
  eviction-interval-timer-in-ms: 3000 #剔除失效服务间隔(默认60s)
instance:
  hostname: localhost
client:
  register-with-eureka: false #该应用是注册中心,代表不向注册中心注册自己
  fetch-registry: false #由于注册中心职责是维护服务实例,它并不需要检索服务
  serviceUrl:
    defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  healthcheck:
    enabled: true


spring:
application:
  name: eureka-registry

操作界面

Eureka Server是有界面,启动工程,访问:http://localhost:1111

原文地址:https://www.cnblogs.com/liuenyuan1996/p/10281602.html