docker安装zookeeper
- docker pull zookeeper
- docker run --privileged=true -d --name zookeeper --publish 2181:2181 -d zookeeper:latest
1、创建模块cloud-payment-8004
2、设置pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud2020</artifactId>
<groupId>com.laoxu.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-provider-payment8004</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.laoxu.springcloud</groupId>
<artifactId>cloud-api-common</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
3、配置注册进远程zookeeper
server:
port: 8004
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
connect-string: 47.244.153.137:2181
4、创建启动类
package com.laoxu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8004 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8004.class, args);
}
}
5、启动8004服务
6、查看服务器zookeeper
进入容器的执行命令:
- docker exec -it zookeeper zkCli.sh
- [zk: localhost:2181(CONNECTED) 0] ls /
- [services, zookeeper]
- [zk: localhost:2181(CONNECTED) 2] ls /services
- [cloud-provider-payment]
可以看到8004的application.name,表示注册成功!
查看注册服务信息:
- [zk: localhost:2181(CONNECTED) 3] ls /services/cloud-provider-payment
- [0b4fd6e6-802c-4fed-9a6b-a4fb4450094f]
- [zk: localhost:2181(CONNECTED) 4] ls /services/cloud-provider-payment/0b4fd6e6-802c-4fed-9a6b-a4fb4450094f
- []
- [zk: localhost:2181(CONNECTED) 5] get /services/cloud-provider-payment/0b4fd6e6-802c-4fed-9a6b-a4fb4450094f
- {"name":"cloud-provider-payment","id":"0b4fd6e6-802c-4fed-9a6b-a4fb4450094f","address":"XSJ-2005","port":8004,"sslPort":null,"payload":{"@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance","id":"application-1","name":"cloud-provider-payment","metadata":{}},"registrationTimeUTC":1598709293577,"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) 6]
json串就是注册服务的节点信息