我把原来自己写的一个单体应用(论坛)小小地拆了一下。原来是整个项目打成war包仍到tomcat的webapps目录下访问,也就是基础公共类、接口类、controller类、dao类都属于同级目录。现在拆成四部分:
zookeeper
首先zookeeper依赖Java环境,需要先配下Java环境变量,网上有很多。
因为是我本地对项目改造,所以先在windows上搭下环境,先搞zk。
http://www.apache.org/dyn/closer.cgi/zookeeper/
访问zookeeper官网,下载
三个随便一个都行。进去后下载这个带bin的压缩文件。我是挺早下的zookeeper,是3.5.8版本的
解压:
进入解压文件目录 /apache-zookeeper-3.5.8-bin\apache-zookeeper-3.5.8-bin\conf
找到文件zoo_sample.cfg 复制重命名一份叫zoo.cfg.然后修改下zoo.cfg文件的内容 我改成下面这些:
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/tmp/zookeeper
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
上面是zoo.cfg的文件内容,红色的代表可修改,如果没有哪一项,要手动加上。
启动zookeeper,bin目录下,因为我是在windwos操作的,找到zkServer.cmd 执行即可:
ps:如果执行zkServer.cmd 命令脚本失败可以修改下zkServer.cmd文件内容,加上这两行在末尾:保存再执行可以看的报错...
pause
endlocal
如果是这样的,代表启动好了。
添加dubbo的pom依赖:
父级pom中定义dubbo版本、spring boot版本 等:
<dubbo-version>2.7.7</dubbo-version>
<spring-boot.version>2.3.0.RELEASE</spring-boot.version>
关于dubbo的依赖(spring等其他的就不贴了):这里使用zookeeper作注册中心所以需要依赖 dubbo-dependencies-zookeeper ps说一句废话:看坐标可以看出这个是apache提供的
<!-- dubbo-springboor -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo-version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo-version}</version>
<type>pom</type>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- bouncycastle-->
上面关于dubbo的依赖需要在 controller和svsImpl两个工程中引入。
有关的配置:配置文件我用application.yml
controller工程的application.yml配置 增加:
dubbo:
application:
name: hehe
scan:
base-packages: com.dabai.mytwo.controller
protocol:
port: 20881
name: dubbo
registry:
#zookeeper注册中心地址
address: zookeeper://localhost:2181
svsImpl工程的application.yml配置 增加:
#dubbo配置
dubbo:
application:
name: hehe
scan:
base-packages: com.dabai.serviceImpl
protocol:
port: 20881
name: dubbo
registry:
#zookeeper注册中心地址
address: zookeeper://localhost:2181
使用:
svsImpl作为服务的提供者,需要明确接口的具体实现,在接口的实现类上加上注解 :@DubboService 如
/**
* @author dabai:
* <p>
* 类说明 commentService实现类
*/
@DubboService
@Component
public class CommentServiceImpl implements CommentService {}
controller作为服务的消费者,使用dubbo提供的注解,在controller类上加上注解:@DubboReference 如:
@Controller
public class CommentController {
@DubboReference
private CommentService commentService;
}
参考文档:
https://dubbo.apache.org/zh/docs/
ps:@DubboService,@DubboReference 两注解还包含其它很多属性可以设置,如口令等
SpringBoot+dubbo+zookeeper(windows )
原文:https://www.cnblogs.com/notably/p/14361393.html