一、docker安装zookeeper
1.在docker中安装zookeeper
docker pull zookeeper
2.查看zookeeper是否安装成功
docker images
3.运行zookeeper镜像
docker run -d -p 2181:2181 --name some-zookeeper zookeeper
4.查看是否成功运行
docker ps
5.启动zookeeper
docker exec -it f229e81e13bd bash
./bin/zkCli.sh
*先不要关闭centos系统,后续IDEA服务注册zookeeper还需要用到,放着不动就可以了
二、 IDEA服务注册进zookeeper
1.新建好maven项目后改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>springcloud</artifactId>
<groupId>org.example</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.example</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</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.mybatis.spring.boot </groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</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>
</dependency>
</dependencies>
</project>
2.修改yml文件(数据库的配置信息不重要)
server:
port: 端口号
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
connect-string: 服务器地址:端口号
datasource:
type: com.alibaba.druid.pool.DruidDataSource
username: 数据库用户名
password: 密码
driver-class-name: org.gjt.mm.mysql.Driver
url: 数据库url
3.设置启动类
package com.lpy.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);
}
}
4. 启动项目
5.查看服务是否注册成功进入zookeeper(再次打开centos服务器)
ls /
ls /services
可以发现,已经成功注册进入zookeeper中,服务名称为cloud-provider-payment。