Spring Cloud Eureka

Eureka 介绍

Eureka是一个服务注册和发现模块。
Eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳。

Eureka 注册中心使用

Maven 依赖

参考: Spring-Cloud-Edgware-Release-Notes, MVN Repository

Eureka Server
  • spring-cloud-starter-eureka-server(Spring Cloud Edgware 之前的版本)
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
        <version>1.4.7.RELEASE</version>
    </dependency>
    

    1.0.0-RELEASE-1.4.7.RELEASE

  • spring-cloud-starter-netflix-eureka-server(Spring Cloud Edgware和它之后的版本)
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        <version>3.0.3</version>
    </dependency>
    

    1.4.0-RELEASE-2.2.9.RELEASE
    3.0.0-??

Eureka Client
  • spring-cloud-starter-eureka(Spring Cloud Edgware 之前的版本)
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
        <version>1.4.7.RELEASE</version>
    </dependency>
    

    1.0.0-RELEASE-1.4.7.RELEASE

  • spring-cloud-starter-netflix-eureka-client(Spring Cloud Edgware和它之后的版本)
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        <version>3.0.3</version>
    </dependency>
    

    1.4.0-RELEASE-2.2.9.RELEASE
    3.0.0-??

Eureka 配置

单节点配置
Eureka Server 配置
server:
  port: 1111

spring:
  application:
    name: eureka-server

eureka:
  instance:
    hostname: localhost
  client:
    # 关闭向注册中心注册自己的信息
    register-with-eureka: false
    # 关闭向注册中心获取注册表信息
    fetch-registry: false
    service-url: 
      defaultZone: http://${eureka.instance.hostname}/${server.port}/eureka/

注意: defaultZone不能写成default-zone,因为service-url是Map类型.

Eureka Client 配置
server:
  port: 9990
spring:
  application:
    name: eureka-client
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:1111/eureka/
多节点配置
hosts 配置(可选)
127.0.0.1	server1
127.0.0.1	server2
127.0.0.1	server3
Eureka Server 配置
spring:
  application:
    name: eureka-server

---
spring:
  application:
    name: eureka-server
  profiles: server1

server:
  address: server1
  port: 1111

eureka:
  instance:
    hostname: server1
  client:
    service-url:
      defaultZone: http://server2:2222/eureka/,http://server3:3333/eureka/

---
spring:
  application:
    name: eureka-server
  profiles: server2

server:
  address: server2
  port: 2222

eureka:
  instance:
    hostname: server2
  client:
    service-url:
      defaultZone: http://server1:1111/eureka/,http://server3:3333/eureka/

---
spring:
  application:
    name: eureka-server
  profiles: server3

server:
  address: server3
  port: 3333

eureka:
  instance:
    hostname: server3
  client:
    service-url:
      defaultZone: http://server1:1111/eureka/,http://server2:2222/eureka/

因为是多节点集群,相互间要获取注册中心信息,所有不需要设置register-with-eurekafetch-registryfalse(其值默认为true)

Eureka Client 配置
server:
  port: 9990
spring:
  application:
    name: eureka-client
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:1111/eureka/

客户端defaultZone可以填写注册中心集群的一个,多个或者所有

在这里插入图片描述

别忘了给启动程序添加注解 @org.springframework.cloud.client.discovery.EnableDiscoveryClient

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值