本文主要是介绍分布式必学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: peer1client: 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文件进行转换
-------------------------------------------------------------------------------------------------------------
#
# 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
# 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/ 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
这篇关于分布式必学springcloud--高可用Eureka解析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!