分布式必学springcloud--高可用Eureka解析

高可用Eureka    :

Eureka的高可用其实就是将自己作为服务向其他注册中心注册自己,这样形成一个互相注册的注册中心集群,达到高可用。

下面我们搭建两台互相注册的注册中心集群(Eureka最少2台服务,但在zookeeper中作为注册中心,高可用至少3台服务,因为zookeeper是投票机制需要选出一个leader)。

借鉴前面搭建的Eureka,这里我们搭建两台,修改配置文件:


?
1
2
3
4
5
6
7
8
9
10
11
12
13

EurekaServer1的配置文件:注册到2上
spring:
  application:
    name: EUREKAHA
---
server:
  port: 8761
spring:
  profiles: peer1
eureka:
  instance:
    hostname: peer1
  client:
    serviceUrl:
      defaultZone : http://peer2:8762/eureka/


?
1




EurekaServer2的配置文件:注册到1上

spring:
  application:
    name:
EUREKAHA server: port: 8762 spring: profiles: peer2 eureka: instance: hostname: peer2 client: serviceUrl: defaultZone : http://peer1:8761/eureka/

    




注意:因为我们代码中写的是peer1和peer2,这些是服务的名字,在电脑上默认没有这个名字,所以需要进行配置,否则读不出服务器。

Linux 需要再/etc/hosts文件对peer1和peer2进行转换;

windows 需要C:\Windows\System32\drivers\etc 下的hosts文件进行转换

-------------------------------------------------------------------------------------------------------------

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1       localhost
# ::1             localhost
127.0.0.1       localhost
127.0.0.1       peer1 peer2

------------------------------------------------------------------------------------------------------------



下面我们启动所有的注册中心和提供者:

注册中心 https://peer1:8761/:



注册中心 https://peer2:8762/ :


提供者配置文件修改:

?
1
2
3
4
5
6
7
8
9
server:
   port: 9002
eureka:
   client:
     serviceUrl:
       defaultZone: https: //peer1:1111/eureka/,http//peer2:2222/eureka/

spring:

 
application:
 
  
name: provider


供者的配置文件中添加了所有的注册中心。

启动提供者,再去看看2个服务节点:

peer2:



提供者的配置文件中添加了所有的注册中心。从图中我们可以看出,提供者同时候注册到了注册中心1和注册中心2上,然后我们把注册中心二关闭后,发现提供者依旧在注册中心1上达到了高可用的状态。

想更多了解spring-cloud-starter-netflix-eureka可以去官网看看:点击打开链接http://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-eureka-server.html



另外在eureka首页  显示应用的ip,配置如下:

server:
  port: 8011
eureka:
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:}${server.port}
  client:
    service-url:
      defaultZone: http://user:password123@localhost:8761/eureka
spring:
  application:
    name: microservice-consumer-movie-ribbon
user:
   userServicePath: http:localhost:7900/simple/
microservice-provider-user:
  ribbon:
    NFLoadBalanceRuleClassName: com.netflix.loadbalance.RandomRule

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值