01.spring cloud 服务注册与发现 之 eureka (Greenwich.SR2)

- 服务发现
在计算机网络中,一种自动发现设备或服务的技术,通过服务发现协议实现。

- 服务注册
在计算机网络里,为了更好地治理多个设备或服务,这些设备或服务会主动或者被动注册到注册中心,以便服务被发现和消费。
常见注册中心:

  • apache zookeeper
  • netfix eureka
  • consul

实现方式:

  • 中心化
    注册中心为集群
  • 去中心化
    实现方式如区块链

Eureka

1 编写Eureka server

1.1 生成 ArtifactId 是 microsoft-discovery-eureka 的工程,添加 Eureka Server的依赖

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

最新配置方式可参考:https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server

1.2 启动类添加注解

@EnableEurekaServer

1.3 配置yml

server:
  port: 8791

eureka:
  client:
    register-with-eureka: false #是否将自己注册到 Eureka Server
    fetch-registry: false #是否从 Eureka Server 获取注册信息。目前做的是单点,不需要同步其他节点的信息
    service-url: 
      defaultZone: http://localhost:8791/eureka/ # 与 Eureka Server 交互地址,多个可用,分割; 
      # 注意 不要用 default-zone,会默认 8761端口

1.4 访问 localhost:8791

在这里插入图片描述

2 将服务注册到Eureka Server上

2.1 新建工程 microsoft-provider-user,添加依赖

<!-- Eureka 服务发现与注册客户端依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

2.2 在application.yml中添加配置

spring:
  application:
    name: microsoft-provider-user  #注册到服务中心的名字

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8791/eureka/  # 注意 不要用 default-zone,会默认 8761端口
  instance:
    prefer-ip-address: true  #将自己的IP注册到Eureka Server;否则则是注册 操作系统的hostname

2.3 启动了添加注解

@EnableDiscoveryClient    //该注解支持所有的客户端

或者

@EnableEurekaClient    //该注解只支持Eureka

2.4 注意需要添加web包依赖,否则注册会失败

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

2.5 同理新建工程 microsoft-consumer-movie

在这里插入图片描述

3 Eureka Server 高可用

eureka客户端会定时的到服务端获取注册信息表中的数据并缓存到本地,微服务在消费远程API时总是使用本地缓存。因此,一般来说即使Eureka Server发生宕机,也不影响服务之间调用。但是,若在Eureka Server宕机期间也有客户端出现宕机,本地缓存得不到即使更新的话,那就会影响到整个系统了。所以需要Eureka Server 集群化。

eureka可以通过运行多个实例并相互注册的方式实现高可用。

在前文基础上构建双节点的Eureka Server 集群

3.1 复制 microsoft-discovery-eureka 工程,命名为 microsoft-discovery-eureka-ha

3.2 配置系统的hosts文件

127.0.0.1 peer1 peer2

3.3 配置applicatin.yml,让其互相注册


spring:
  application:
    name: microsoft-discovery-eureka-ha
---
spring:
  profiles: peer1 # 指定profile = peer1
server:
  port: 8761
eureka:
  instance:
    hostname: peer1 # 指定当profile = peer1时,主机名是peer1
  client:
    service-url:
      defaultZone: http://peer2:8762/eureka/  # 将自己注册到peer2这个eureka上
---
spring:
  profiles: peer2
server:
  port: 8762
eureka:
  instance:
    hostname: peer2
  client:
    service-url:
      defualtZone: http://peer1:8761/eureka/

# --- 进行分割

3.4 打包项目,并制定profile启动工程

java -jar microsoft-discovery-eureka-ha-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1

java -jar microsoft-discovery-eureka-ha-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2

使用 spring.profiles.active= 指定使用的profile
分别访问 localhost:8761 及 localhost:8762查看注册结果
在这里插入图片描述

3.5 将应用注册到集群

只需将客户端的 yml 配置成多个

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云上凯歌

好活,当赏!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值