springboot 整合 dubbo
Dubbo简介
Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分业务的架构,使用这种方式可以使各个业务之间解耦合(或者最大限度地松耦合)。
简单来说Dubbo:
1:一款分布式服务框架
2:高性能和透明化的RPC远程服务调用方案
3:SOA服务治理方案
Dubbo架构
这里写图片描述
Provider: 暴露服务的服务提供方。
Consumer: 调用远程服务的服务消费方。
Registry: 服务注册与发现的注册中心。
Monitor: 统计服务的调用次数和调用时间的监控中心。
调用流程
1:服务容器负责启动,加载,运行服务提供者。
2:服务提供者在启动时,向注册中心注册自己提供的服务。
3:服务消费者在启动时,向注册中心订阅自己所需的服务。
4:注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
5:服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
6:服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
一、准备工作
1:zookeeper环境搭建
目前是在windows中安装的zookeeper
首先进入官网http://dubbo.apache.org/zh-cn/blog/download.html下载好zookeeper的安装压缩包
目前我用的版本是3.6.3
进入解压后的目录中再进入bin目录有专门在windows中使用zkCli.cmd(客户端)和zkServer.cmd(服务端)
我们先在该目录下打开cmd,然后运行zkServer.cmd
会发现运行的时候会出现错误,说在cof\目录下找不到zoo.cfg文件
原因确实是在cof目录下没有zoo.cfg这个文件,但是有一个zoo_sample.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
dataDir为zookeeper数据的存储目录,因为原始路径是linux下的路径,但我这是安装在windows中,所以需要修改一下
首先在zookeeper的安装目录下建一个data目录
然后修改zoo.cfg文件中dataDir的值为…/data(用的相对路径)
2、clientPort=2181 2181为zookeeper的默认端口
修改完成之后再在cmd中运行一下zkServer.cmd就能启动成功了
以下这一步可以不用做,因为写代码的时候可以在idea创建可查看节点(开启服务)
不要关闭运行着zookeeper服务端的这个cmd窗口,我们再在该目录下重新开启一个cmd窗口运行zkCli.cmd(客户端)进行连接 zookeeper服务器
2:ZK UI 环境搭建
zookeeper管理控制台(zkui)
一 下载jar包
zkui的源码包,我们可以在github上进行下载,如下:
https://github.com/DeemOpen/zkui.git
目前我下载的本地位置:
通过idea使用mvn命令进行编译、构建和打包,如下:
mvn clean install
可以很明显看到maven打包后生成了zkui-2.0-SNAPSHOT.jar和zkui-2.0-SNAPSHOT-jar-with-dependencies.jar两个文件,
其中zkui-2.0-SNAPSHOT-jar-with-dependencies.jar才是我们需要的jar文件
二 启动zkui
修改zkui的配置文件config.cfg
zkui需要连接的zookeeper集群的IP地址和端口,访问ui的用户名和密码,以及zkui监听的端口号。如下:
#Server Port
serverPort=9090
#Comma seperated list of all the zookeeper servers
zkServer=localhost:2181,localhost:2181
#Http path of the repository. Ignore if you dont intent to upload files from repository.
scmRepo=http://myserver.com/@rev1=
#Path appended to the repo url. Ignore if you dont intent to upload files from repository.
scmRepoPath=//appconfig.txt
#if set to true then userSet is used for authentication, else ldap authentication is used.
ldapAuth=false
ldapDomain=mycompany,mydomain
#ldap authentication url. Ignore if using file based authentication.
ldapUrl=ldap://<ldap_host>:<ldap_port>/dc=mycom,dc=com
#Specific roles for ldap authenticated users. Ignore if using file based authentication.
ldapRoleSet={"users": [{ "username":"domain\\user1" , "role": "ADMIN" }]}
userSet = {"users": [{ "username":"admin" , "password":"admin","role": "ADMIN" },{ "username":"appconfig" , "password":"appconfig","role": "USER" }]}
#Set to prod in production and dev in local. Setting to dev will clear history each time.
env=prod
jdbcClass=org.h2.Driver
jdbcUrl=jdbc:h2:zkui
jdbcUser=root
jdbcPwd=manager
#If you want to use mysql db to store history then comment the h2 db section.
#jdbcClass=com.mysql.jdbc.Driver
#jdbcUrl=jdbc:mysql://localhost:3306/zkui
#jdbcUser=root
#jdbcPwd=manager
loginMessage=Please login using admin/manager or appconfig/appconfig.
#session timeout 5 mins/300 secs.
sessionTimeout=300
#Default 5 seconds to keep short lived zk sessions. If you have large data then the read will take more than 30 seconds so increase this accordingly.
#A bigger zkSessionTimeout means the connection will be held longer and resource consumption will be high.
zkSessionTimeout=5
#Block PWD exposure over rest call.
blockPwdOverRest=false
#ignore rest of the props below if https=false.
https=false
keystoreFile=/home/user/keystore.jks
keystorePwd=password
keystoreManagerPwd=password
# The default ACL to use for all creation of nodes. If left blank, then all nodes will be universally accessible
# Permissions are based on single character flags: c (Create), r (read), w (write), d (delete), a (admin), * (all)
# For example defaultAcl={"acls": [{"scheme":"ip", "id":"192.168.1.192", "perms":"*"}, {"scheme":"ip", id":"192.168.1.0/24", "perms":"r"}]
defaultAcl=
# Set X-Forwarded-For to true if zkui is behind a proxy
X-Forwarded-For=false
zkui默认的用户名和密码是admin/manager,我为了方便修改为admin/admin。
现在使用如下命令启动zkui,如下:
java -jar target/zkui-2.0-SNAPSHOT-jar-with-dependencies.jar
访问:http://localhost:9090/