天天看點

(阿裡巴巴 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 起到 注冊中心,服務發現的作用

(阿裡巴巴 dubbo,有資料庫,可執行 )dubbo zookeeper spring demo

consumer 測試結果:

(阿裡巴巴 dubbo,有資料庫,可執行 )dubbo zookeeper spring demo

provider 釋出,(這是一種釋出方式)

(阿裡巴巴 dubbo,有資料庫,可執行 )dubbo zookeeper spring demo

 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:[email protected]
 * @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:[email protected]
 * @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:[email protected]
 * @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:[email protected]
 * @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捐助),沒錢捧個人場,謝謝各位。

(阿裡巴巴 dubbo,有資料庫,可執行 )dubbo zookeeper spring demo
(阿裡巴巴 dubbo,有資料庫,可執行 )dubbo zookeeper spring demo
(阿裡巴巴 dubbo,有資料庫,可執行 )dubbo zookeeper spring demo

 謝謝您的贊助,我會做的更好!