springMvc dubbo zookeeper 集成demo

2 篇文章 0 订阅
2 篇文章 0 订阅

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

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


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


生产者demo结构图



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();*/
		
	}

}


消费者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;
	}
	
}

项目架构图





运行效果:






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值