服务端
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerConfiguration.class, args);
}
@EnableAutoConfiguration
@EnableEurekaServer
public static class EurekaServerConfiguration{}
}
Eureka
<!-- Eureka 服务发现与注册客户端依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- 自定义instanceId
- 官网资料
eureka:
instance:
instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}
启动多个Eureka应用
- 在Run/Debug Configurations勾选Allow paraller run
- 切换启动端口server.port
- 若与consul冲突可注释掉consul的依赖
Zookeeper
该版本使用的是zookeeper版本3.5.3-beta
- 下载地址:清华大学提供的镜像
- https://zookeeper.apache.org/releases.html
- 本文下载的是apache-zookeeper-3.6.1-bin.tar.gz
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-zookeeper</artifactId>
<version>2.2.3.RELEASE</version>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<packaging>pom</packaging>
<version>3.5.3-beta</version>
启动zookeeper服务端
- 启动命令:zkServer.cmd
- 第一次启动报错zoo.cfg文件丢失
f:\project\download\spring-cloud\apache-zookeeper-3.6.1-bin\bin>zkServer.cmd
f:\project\download\spring-cloud\apache-zookeeper-3.6.1-bin\bin>call "F:\javakit\java\jdk1.8.0_77"\bin\java "-Dzookeeper.log.dir=f:\project\download\spring-cloud\apache-zookeeper-3.6.1-bin\bin\..\logs" "-Dzookeeper.root.logger=INFO,CONSOLE" "-Dzookeeper.log.file=zookeeper-ylm-server-DESKTOP-OJI7U38.log" "-XX:+HeapDumpOnOutOfMemoryError" "-XX:OnOutOfMemoryError=cmd /c taskkill /pid %%p /t /f" -cp "f:\project\download\spring-cloud\apache-zookeeper-3.6.1-bin\bin\..\build\classes;f:\project\download\spring-cloud\apache-zookeeper-3.6.1-bin\bin\..\build\lib\*;f:\project\download\spring-cloud\apache-zookeeper-3.6.1-bin\bin\..\*;f:\project\download\spring-cloud\apache-zookeeper-3.6.1-bin\bin\..\lib\*;f:\project\download\spring-cloud\apache-zookeeper-3.6.1-bin\bin\..\conf" org.apache.zookeeper.server.quorum.QuorumPeerMain "f:\project\download\spring-cloud\apache-zookeeper-3.6.1-bin\bin\..\conf\zoo.cfg"
2020-08-29 12:43:29,738 [myid:] - INFO [main:QuorumPeerConfig@173] - Reading configuration from: f:\project\download\spring-cloud\apache-zookeeper-3.6.1-bin\bin\..\conf\zoo.cfg
2020-08-29 12:43:29,746 [myid:] - ERROR [main:QuorumPeerMain@98] - Invalid config, exiting abnormally
org.apache.zookeeper.server.quorum.QuorumPeerConfig$ConfigException: Error processing f:\project\download\spring-cloud\apache-zookeeper-3.6.1-bin\bin\..\conf\zoo.cfg
at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parse(QuorumPeerConfig.java:197)
at org.apache.zookeeper.server.quorum.QuorumPeerMain.initializeAndRun(QuorumPeerMain.java:124)
at org.apache.zookeeper.server.quorum.QuorumPeerMain.main(QuorumPeerMain.java:90)
Caused by: java.lang.IllegalArgumentException:
f:\project\download\spring-cloud\apache-zookeeper-3.6.1-bin\bin\..\conf\zoo.cfg file is missing
at org.apache.zookeeper.server.util.VerifyingFileFactory.doFailForNonExistingPath(VerifyingFileFactory.java:54)
at org.apache.zookeeper.server.util.VerifyingFileFactory.validate(VerifyingFileFactory.java:47)
at org.apache.zookeeper.server.util.VerifyingFileFactory.create(VerifyingFileFactory.java:39)
at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parse(QuorumPeerConfig.java:179)
... 2 more
Invalid config, exiting abnormally
2020-08-29 12:43:29,752 [myid:] - INFO [main:ZKAuditProvider@42] - ZooKeeper audit is disabled.
2020-08-29 12:43:29,796 [myid:] - ERROR [main:ServiceUtils@42] - Exiting JVM with code 2
f:\project\download\spring-cloud\apache-zookeeper-3.6.1-bin\bin>endlocal
f:\project\download\spring-cloud\apache-zookeeper-3.6.1-bin\bin>
- 解决报错:将zoo_sample.cfg重命名为zoo.cfg
f:\project\download\spring-cloud\apache-zookeeper-3.6.1-bin\conf 的目录
2020/08/29 12:38 <DIR> .
2020/08/29 12:38 <DIR> ..
2020/04/21 22:59 535 configuration.xsl
2020/04/21 22:59 3,435 log4j.properties
2020/04/21 22:59 1,148 zoo_sample.cfg
zoo.cfg
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/tmp/zookeeper
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
重新启动zookeeper服务端
- zookeeper启动占用的8080端口,与tomcat默认启动端口冲突
2020-08-29 12:51:33,370 [myid:] - INFO [main:AbstractConnector@330] - Started ServerConnector@598067a5{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
2020-08-29 12:51:33,396 [myid:] - INFO [main:NIOServerCnxnFactory@674] - binding to port 0.0.0.0/0.0.0.0:2181
访问http://127.0.0.1:8080/
启动zookeeper客户端
f:\project\download\spring-cloud\apache-zookeeper-3.6.1-bin\bin>zkCli.cmd
2020-08-29 13:14:35,278 [myid:] - INFO [main:ZooKeeper@1005] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@41a4555e
2020-08-29 13:14:35,304 [myid:] - INFO [main:X509Util@77] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
2020-08-29 13:14:35,364 [myid:] - INFO [main:ClientCnxnSocket@239] - jute.maxbuffer value is 1048575 Bytes
2020-08-29 13:14:35,378 [myid:] - INFO [main:ClientCnxn@1703] - zookeeper.request.timeout value is 0. feature enabled=false
Welcome to ZooKeeper!
2020-08-29 13:14:35,391 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1154] - Opening socket connection to server localhost/127.0.0.1:2181.
2020-08-29 13:14:35,392 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1156] - SASL config status: Will not attempt to authenticate using SASL (unknown error)
2020-08-29 13:14:35,395 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@986] - Socket connection established, initiating session, client: /127.0.0.1:49219, server: localhost/127.0.0.1:2181
JLine support is enabled
2020-08-29 13:14:35,469 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1420] - Session establishment complete on server localhost/127.0.0.1:2181, session id = 0x10055f222ec0000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
启动spring-cloud项目
- 在Run/Debug Configurations配置Program arguments:
--spring.profiles.active=zookeeper
启动报错
2020-08-29 13:45:53.816 INFO 16832 --- [ main] d.s.s.SpringCloudConfigClientApplication : The following profiles are active: zookeeper
...
Field autoServiceRegistration in org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration required a single bean, but 2 were found:
- zookeeperAutoServiceRegistration: defined by method 'zookeeperAutoServiceRegistration' in class path resource [org/springframework/cloud/zookeeper/serviceregistry/ZookeeperAutoServiceRegistrationAutoConfiguration.class]
- consulAutoServiceRegistration: defined by method 'consulAutoServiceRegistration' in class path resource [org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationAutoConfiguration.class]
- 解决方案
spring.cloud.consul.enabled=false # Consul 服务发现与注册改为失效,默认值是true
通过zookeeper客户端验证启动成功
- zookeeper是通过新增节点或目录方式来注册,新增services目录
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 1] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 2] ls /
[services, zookeeper]
[zk: localhost:2181(CONNECTED) 3] ls /services/config-client
[0ad5ba64-e855-4c62-ac40-d3b2c32fcfaa]
[zk: localhost:2181(CONNECTED) 4]
验证id和所有数据的一致性
- 访问http://127.0.0.1:9091/services/config-client
- 查看zookeeper客户端
[zk: localhost:2181(CONNECTED) 3] ls /services/config-client
[0ad5ba64-e855-4c62-ac40-d3b2c32fcfaa]
[zk: localhost:2181(CONNECTED) 4] get /services/config-client/0ad5ba64-e855-4c62-ac40-d3b2c32fcfaa
{"name":"config-client","id":"0ad5ba64-e855-4c62-ac40-d3b2c32fcfaa","address":"localhost","port":9091,"sslPort":null,"payload":{"@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance","id":"application-1","name":"config-client","metadata":{"instance_status":"UP"}},"registrationTimeUTC":1598679958140,"serviceType":"DYNAMIC","uriSpec":{"parts":[{"value":"scheme","variable":true},{"value":"://","variable":false},{"value":"address","variable":true},{"value":":","variable":false},{"value":"port","variable":true}]}}
[zk: localhost:2181(CONNECTED) 5]
Consul
- 官网https://www.consul.io/
- consul_1.8.3_windows_amd64.zip下载极慢
- 参考:consul_1.7.2_windows_64安装
- 链接: https://pan.baidu.com/s/1p1NxQjqWImVCunZt0Wju0w 提取码: hcx6
解压缩后启动consul
- Started HTTP server: address=127.0.0.1:8500 network=tcp
f:\project\download\spring-cloud>consul.exe agent -dev
==> Starting Consul agent...
Version: 'v1.7.2'
Node ID: '42e7eb11-5c6b-2b08-3762-ac581453bc2c'
Node name: 'DESKTOP-OJI7U38'
Datacenter: 'dc1' (Segment: '<all>')
Server: true (Bootstrap: false)
Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, gRPC: 8502, DNS: 8600)
Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)
Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false, Auto-Encrypt-TLS: false
==> Log data will now stream in as it occurs:
2020-08-29T14:26:09.888+0800 [DEBUG] agent: Using random ID as node ID: id=42e7eb11-5c6b-2b08-3762-ac581453bc2c
2020-08-29T14:26:10.117+0800 [DEBUG] agent.tlsutil: Update: version=1
2020-08-29T14:26:10.120+0800 [DEBUG] agent.tlsutil: OutgoingRPCWrapper: version=1
2020-08-29T14:26:10.121+0800 [INFO] agent.server.raft: initial configuration: index=1 servers="[{Suffrage:Voter ID:42e7eb11-5c6b-2b08-3762-ac581453bc2c Address:127.0.0.1:8300}]"
2020-08-29T14:26:10.121+0800 [INFO] agent.server.raft: entering follower state: follower="Node at 127.0.0.1:8300 [Follower]" leader=
2020-08-29T14:26:10.122+0800 [INFO] agent.server.serf.wan: serf: EventMemberJoin: DESKTOP-OJI7U38.dc1 127.0.0.1
2020-08-29T14:26:10.122+0800 [INFO] agent.server.serf.lan: serf: EventMemberJoin: DESKTOP-OJI7U38 127.0.0.1
2020-08-29T14:26:10.123+0800 [INFO] agent.server: Adding LAN server: server="DESKTOP-OJI7U38 (Addr: tcp/127.0.0.1:8300) (DC: dc1)"
2020-08-29T14:26:10.123+0800 [INFO] agent.server: Handled event for server in area: event=member-join server=DESKTOP-OJI7U38.dc1 area=wan
2020-08-29T14:26:10.123+0800 [INFO] agent: Started DNS server: address=127.0.0.1:8600 network=udp
2020-08-29T14:26:10.123+0800 [INFO] agent: Started DNS server: address=127.0.0.1:8600 network=tcp
2020-08-29T14:26:10.124+0800 [INFO] agent: Started HTTP server: address=127.0.0.1:8500 network=tcp
2020-08-29T14:26:10.126+0800 [INFO] agent: started state syncer
==> Consul agent running!
2020-08-29T14:26:10.126+0800 [INFO] agent: Started gRPC server: address=127.0.0.1:8502 network=tcp
2020-08-29T14:26:10.167+0800 [WARN] agent.server.raft: heartbeat timeout reached, starting election: last-leader=
2020-08-29T14:26:10.167+0800 [INFO] agent.server.raft: entering candidate state: node="Node at 127.0.0.1:8300 [Candidate]" term=2
2020-08-29T14:26:10.167+0800 [DEBUG] agent.server.raft: votes: needed=1
2020-08-29T14:26:10.168+0800 [DEBUG] agent.server.raft: vote granted: from=42e7eb11-5c6b-2b08-3762-ac581453bc2c term=2 tally=1
2020-08-29T14:26:10.170+0800 [INFO] agent.server.raft: election won: tally=1
2020-08-29T14:26:10.173+0800 [INFO] agent.server.raft: entering leader state: leader="Node at 127.0.0.1:8300 [Leader]"
2020-08-29T14:26:10.176+0800 [INFO] agent.server: cluster leadership acquired
Processing server acl mode for: DESKTOP-OJI7U38 - 0
2020-08-29T14:26:10.176+0800 [INFO] agent.server: New leader elected: payload=DESKTOP-OJI7U38
2020-08-29T14:26:10.178+0800 [INFO] agent.server: Cannot upgrade to new ACLs: leaderMode=0 mode=0 found=true leader=127.0.0.1:8300
2020-08-29T14:26:10.182+0800 [DEBUG] connect.ca.consul: consul CA provider configured: id=07:80:c8:de:f6:41:86:29:8f:9c:b8:17:d6:48:c2:d5:c5:5c:7f:0c:03:f7:cf:97:5a:a7:c1:68:aa:23:ae:81 is_primary=true
2020-08-29T14:26:10.193+0800 [INFO] agent.server.connect: initialized primary datacenter CA with provider: provider=consul
2020-08-29T14:26:10.194+0800 [INFO] agent.leader: started routine: routine="CA root pruning"
2020-08-29T14:26:10.195+0800 [DEBUG] agent.server: Skipping self join check for node since the cluster is too small: node=DESKTOP-OJI7U38
2020-08-29T14:26:10.197+0800 [INFO] agent.server: member joined, marking health alive: member=DESKTOP-OJI7U38
2020-08-29T14:26:10.312+0800 [DEBUG] agent: Skipping remote check since it is managed automatically: check=serfHealth
2020-08-29T14:26:10.314+0800 [INFO] agent: Synced node info
2020-08-29T14:26:12.181+0800 [DEBUG] agent.tlsutil: OutgoingRPCWrapper: version=1
2020-08-29T14:26:12.535+0800 [DEBUG] agent: Skipping remote check since it is managed automatically: check=serfHealth
2020-08-29T14:26:12.537+0800 [DEBUG] agent: Node info in sync
2020-08-29T14:26:12.538+0800 [DEBUG] agent: Node info in sync
访问consul页面
- http://127.0.0.1:8500/ui/dc1/services
小结
所有服务信息
- http:// h o s t : {host}: host:{port}/eureka/apps
某个应用信息
- http:// h o s t : {host}: host:{port}/eureka/apps/${application_name}
某个应用实例信息
- http:// h o s t : {host}: host:{port}/eureka/apps/ a p p l i c a t i o n n a m e / {application_name}/ applicationname/{instance_instanceId}
Eureka客户端特性
- 注册模式:异步
注册中心 | CAP特性 | 推荐规模
- Eureka | AP | <30K
- Zookeeper | CP | <30K
- Consul | AP/CP | <5K
- Nacos | AP/CP | 100K~200k