springMvc dubbo zookeeper 集成demo

dubbo官方文档   http://dubbo.io/User+Guide-zh.htm

dubbox官方文档地址 http://dangdangdotcom.github.io/dubbox/


springMvc dubbo zookeeper 集成demo_第1张图片springMvc dubbo zookeeper 集成demo_第2张图片线上业务一般都是DAO ,SERVICE与controller区分开,且部署于不同的机器上的,这里不做探讨, 环境搭建这些的网上教程很多,我也就不多说了,先安装zookeeper,再tomcat,再dubbo-admin,进入如下的管理界面

springMvc dubbo zookeeper 集成demo_第3张图片

生产者demo结构图

springMvc dubbo zookeeper 集成demo_第4张图片springMvc dubbo zookeeper 集成demo_第5张图片springMvc dubbo zookeeper 集成demo_第6张图片


package com.hao.vo;

import java.io.Serializable;


public class Hao implements Serializable{
	
	private int id;
	private String name;
	private int age;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	

}

package com.hao;

import com.hao.vo.Hao;

public interface DemoService {
    public Hao sayHello();
}

package com.hao;

import java.util.concurrent.atomic.AtomicInteger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hao.vo.Hao;


public class DemoServiceImpl implements DemoService{
	
	private static final Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class);

	public static AtomicInteger c = new AtomicInteger(1);
	public static int i =1;
	
	@Override
	public Hao sayHello() {
		
		logger.info("hello zy!");
		Hao hao = new Hao();
		hao.setId(1001);
		hao.setName("haoj");
		hao.setAge(30);
		/*try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}*/
		logger.info("----"+DemoServiceImpl.c.getAndIncrement()+"----"+ i++ +"----provider hao "+hao);
		return hao;
	}
}

package com.hao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
/**
 * spring瀹瑰櫒
 * @author haojian
 * Mar 29, 2013 3:53:06 PM
 */
public class GameContext {
	
	private static final Logger logger = LoggerFactory.getLogger(GameContext.class);

	/**
	 * spring 閰嶇疆鏂囦欢璺緞
	 */
	private static String confPath = 
		System.getProperty("file.separator")
		+ System.getProperty("user.dir")
		+ System.getProperty("file.separator") + "conf"
		+ System.getProperty("file.separator");
	
	/**
	 * spring瀹瑰櫒
	 */
	private static ApplicationContext context = null;
	
	static{
		String[] files = {
				 confPath+"application-provider.xml" 
				//,confPath+"applicationContext-hibernate.xml"
				};
		
		context = new FileSystemXmlApplicationContext(files); 
		logger.info("鍒濆鍖杝pring閰嶇疆缁撴潫...");
	}


	/**
	 * 鑾峰彇Spring瀹瑰櫒绠$悊鐨勫璞�
	 * @author haojian
	 * Apr 8, 2013 4:46:11 PM
	 * @param beanName
	 * @return
	 */
	public static Object getBean(String beanName){
		Object obj = context.getBean(beanName);
		return obj;
	}

	
	
	
}

package com.hao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Provider {
	
	private static final Logger logger = LoggerFactory.getLogger(GameContext.class);
	
	public static void main(String[] args){
		new GameContext();
		logger.info("i am provider...");
		try {
			Thread.sleep(1000000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/*DemoService demoService = (DemoService)GameContext.getBean("demoService");
		demoService.sayHello();*/
		
	}

}

springMvc dubbo zookeeper 集成demo_第7张图片

消费者demo


package com.hao;

import java.util.Random;

import com.hao.vo.Hao;

public class Consumer {
	
	public static void main(String[] args){
		
		System.out.println("i am consumer...");
		DemoService demoService = (DemoService)GameContext.getBean("demoService");
		for(int i=0;i<100;i++){
			Hao hao = demoService.sayHello();
			System.out.println("provider hao "+hao);
		}
		
		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		
	}

}

package com.hao;

import com.hao.vo.Hao;

public interface DemoService {
    public Hao sayHello();
}

package com.hao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
/**
 * spring容器
 * @author haojian
 * Mar 29, 2013 3:53:06 PM
 */
public class GameContext {
	
	private static final Logger logger = LoggerFactory.getLogger(GameContext.class);

	/**
	 * spring 配置文件路径
	 */
	private static String confPath = 
		System.getProperty("file.separator")
		+ System.getProperty("user.dir")
		+ System.getProperty("file.separator") + "conf"
		+ System.getProperty("file.separator");
	
	/**
	 * spring容器
	 */
	private static ApplicationContext context = null;
	
	static{
		String[] files = {
				 confPath+"application-consumer.xml"
				//,confPath+"applicationContext-hibernate.xml"
				};
		
		context = new FileSystemXmlApplicationContext(files); 
		logger.info("初始化spring配置结束...");
	}

	/**
	 * 获取Spring容器管理的对象
	 * @author haojian
	 * Apr 8, 2013 4:46:11 PM
	 * @param beanName
	 * @return
	 */
	public static Object getBean(String beanName){
		Object obj = context.getBean(beanName);
		return obj;
	}
	
}

项目架构图

springMvc dubbo zookeeper 集成demo_第8张图片


springMvc dubbo zookeeper 集成demo_第9张图片springMvc dubbo zookeeper 集成demo_第10张图片


运行效果:


springMvc dubbo zookeeper 集成demo_第11张图片springMvc dubbo zookeeper 集成demo_第12张图片springMvc dubbo zookeeper 集成demo_第13张图片springMvc dubbo zookeeper 集成demo_第14张图片




你可能感兴趣的:(dubbo,zookeeper,springmvc)