前言:

运行需要自己安装zookeeper,自己创建maven工程,不会的自行百度。

1、先展示目录结构



2、新建maven工程

3、修改pom,引入对应jar

<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.sino.mars.dubbo</groupId>
	<artifactId>demo.paractice</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>demo.paractice</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

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

		<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.5.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.3.6</version>
			<exclusions>
				<exclusion>
					<groupId>log4j</groupId>
					<artifactId>log4j</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.16</version>
		</dependency>
		
		<!-- zookeeper_service 出错 java.lang.NoClassDefFoundError: org/I0Itec/zkclient/exception/ZkNoNodeException -->
		<dependency>
			<groupId>com.101tec</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.10</version>
		</dependency>



	</dependencies>
</project>

4、创建model

package com.sino.mars.dubbo.demo.paractice.model;

import java.io.Serializable;
import java.util.List;

public class MsgInfo implements Serializable {

    private static final long serialVersionUID = -357956654466558490L;

    int id;
    String name;
    List<String> msgs;

    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 List<String> getMsgs() {
        return msgs;
    }

    public void setMsgs(List<String> msgs) {
        this.msgs = msgs;
    }



}

5、创建helloservice

package com.sino.mars.dubbo.demo.paractice.service;

import com.sino.mars.dubbo.demo.paractice.model.MsgInfo;

/**
 * 
 * @版权:SINOSERVICES 版权所有 (c) 2013
 * @author:Mars
 * @version Revision 1.0.0
 * @email:Mars.Wei@sinoservices.com
 * @see:
 * @创建日期:2017年5月23日
 * @功能说明:
 * @begin
 * @修改记录:
 * @修改后版本 修改人 修改内容
 * @2017年5月23日 Mars 创建
 * @end
 */
public interface HelloService {

    public void sayHello();

    public String returnHello();

    public MsgInfo returnMsgInfo(MsgInfo info);

}
6、实现helloservice

package com.sino.mars.dubbo.demo.paractice.service.impl;

import com.sino.mars.dubbo.demo.paractice.model.MsgInfo;
import com.sino.mars.dubbo.demo.paractice.service.HelloService;

/**
 * 
 * @版权:SINOSERVICES 版权所有 (c) 2013
 * @author:Mars
 * @version Revision 1.0.0
 * @email:Mars.Wei@sinoservices.com
 * @see:
 * @创建日期:2017年5月23日
 * @功能说明:
 * @begin
 * @修改记录:
 * @修改后版本          修改人      	修改内容
 * @2017年5月23日  	         Mars        	创建
 * @end
 */
public class HelloServiceImpl implements HelloService {

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

    public String returnHello() {
        return "hello world!";
    }

    public MsgInfo returnMsgInfo(MsgInfo info) {
        info.getMsgs().add("处理完毕");
        return info;
    }

}
7、创建生产者provider.xml配置文件

<?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">
	<dubbo:application name="hello-world-app" />
	<!--zookeeper注册中心 -->
	<dubbo:registry protocol="zookeeper" address="127.0.0.1:2182" />
	<dubbo:protocol name="dubbo" port="20880" />
	<dubbo:service
		interface="com.sino.mars.dubbo.demo.paractice.service.HelloService" ref="helloService" />
	<!-- 和本地bean一样实现服务 -->
	<bean id="helloService" class="com.sino.mars.dubbo.demo.paractice.service.impl.HelloServiceImpl" />
</beans>

8、创建消费者consumer.xml配置文件

<?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
       ">
	<!--消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
	<dubbo:application name="consumer-of-helloworld-app" />
	<!--zookeeper注册中心 -->
	<dubbo:registry protocol="zookeeper" address="127.0.0.1:2182" />
	<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
	<dubbo:reference id="helloService"
		interface="com.sino.mars.dubbo.demo.paractice.service.HelloService" />
</beans>

9、创建生产者主程序

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ProviderMain {

    public static void main(String[] args) throws InterruptedException {
        ProviderMain providerMain = new ProviderMain();
        providerMain.start();
        Thread.sleep(1000 * 60 * 10);
    }

    public void start() {
        String configLocation = "provider.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();
    }

}

10、创建消费者主程序

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sino.mars.dubbo.demo.paractice.model.MsgInfo;
import com.sino.mars.dubbo.demo.paractice.service.HelloService;

public class ConsumerMain {
    public static void main(String[] args) throws InterruptedException {
        ConsumerMain luncher = new ConsumerMain();
        luncher.start();
    }


    void start() {
        String configLocation = "consumer.xml";
        ApplicationContext context = new ClassPathXmlApplicationContext(configLocation);
        HelloService ds = (HelloService) context.getBean("helloService");
        String[] names = context.getBeanDefinitionNames();
        System.out.print("Beans:");
        for (String string : names) {
            System.out.print(string);
            System.out.print(",");
        }
        System.out.println();

        MsgInfo info = new MsgInfo();
        info.setId(1);
        info.setName("mars");
        List<String> msgs = new ArrayList<String>();
        msgs.add("I");
        msgs.add("am");
        msgs.add("test");
        info.setMsgs(msgs);


        System.out.println(ds.returnMsgInfo(info).getMsgs());
        
        ds.sayHello();
        
        System.err.println(ds.returnHello());
    }
}

11、先启动生产者主程序


12、再启动消费者主程序



至此:生产者--消费者已经运行完毕。


附:Dubbo后台管理页面查看我们刚创建的服务。



Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐