当前位置: 首页 > news >正文

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

源码下载  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捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

 

 

相关文章:

  • Badboy - 从excel中读取数据
  • spring - ioc和aop
  • Java中内存分配
  • Android--获取系统时间的几种方式
  • PLSQL Developer过期要注冊表
  • JavaScript中call,apply,bind方法的总结。
  • Spring MVC入门
  • umount device is busy
  • Ajax、Comet与Websocket
  • 新博客 Fighting
  • Python输出中文乱码问题
  • 查看控件id的工具
  • 夜雨
  • 15位行业领袖策划顶尖技术聚会
  • 极简反传(BP)神经网络
  • @jsonView过滤属性
  • 《深入 React 技术栈》
  • 【node学习】协程
  • angular学习第一篇-----环境搭建
  • CSS 提示工具(Tooltip)
  • ESLint简单操作
  • export和import的用法总结
  • Go 语言编译器的 //go: 详解
  • mysql 数据库四种事务隔离级别
  • React 快速上手 - 06 容器组件、展示组件、操作组件
  • react-core-image-upload 一款轻量级图片上传裁剪插件
  • XForms - 更强大的Form
  • 从零搭建Koa2 Server
  • 分布式任务队列Celery
  • 工作手记之html2canvas使用概述
  • 精彩代码 vue.js
  • 精益 React 学习指南 (Lean React)- 1.5 React 与 DOM
  • 七牛云假注销小指南
  • 如何用Ubuntu和Xen来设置Kubernetes?
  • 设计模式 开闭原则
  • 使用Envoy 作Sidecar Proxy的微服务模式-4.Prometheus的指标收集
  • 小程序上传图片到七牛云(支持多张上传,预览,删除)
  • 一个完整Java Web项目背后的密码
  • 积累各种好的链接
  • 移动端高清、多屏适配方案
  • 支付宝花15年解决的这个问题,顶得上做出十个支付宝 ...
  • # 再次尝试 连接失败_无线WiFi无法连接到网络怎么办【解决方法】
  • #鸿蒙生态创新中心#揭幕仪式在深圳湾科技生态园举行
  • #设计模式#4.6 Flyweight(享元) 对象结构型模式
  • (10)ATF MMU转换表
  • (2)STL算法之元素计数
  • (C语言)输入一个序列,判断是否为奇偶交叉数
  • (PHP)设置修改 Apache 文件根目录 (Document Root)(转帖)
  • (Redis使用系列) Springboot 使用redis实现接口Api限流 十
  • (分布式缓存)Redis哨兵
  • (更新)A股上市公司华证ESG评级得分稳健性校验ESG得分年均值中位数(2009-2023年.12)
  • (紀錄)[ASP.NET MVC][jQuery]-2 純手工打造屬於自己的 jQuery GridView (含完整程式碼下載)...
  • (十) 初识 Docker file
  • (四)汇编语言——简单程序
  • (算法二)滑动窗口