springboot 2 eureka 注册中心的搭建

Eureka 是Netflix 的服务发现组件,本身是一个基于rest的服务,它包含Server 和Client 两部分。从而实现微服务的注册与发现,下面就粗略的写下eureka的使用

备注本文章的代码使用的项目地址 smaug-cloud

启动类

package smaug.eureka.service;

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

/**
 * Created by naonao on 18/7/2.
 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServiceApplication.class, args);
    }
}

@EnableEurekaServer 标志该启动类是一个Eureks Server

相关的配置都在resources 下
application.yml

spring:
  profiles:
    active: peer2
  application:
    name: smaug-eureka-service

其中

  • profiles.active 指的是 当前使用哪个配置文件
  • application.name 是指该服务的名称,即在注册中心的名字

application-peer1.yml

server:
  port: 3001
eureka:
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ipAddress}:${server.port}
    hostname: 127.0.0.1
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:3002/eureka
    register-with-eureka: true
    fetch-registry: true

application-peer2.yml

server:
  port: 3002
eureka:
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ipAddress}:${server.port}
    hostname: 127.0.0.1
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:3001/eureka
    register-with-eureka: true
    fetch-registry: true
  • server.port 指的是服务端口号
  • eureka.instance.instance-id 是服务的实例ID
  • eureka.instance.hostname 主机名
  • eureka.client.serviceUrl.defaultZone: 是要把本服务注册到哪个eureka的地址

那么依次将 profiles.active 设置为peer1 peer2
然后运行启动类 浏览器访问 peer1:3001 可以看到如下图片即注册中心的截图
这里写图片描述

  1. Eureka Server 和 Eureka Client 都可以认为是一个Java客户端 ,多个Eureka Server 之间 通过复制的方式, 来实现 服务注册表中数据的同步
  2. 微服务启动后周期性的(每30s)后向Eureka Server 发送心跳表明该服务还健在
  3. 当服务中心在一定时间内(默认90s)没有接收到某个微服务实例的心跳后就会默认该服务应宕机,并注销该服务

猜你喜欢

转载自blog.csdn.net/weixin_39526391/article/details/80894842
今日推荐