简介
Dubbo
Common
公共依赖
<dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starterartifactId> <version>2.3.0.RELEASEversion>dependency><dependency> <groupId>org.apache.dubbogroupId> <artifactId>dubbo-spring-boot-starterartifactId> <version>2.7.8version>dependency><dependency> <groupId>org.apache.dubbogroupId> <artifactId>dubbo-registry-zookeeperartifactId> <version>2.7.8version>dependency>
- 定义接口(用户服务注册使用)
public interface HelloService { String sayHello(String name);}
Provider
首先需要依赖Common
- yml
dubbo: application: name: my-application registry: address: zookeeper://localhost:2181
- 实现定义的接口(Service是apache.dubbo)
@Servicepublic class HelloServiceImpl implements HelloService { @Override public String sayHello(String name) { return "Hello, " + name + "!"; }}
- 启动类(@EnableDubbo)
@EnableDubbo@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
Consumer
首先需要依赖Common
- Controller(@Reference注解)
@RestControllerpublic 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文件 并 设置PIDtouch /data/server/zookeeper-3.4.14/data/myid;# PID,与MAP key相同echo 1 > /data/server/zookeeper-3.4.14/data/myid;# 复制默认配置,改为zoo.cfgcp /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;# 写入dataLogDirdecho 'dataLogDir=/data/server/zookeeper-3.4.14/logs' >> /data/server/zookeeper-3.4.14/conf/zoo.cfg;# 单节点,只需要写入单个即可declare -A mapmap[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
来源地址:https://blog.csdn.net/Cy_LightBule/article/details/131788005