Spring Boot Dubbo Zookeeper(含ZK安装脚本)
简介
Dubbo
Common
公共依赖
<!-- 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>
- 定义接口(用户服务注册使用)
public interface HelloService {
String sayHello(String name);
}
Provider
首先需要依赖Common
文章来源:https://www.toymoban.com/news/detail-669806.html
- yml
dubbo:
application:
name: my-application
registry:
address: zookeeper://localhost:2181
- 实现定义的接口(Service是apache.dubbo)
@Service
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "Hello, " + name + "!";
}
}
- 启动类(@EnableDubbo)
@EnableDubbo
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Consumer
首先需要依赖Common
文章来源地址https://www.toymoban.com/news/detail-669806.html
- Controller(@Reference注解)
@RestController
public class HelloController {
@Reference
private HelloService helloService;
@GetMapping("/hello/{name}")
public String sayHello(@PathVariable String name) {
return helloService.sayHello(name);
}
}
Zookeeper
- 安装脚本
#!/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
到了这里,关于Spring Boot Dubbo Zookeeper的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!