Maven+Spring+Dubbo学习笔记

学习参考文档  http://dubbo.io/User+Guide-zh.htm 

本案例使用maven创建,关于如何创建可以参考我另一篇博客  http://blog.youkuaiyun.com/a_piaoyouareminemine/article/details/49925289

准备工具:eclipse-jee-luna-SR2-win32-x86_64,apache-maven-3.3.3-bin,zookeeper-3.4.5,dubbo-monitor-simple-2.5.3

下载链接:http://pan.baidu.com/s/1bn5GLFh

1.创建maven项目,大概目录如下:

2.创建一个接口DemoService,和一个实现类DemoServiceImpl


<pre name="code" class="java">import com.dubbo.client.model.User;


public interface DemoService {
	
    public void sayHello();
    
    public User add(String username);
    
}
public class DemoServiceImpl implements DemoService {


	public void sayHello() {
		System.out.println("hello world!");
	}


	public User add(String username) {
		User user = new User();
		user.setId(1);
		user.setName(username);
		user.setPassword("123");
		return user;
	}
}


 
 3.添加LuncherProvider服务提供类和 LuncherConsumer服务消费类 

<pre name="code" class="java">public class LuncherProvider  {
	public static void main(String[] args) throws InterruptedException{
		LuncherProvider luncher=new LuncherProvider();
		luncher.start();
		Thread.sleep(1000*60*10);
	}
	
	void start(){
		String configLocation="classpath:conf/dubbo-server.xml";
		ApplicationContext context =new  ClassPathXmlApplicationContext(configLocation);
		String [] names=context.getBeanDefinitionNames();
		System.out.print("Beans:");
		for (String string : names)
			System.out.print(string+",");
		System.out.println();
	}
}
public class LuncherConsumer  {
	public static void main(String[] args) throws InterruptedException{
		LuncherConsumer luncher=new LuncherConsumer();
		luncher.start();
	}
	
	
	void start(){
		String configLocation="classpath:conf/spring-dubbo.xml";
		ApplicationContext context =new  ClassPathXmlApplicationContext(configLocation);
		DemoService ds=(DemoService) context.getBean("userService");
		String [] names=context.getBeanDefinitionNames();
		System.out.print("Beans:");
		for (String string : names) {
			System.out.print(string);
			System.out.print(",");
		}
		System.out.println();
		
		User user = new User();
		user.setName("test1");


		System.out.println(ds.add("werwer"));
	}
}


 
 

4.添加一个实体类,控制台demo可以不用model类

<pre name="code" class="java">/**
 * 用户表
 * @author wanghaiyang
 * 2015年11月19日
 */
public class User {


	private int id;
	private String name;
	private String password;
	
	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 String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
}


 
 

5.配置pom.xml 项目所以来的jar包

<pre name="code" class="html"><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.dubbo.demo</groupId>
	<artifactId>TestDubbo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<name>TestDubbo</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<spring.version>3.2.4.RELEASE</spring.version>
		<slf4j.version>1.6.6</slf4j.version>
		<log4j.version>1.2.9</log4j.version>
	</properties>

	<dependencies>
		 <dependency> 
		 <groupId>junit</groupId> 
		 <artifactId>junit</artifactId> 
		<version>3.8.1</version> 
		<scope>test</scope> 
		</dependency>

		<!-- spring核心包 -->
		<!-- springframe start -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-oxm</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<!-- springframe end -->

	<dependency>
		    <groupId>org.apache.zookeeper</groupId>
		    <artifactId>zookeeper</artifactId>
		    <version>3.4.6</version>
		</dependency>
		 <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
        </dependency>
        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.18.1-GA</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.netty</groupId>
            <artifactId>netty</artifactId>
            <version>3.2.9.Final</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.4</version>
        </dependency>
        
        <!-- 日志文件管理包 -->
		<!-- log start -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>${log4j.version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
        
	</dependencies>


</project>


 
 6.配置dubbo-provider.xml 

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

	<!-- <dubbo:registry address="zookeeper://127.0.0.1:2181" /> -->
	<dubbo:registry protocol="zookeeper" address="192.168.0.1:2181" />
	<!--  <dubbo:monitor address="127.0.0.1:9090" /> --> 

	<dubbo:protocol name="dubbo" port="20880" />
	 <bean id="userService" class="com.dubbo.example.DemoServiceImpl"></bean> 
	<dubbo:service interface="com.dubbo.example.DemoService" ref="userService" />
</beans>   


 
 7.配置dubbo-consumer.xml 

<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>   
<beans xmlns="http://www.springframework.org/schema/beans"      
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
 xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"     
 xsi:schemaLocation="http://www.springframework.org/schema/beans          
 http://www.springframework.org/schema/beans/spring-beans.xsd          
 http://code.alibabatech.com/schema/dubbo           
 http://code.alibabatech.com/schema/dubbo/dubbo.xsd">         
<!-- Application name -->      
<dubbo:application name="TestDubbo"  />         
<!-- registry address, used for service to register itself -->      
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />        
<!-- expose this service through dubbo protocol, through port 20880 -->  

<dubbo:monitor address = "127.0.0.1:9090" />
 
<dubbo:protocol name="dubbo" port="20880" />    
     
<dubbo:reference id="userService" interface="com.dubbo.client.service.UserService" url="127.0.0.1:2181"/> 
</beans>   


 
 8.上面的文件基本上就是这个项目的大概了,启动的时候先把zookeeper和 
dubbo-monitor启起来,双击bin目录下的zkServer.cmd即可,然后启动dubbo-monitor-simple-2.5.3在bin目录下的start.bat 双击,monitor配置文件里的jetty端口号需要改下,如果创建的是web项目发布到tomcat下会有端口冲突,所以修改下dubbo.jetty.port=9090 

9.再就是启动先服务LuncherProvider 然后启动LuncherConsumer ,都是main方法。

10.可以在浏览器输入localhost:9090查看服务调用情况



11.附上项目下载链接 http://download.youkuaiyun.com/detail/a_piaoyouareminemine/9296383













评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值