文章目录
- [Spring Boot Dubbo Zookeeper](#Spring Boot Dubbo Zookeeper)
Spring Boot Dubbo Zookeeper
简介
Dubbo
Common
公共依赖
xml
<!-- Spring Boot Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
<!-- Dubbo Starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
<!-- ZooKeeper 注册中心 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
<version>2.7.8</version>
</dependency>
- 定义接口(用户服务注册使用)
java
public interface HelloService {
String sayHello(String name);
}
Provider
首先需要依赖Common
- yml
yml
dubbo:
application:
name: my-application
registry:
address: zookeeper://localhost:2181
- 实现定义的接口(Service是apache.dubbo)
java
@Service
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "Hello, " + name + "!";
}
}
- 启动类(@EnableDubbo)
java
@EnableDubbo
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Consumer
首先需要依赖Common
- Controller(@Reference注解)
java
@RestController
public class HelloController {
@Reference
private HelloService helloService;
@GetMapping("/hello/{name}")
public String sayHello(@PathVariable String name) {
return helloService.sayHello(name);
}
}
Zookeeper
- 安装脚本
shell
#!/bin/bash
#### 需要先装JDK ####
#### 需要先装JDK ####
#### 需要先装JDK ####
cd /data/server;
# 下载一个版本儿
wget https://archive.apache.org/dist/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz;
# 创建目录
mkdir -p /data/server/apache-zookeeper-3.4.14;
# 解压到/usr/local下
tar -zxvf zookeeper-3.4.14.tar.gz -C /data/server/;
# 新增zk目录
mkdir -p /data/server/zookeeper-3.4.14/data;
mkdir -p /data/server/zookeeper-3.4.14/logs;
# 新增PID文件 并 设置PID
touch /data/server/zookeeper-3.4.14/data/myid;
# PID,与MAP key相同
echo 1 > /data/server/zookeeper-3.4.14/data/myid;
# 复制默认配置,改为zoo.cfg
cp /data/server/zookeeper-3.4.14/conf/zoo_sample.cfg /data/server/zookeeper-3.4.14/conf/zoo.cfg;
# 修改核心配置zoo.cfg
# 替换
dataDir_old='/tmp/zookeeper';
dataDir_url='/data/server/zookeeper-3.4.14/data';
sed -ig "s#${dataDir_old}#${dataDir_url}#" /data/server/zookeeper-3.4.14/conf/zoo.cfg;
# 写入dataLogDird
echo 'dataLogDir=/data/server/zookeeper-3.4.14/logs' >> /data/server/zookeeper-3.4.14/conf/zoo.cfg;
# 单节点,只需要写入单个即可
declare -A map
map[1]="127.0.0.1"
for key in ${!map[@]};do
echo "server."${key}"="${map[${key}]}":2888:3888" >> /data/server/zookeeper-3.4.14/conf/zoo.cfg;
done
# 查看下配置
cat /data/server/zookeeper-3.4.14/conf/zoo.cfg;
# 启动
# 不是root,需要source /etc/profile!!
# 不是root,需要source /etc/profile!!
# 不是root,需要source /etc/profile!!
/data/server/zookeeper-3.4.14/bin/zkServer.sh start
# 查看状态
/data/server/zookeeper-3.4.14/bin/zkServer.sh status
#### 显示下面内容就是启动成功了 ####
#ZooKeeper JMX enabled by default
#Using config: /data/server/zookeeper-3.4.14/bin/../conf/zoo.cfg
#Mode: standalone
# 日志存储位置
tail -100f /data/server/zookeeper-3.4.14/zookeeper.out
###### 问题
###1. org.apache.zookeeper.server.admin.AdminServer$AdminServerException: Problem starting AdminServer on address 0.0.0.0, port 8080 and command URL /commands
### 解决办法:https://www.jianshu.com/p/181a69836fe9