六、Eureka集群配置

集群配置:

集群就是指将很多服务器集中起来一起进行同一种服务,在客户端看来就像是只有一个服务器。集群可以利用多个计算机进行[并行计算]从而获得很高的计算速度,也可以用多个计算机做[备份],从而使得任何一个机器坏了整个系统还是能正常运行。

从我们的实验看来就是将Eureka的服务进行集中配置,一个坏了,把服务交给另外一个服务;

代码如下:

先配置Windows/system32/drivers/host里面的文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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 activate.navicat.com
203.208.50.161 dl.google.com
127.0.0.1 localhost7001.com
127.0.0.1 localhost7002.com
127.0.0.1 localhost7003.com

按照前面的7001的配置配置其他的7002和7003

配置如下:

7003

1
2
3
4
5
6
7
8
9
10
11
server:
port: 7003

eureka:
instance:
hostname: localhost7003.com
client:
register-with-eureka: false #是否向注册中心注册自己,由于自己就是注册中心,所以这个选项为false
fetch-registry: false #s 是否是注册中心
service-url:
defaultZone: http://localhost7001.com:7001/eureka/,http://localhost7002.com:7002/eureka/

7002

1
2
3
4
5
6
7
8
9
10
11
server:
port: 7002

eureka:
instance:
hostname: localhost7002.com
client:
register-with-eureka: false #是否向注册中心注册自己,由于自己就是注册中心,所以这个选项为false
fetch-registry: false #s 是否是注册中心
service-url:
defaultZone: http://localhost7003.com:7003/eureka/,http://localhost7001.com:7001/eureka/

7001

1
2
3
4
5
6
7
8
9
10
11
server:
port: 7001

eureka:
instance:
hostname: localhost7001.com
client:
register-with-eureka: false #是否向注册中心注册自己,由于自己就是注册中心,所以这个选项为false
fetch-registry: false #s 是否是注册中心
service-url:
defaultZone: http://localhost7002.com:7002/eureka/,http://localhost7003.com:7003/eureka/

需要注意的是,defaultZone的写法,位置末尾不可以有逗号和空格的出现,不然的话就会失败;出现一系列的错误,一个集群一个服务需要再default里面写上其他的地址

然后打开提供者8001,访问其中一个服务出现下面的情况就成功了

image-20201024153705263