IDEA下搭建maven管理的DUBBO项目,Zookeeper作为注册中心
2016-12-29 20:07
531 查看
环境:JDK 1.8、dubbo版本:2.9.2-SNAPSHOT、zookeeper版本:3.5.1-alpha、IDEA2016
1.下载zookeeper,点击下载,解压即可,重命名conf文件夹中zoo_sample.cfg文件为zoo.cfg并修改为下:
各个配置参数解释见链接 https://my.oschina.net/xiaotian120/blog/194820
启动zookeeper:依次打开bin目录下的zkServer.cmd、zkCli.cmd
如果出现启动闪退的情况,需要用记事本打开以上文件
zkServer.cmd修改如下(加入pause暂停查看问题):
2.新建maven工程,父工程不勾选框架为普通工程,子工程customer为web端则需勾选,建议加maven参数,archetypeCatalog=internal。这个参数的意义是让这个maven项目的骨架不要到远程下载而是本地获取
IDEA子工程为父工程下的模块,下图为创建子工程,父工程不需要勾选(create from archetype)
我的工程目录如下:
3.增加Spring框架
web.xml配置
4.api为接口工程,无需配置。provider为提供服务实现接口,在实现接口后需要配置如下:
这两个文件直接导入Spring配置
dubbo-provider.xml放入resource文件目录下
customer还需配置Mybatis文件
spring-mybatis.xml
pom.xml
最后两个工程分别由两个tomcat启动,customer就可以隔空调用provider提供的服务了
provider把服务暴露在zookeeper,customer从zookeeper中获取服务,两者无依赖关系
项目https://github.com/HuangLiwis/myDubbo
1.下载zookeeper,点击下载,解压即可,重命名conf文件夹中zoo_sample.cfg文件为zoo.cfg并修改为下:
# zookeeper中使用的基本时间单位, 毫秒值 tickTime=2000 # follower和leader之间的最长心跳时间 initLimit=10 # 配置leader和follower之间发送消息, 请求和应答的最大时间长度 syncLimit=5 # zk数据存储位置 dataDir=E:\\Work_SoftWare\\zookeeper-3.4.9\\data # zk日志存储位置 dataLogDir=E:\\Work_SoftWare\\zookeeper-3.4.9\\log # 监听client连接的端口号 clientPort=2181 # 清理频率,单位是小时 autopurge.purgeInterval=1
各个配置参数解释见链接 https://my.oschina.net/xiaotian120/blog/194820
启动zookeeper:依次打开bin目录下的zkServer.cmd、zkCli.cmd
如果出现启动闪退的情况,需要用记事本打开以上文件
zkServer.cmd修改如下(加入pause暂停查看问题):
setlocal call "%~dp0zkEnv.cmd" set ZOOMAIN=org.apache.zookeeper.server.quorum.QuorumPeerMain set ZOO_LOG_FILE=zookeeper-%USERNAME%-server-%COMPUTERNAME%.log echo on call %JAVA% "-Dzookeeper.log.dir=%ZOO_LOG_DIR%" "-Dzookeeper.root.logger=%ZOO_LOG4J_PROP%" "-Dzookeeper.log.file=%ZOO_LOG_FILE%" "-XX:+HeapDumpOnOutOfMemoryError" "-XX:OnOutOfMemoryError=cmd /c taskkill /pid %%%%p /t /f" -cp "%CLASSPATH%" %ZOOMAIN% "%ZOOCFG%" %* endlocal pause
2.新建maven工程,父工程不勾选框架为普通工程,子工程customer为web端则需勾选,建议加maven参数,archetypeCatalog=internal。这个参数的意义是让这个maven项目的骨架不要到远程下载而是本地获取
IDEA子工程为父工程下的模块,下图为创建子工程,父工程不需要勾选(create from archetype)
我的工程目录如下:
3.增加Spring框架
web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5" metadata-complete="false"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springmvc-servlet.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 拦截所有请求--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <!--会自动寻找springmvc-servlet.xml文件--> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>springmvc-servlet.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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <import resource="classpath:dubbo-customer.xml"/> <context:annotation-config/> <context:component-scan base-package="com.xiaozhi.controller"/> <!-- 对静态资源文件的访问,交给default servlet handler处理 --> <mvc:default-servlet-handler/> <!-- 默认的注解映射的支持 --> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
4.api为接口工程,无需配置。provider为提供服务实现接口,在实现接口后需要配置如下:
这两个文件直接导入Spring配置
<!--加入其他配置文件--> <import resource="classpath:dubbo-provider.xml"/> <import resource="classpath:spring-mybatis.xml"/>
dubbo-provider.xml放入resource文件目录下
<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管理页面比较清晰是哪个应用暴露出来的 --> <dubbo:application name="dubbo-provider"/> <dubbo:protocol id="dubbo" name="dubbo" port="21521" threads="300"/> <dubbo:provider protocol="dubbo" /> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://10.22.0.23:2181" check="false" /> <!-- 要暴露的服务接口 --> <dubbo:service version="1.0" interface="com.xiaozhi.service.XiaozhiService" ref="xiaozhiServiceImp" /> </beans>同理,customer也需配置如下:
<?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="dubbo-customer" /> <!-- 使用multicast广播注册中心暴露发现服务地址 --> <dubbo:registry address="zookeeper://10.22.0.23:2181" file=".dubbo/dubbo-registry.cache"/> <dubbo:consumer check="false" timeout="6000"/> <dubbo:reference version="1.0" id="xioazhiService" interface="com.xiaozhi.service.XiaozhiService" check="false"/> </beans>
customer还需配置Mybatis文件
spring-mybatis.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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <!--引入配置文件--> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:application.properties"/> </bean> <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${mybatis.driverClass}"/> <property name="url" value="${mybatis.jdbcUrl}"/> <property name="username" value="${mybatis.username}"/> <pro c474 perty name="password" value="${mybatis.password}"/> <!-- 初始化连接大小 --> <property name="initialSize" value="${mybatis.initialSize}"></property> <!-- 连接池最大数量 --> <property name="maxActive" value="${mybatis.maxActive}"></property> <!-- 连接池最大空闲 --> <property name="maxIdle" value="${mybatis.maxIdle}"></property> <!-- 连接池最小空闲 --> <property name="minIdle" value="${mybatis.minIdle}"></property> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="${mybatis.maxWait}"></property> </bean> <!-- mybatis和spring完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 自动扫描mapping.xml文件 --> <property name="mapperLocations" value="classpath:mapping/*.xml"></property> </bean> <!-- DAO接口所在包名,Spring会自动查找其下的类 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="annotationClass" value="org.springframework.stereotype.Repository"/> <property name="basePackage" value="com.xiaozhi.dao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <parent> <artifactId>DubboDemo</artifactId> <groupId>com.dubboDemo</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>customer</artifactId> <packaging>war</packaging> <name>customer Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>com.dubboDemo</groupId> <artifactId>api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- spring start --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <scope>test</scope> </dependency> <!--spring end --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> </dependency> <!--dubbo--> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> </dependency> <!--netty--> <dependency> <groupId>io.netty</groupId> <artifactId>netty</artifactId> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.5.1-alpha</version> </dependency> </dependencies> <build> <finalName>customer</finalName> </build> </project>
最后两个工程分别由两个tomcat启动,customer就可以隔空调用provider提供的服务了
provider把服务暴露在zookeeper,customer从zookeeper中获取服务,两者无依赖关系
项目https://github.com/HuangLiwis/myDubbo
相关文章推荐
- Dubbo-Admin管理平台和Zookeeper注册中心的搭建
- Dubbo-Admin管理平台和Zookeeper注册中心的搭建(转)
- Dubbo-Admin管理平台和Zookeeper注册中心的搭建
- Dubbo-Admin管理平台和Zookeeper注册中心的搭建
- Dubbo-Admin管理平台和Zookeeper注册中心的搭建
- Dubbo学习总结(3)——Dubbo-Admin管理平台和Zookeeper注册中心的搭建
- Window环境上Dubbo-Admin管理平台和Zookeeper注册中心的搭建
- Dubbo-Admin管理平台和Zookeeper注册中心的搭建
- Dubbo学习总结(3)——Dubbo-Admin管理平台和Zookeeper注册中心的搭建
- Dubbo-Admin管理平台和Zookeeper注册中心的搭建
- windows Dubbo-Admin管理平台和Zookeeper注册中心的搭建
- dubbo学习一:Zookeeper注册中心和Dubbo-Admin管理平台的搭建
- Dubbo-Admin管理平台和Zookeeper注册中心的搭建
- Dubbo-Admin管理平台和Zookeeper注册中心的搭建
- Zookeeper注册中心和Dubbo-Admin管理平台的搭建
- Dubbo学习总结(3)——Dubbo-Admin管理平台和Zookeeper注册中心的搭建
- Dubbo-Admin管理平台和Zookeeper注册中心的搭建
- Zookeeper注册中心和Dubbo-Admin管理平台的搭建
- Zookeeper注册中心和Dubbo-Admin管理平台的搭建
- Dubbo-Admin管理平台和Zookeeper注册中心的搭建