Dubbo的应用
Posted xiaowejie
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dubbo的应用相关的知识,希望对你有一定的参考价值。
导语:Dubbo是阿里巴巴的一个分布式服务的开源框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。
参考网站:http://dubbo.io/
1.dubbo-admin管理平台搭建(对消费者和生产者进行管理) zookeeper---服务注册中心
部署dubbo-admin:
软件下载:链接: http://pan.baidu.com/s/1midNOMk 密码: f2yy
1.安装jdk
2.安装tomcat
3.将dubbo-admin工程在tomcat上运行起来(要先运行zookeeper)
下载dubbo-admin包(dubbo-admin-2.5.4-SNAPSHOT.war)
找到 tomcat安装路径下的 .\\webapps\\ROOT目录,然后清空里面的所有文件
将dubbo-admin包(dubbo-admin-2.5.4-SNAPSHOT.war)解压到此文件中
在解压的文件中找到\\WEB-INF文件夹下的dubbo.properties文件,然后进行配置,默认属性配置如下:
- dubbo.registry.address=zookeeper://127.0.0.1:2181
- dubbo.admin.root.password=root
- dubbo.admin.guest.password=guest
然后启动tomcat(zookeeper已启动,此步tomcat要访问zookeeper,如果zk没有启动,tomcat会一直等待其启动)。在浏览器中输入localhost:8080,进入监控中心的管理界面(默认管理员账户密码为:root,root)
linux命令:
#清空ROOT里面的所有文件
yum install –y unzip
rm –rf /usr/local/tomcat/webapp/ROOT/* unzip dubbo-admin-2.5.4.war –d /usr/local/tomcat/webapp/ROOT
启动tomcat:./usr/local/tomcat/bin/startup.sh
安装zookeeper:(注意:zookeeper集群超过半数以上的机器挂掉,集群不可以,如:2n+1台server,只要有n+1台就可以使用。也就是你说的少于一半集群就无效了)
mkdir /home/lgp :创建包用于存放 zookeeper cp –rf zookeeper-3.4.6.tar.gz /home/lgp cd /home/lgp tar -xzvf zookeeper-3.4.6.tar.gz cd zookeeper-3.4.6/conf/ mv zoo_sample.cfg zoo.cfg vi zoo.cfg :参考详情
server.X=A:B:C # 其中X是一个数字, 表示这是第几号server. A是该server所在的IP地址. B配置该server和集群中的leader交换消息所使用的端口. C配置选举leader时所使用的端口. 这里的x是一个数字,与myid文件中的id是一致的。
单机版可以不用配置server.X=A:B:C
# 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=/home/xudan/zookeeper-3.4.6/data #myid文件存放的路径 dataLogDir=/home/xudan/zookeeper-3.4.6/logs #日志存放路径 # 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 server.1=Master:2555:3555 server.2=Slave1:2556:3556 server.3=Slave2:2557:3557 |
cd ..
mkdir data
mkdir logs
touch /data/myid
echo 1 > /home/lgp/ zookeeper-3.4.6/data/myid //设置myid的值为1
vi /etc/hosts //设置host
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 root
127.0.0.1 localhost
192.168.1.63 Master
192.168.1.53 Slave1
192.168.1.54 Slave2
b、集群(把单台zookeeper的配置copy到其他机器上,改下hosts和对应的myid,根据zoo.cfg的配置)
复制:对应的机器创建 /home/lgp
yum -y install openssh-clients
修改对应 myid ,etc/hosts
scp /home/lgp/zookeeper-3.4.6 192.168.1.81:/home/lgp //跨机器复制
启动服务:
service iptables stop:分别关闭对应机器的 iptables
cd /home/lgp/zookeeper-3.4.6/bin
./bin/zkServer.sh start :开启zookeeper
./bin/zkServer.sh status :状态查询
./bin/zkServer.sh stop :关闭zookeeper
2、dubbo整合Zookeeper
cd /usr/local/tomcat/webapp/ROOT/WEB-INF
vi dubbo.properties
dubbo.registry.address=zookeeper://192.168.1.63:2181?backup=192.168.1.53:2181,192.168.1.54:2181 //backup是配合zookeeper挂掉后的替代
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest
最后,先起zookeeper在启动tomcat
4.登录dubbo(账号密码都是:root)
2.zookeeper注册中心的配置安装(service在zookeeper上注册运行)
3.dubbo项目整合SpringMVC(maven)
1、下载jar:
maven导入所需jar包:dubbo、zookeeper、zkclient
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<!-- Zookeeper 用于分布式服务管理 -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.3</version>
</dependency>
<!-- Zookeeper 用于分布式服务管理 end -->
2、eclipse配置dubbo.xsd,解决标签不识别的问题
3、在.properties配置zookeeper的注册地址
dubbo.registry.address=192.168.1.72:2181\\,192.168.1.73:2182\\,192.168.1.74:2183
4、service提供服务:spring-dubbo-provider.xml(生产者)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns: dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="guduo-service-user" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry protocol="zookeeper" address="${dubbo.registry.address}" />
<!—本机 -->
<dubbo:protocol name="dubbo" port="20823" />
<!-- 监控中心配置,protocol="registry",表示从注册中心发现监控中心地址 -->
<dubbo:monitor protocol="registry"/>
<!-- 当ProtocolConfig和ServiceConfig某属性没有配置时,采用此缺省值 -->
<dubbo:provider timeout="30000" threadpool="fixed" threads="100" accepts="1000" />
<!-- 服务接口 -->
<dubbo:service retries="0" interface="com.guduo.service.system.UserReportService" ref="userReportService" />
<dubbo:service retries="0" interface="com.guduo.service.system.UserSuggestionService" ref="userSuggestionService" />
</beans>
spring-context.xml引入spring-dubbo-provider.xml
5、consumer注册服务:dubbo-consumer.xml(消费者)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="guduo-web-publish" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<!-- 注册中心地址 -->
<dubbo:registry protocol="zookeeper" address="${dubbo.registry.address}" />
<!-- 用户服务接口 -->
<dubbo:reference interface="com.guduo.service.user.PmsActionService" id="pmsActionService" check="false"/>
<dubbo:reference interface="com.guduo.service.user.PmsMenuService" id="pmsMenuService" check="false"/>
</beans>
4、开发阶段,启动Dubbo服务
建议放在"src/test"目录下面
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
*
* @描述: 启动Dubbo服务用的MainClass.
* @作者: 刘广平
* @创建时间: 2016-9-5,下午9:47:55 .
* @版本: 1.0 .
*/
public class DubboProvider {
private static final Log log = LogFactory.getLog(DubboProvider.class);
public static void main(String[] args) {
try {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-context.xml");
context.start();
} catch (Exception e) {
log.error("== DubboProvider context start error:",e);
}
synchronized (DubboProvider.class) {
while (true) {
try {
DubboProvider.class.wait();
} catch (InterruptedException e) {
log.error("== synchronized error:",e);
}
}
}
}
}
以上是关于Dubbo的应用的主要内容,如果未能解决你的问题,请参考以下文章
Dubbo实战 Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务(转)
Dubbo实战 Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务
Dubbo实战 Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务