您的位置:首页 > 编程语言 > Java开发

(阿里巴巴 dubbo,有数据库,可执行 )dubbo zookeeper spring demo

2018-02-07 00:00 711 查看
源码下载 http://download.csdn.net/download/knight_black_bob/9439432

windows zookeeper 下载地址 http://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.6/ 修改zookeeper配置文件zoo-example.cfg改为zoo.cfg,zookeeper默认寻找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
dataDir=F:\\log\\zookeeper\\data
dataLogDir=F:\\log\\zookeeper\\log
# 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

启动zookeeper
Window下命令:进入bin目录 ->zkServer.cmd


配置 <dubbo:registry id="zk1" address="quickstart.cloudera:2181" protocol="zookeeper" />

测试成功

,provider 发布 后 ,linux 下 zookeeper path 会增加。 zookeeper 起到 注册中心,服务发现的作用



consumer 测试结果:



provider 发布,(这是一种发布方式)



applictionContext-dubbo-provider.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> 
<!-- 提供方应用名称信息,这个相当于起一个名字,我们dubbo管理页面比较清晰是哪个应用暴露出来的 -->
<dubbo:application name="curiousby-dubbo-provider"/>
<!-- 提供方应用名称信息,这个相当于起一个名字,我们dubbo管理页面比较清晰是哪个应用暴露出来的 -->
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry  id="zk1" address="192.168.16.21:2181,192.168.16.29:2181,192.168.16.30:2181" protocol="zookeeper" />
<!-- 使用multicast广播注册中心暴露服务地址 -->
<!--<dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<!-- 用dubbo协议在20886端口暴露服务 -->
<dubbo:protocol id="mydubbo"  name="dubbo" port="20886" />
<dubbo:provider registry="zk1"  protocol="mydubbo"/>

<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.baoy.service.UserService"   ref="userServiceImpl" />

</beans>


applictionContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:oscache="http://www.springmodules.org/schema/oscache"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springmodules.org/schema/oscache http://www.springmodules.org/schema/cache/springmodules-oscache.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> 
<!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
<mvc:annotation-driven />
<!-- 扫描包 -->
<context:annotation-config />
<!-- import the properties -->
<context:property-placeholder location="classpath:jdbc.properties" />
<context:component-scan base-package="com.baoy" />

<!-- 配置數據源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="${jdbc.initialSize}" />
<property name="maxActive" value="${jdbc.maxActive}" />
<property name="maxIdle" value="${jdbc.maxIdle}" />
<property name="minIdle" value="${jdbc.minIdle}" />
</bean>

<!-- 配置jdbcTemplate模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置 transactionManager事物管理 -->
<!-- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" /> </bean> -->

<!-- Spring AOP config配置切点 -->
<!-- <aop:config> <aop:pointcut expression="execution(* cmcc.picrepository.service.*.*(..))"
id="bussinessService" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="bussinessService"
/> </aop:config> -->

<!-- 配置那个类那个方法用到事务处理 -->
<!-- <tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes> <tx:method name="search*" read-only="true" /> <tx:method
name="find*" read-only="true" /> <tx:method name="get*" read-only="true"
/> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="save*"
propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED"
/> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="*"
propagation="REQUIRED" /> </tx:attributes> </tx:advice> -->
</beans>


package com.baoy.service;

import java.util.List;

import com.baoy.bean.User;

/**
* @author baoyou E-mail:curiousby@163.com
* @version 2016年2月22日 上午10:24:23
*
* desc: ...
*/
public interface UserService {
public List<User> getAllUsers();
public void delUserById(int userId);
public User getUserById(int userId);
public void updateUser(User user);
public void addUser(User user);
}


package com.baoy.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baoy.bean.User;
import com.baoy.dao.UserDao;
import com.baoy.service.UserService;

/**
* @author baoyou E-mail:curiousby@163.com
* @version 2016年2月22日 上午10:24:54
*
* desc: ...
*/
@Component
public class UserServiceImpl implements UserService {

@Autowired
UserDao userDao;

public List<User> getAllUsers() {
return userDao.getAllUsers();
}

public void delUserById(int userId) {
userDao.delUserById(userId);
}

public User getUserById(int userId) {
return 	userDao.getUserById(userId);
}

public void updateUser(User user) {
userDao.updateUser(user);
}

public void addUser(User user) {
userDao.addUser(user);
}

}


provider 发布,上面有一种发布方式

package com.baoy.main;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* @author baoyou E-mail:curiousby@163.com
* @version 2016年2月22日 下午4:44:44
*
*          desc: ...
*/
public class Start {

public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "classpath*:META-INF/spring/applictionContext.xml",
"classpath*:META-INF/spring/applictionContext-dubbo-provider.xml" });
context.start();
System.in.read(); // 按任意键退出
}

}


appliction-dubbo-consumer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> 
<dubbo:application name="curiousby-dubbo-consumer"/>

<dubbo:registry id="zk1" address="192.168.16.21:2181,192.168.16.29:2181,192.168.16.30:2181" protocol="zookeeper" />

<dubbo:consumer registry="zk1"/>

<dubbo:reference id="userService"  interface="com.baoy.service.UserService"/>

</beans>


appliction.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:oscache="http://www.springmodules.org/schema/oscache"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springmodules.org/schema/oscache http://www.springmodules.org/schema/cache/springmodules-oscache.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> 
<context:annotation-config />
<context:component-scan base-package="com.baoy.test" />

</beans>


package com.baoy.test;

import java.util.List;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.baoy.bean.User;
import com.baoy.service.UserService;

/**
* @author baoyou E-mail:curiousby@163.com
* @version 2016年2月22日 下午1:48:37
*
* desc: ...
*/

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:appliction.xml","classpath:appliction-dubbo-consumer.xml"})
public class ConsumerTest {

@Resource
UserService userService;

@Test
public  void allUsersTest() {
List<User> allUsers = userService.getAllUsers();
System.out.println("\r\n\r\n\r\n"+allUsers.toString()+"\r\n\r\n\r\n");
}

}


捐助开发者

在兴趣的驱动下,写一个
免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。







谢谢您的赞助,我会做的更好!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息