(三)SpringBoot+SpringCloud —— 高可用的Eureka注册中心

1、配置高可用的Eureka注册中心集群

以前使用的是独立模式的注册中心,为了防止注册中心崩溃使整个应用系统瘫痪,现采用集群的方式部署高可用的注册中心。java

建立两个属性文件,命名为application-server1.properties和application-server2.properties:linux

#application-server1.properties
spring.application.name=eureka-server-01
server.port=9001
eureka.instance.hostname=server1
eureka.client.serviceUrl.defaultZone=http://server2:9002/eureka/
#application-server2.properties
spring.application.name=eureka-server-02
server.port=9002
eureka.instance.hostname=server2
eureka.client.serviceUrl.defaultZone=http://server1:9001/eureka/

这么作的目的是要开启两个应用,一个端口为9001,一个为9002,相互注册,便是服务端又是客户端。git

须要注意的是hostname,须要在hostname文件中,加入映射,window系统在C:\Windows\System32\drivers\etc目录下,linux系统在/etc/hosts:github

# 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 server1
127.0.0.1 server2

经过maven install打包jar包,而后打开两个cmd,经过命令行执行:web

  • java -jar tbp-eureka-server-1.0-SNAPSHOT.jar --spring.profiles.active=server1
  • java -jar tbp-eureka-server-1.0-SNAPSHOT.jar --spring.profiles.active=server2

随后访问localhost:9001与localhost:9002,能够看到注册中心已经互相注册了:spring

2、将服务注册到Eureka注册中心集群

修改owl-bookstore-service-user工程的application.yml配置文件,将defaultZone设置为集群链接:bash

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:9001/eureka/,http://localhost:9002/eureka/
server:
  port: 7001
spring:
  application:
    name: SERVICE-USER

从新启动两个user服务,打开两个cmd,输入:app

  • java -jar owl-bookstore-service-user-1.0-SNAPSHOT.jar --server.port=7001
  • java -jar owl-bookstore-service-user-1.0-SNAPSHOT.jar --server.port=7002

查看注册中心,确认服务注册成功:maven

3、将消费端注册到Eureka注册中心集群

同服务的配置文件,这里也将owl-bookstore-web-console工程的application.yml注册到集群:测试

spring:
  application:
    name: owl-bookstore-web-console
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:9001/eureka/,http://localhost:9002/eureka/
server:
  port: 8000

测试如上图。

到此,Eureka Server的集群就已经搭建完毕了。

Github:https://github.com/XuePeng87/owl-bookstore

相关文章
相关标签/搜索