Dubbo入门例子(不使用注册中心)

刚学的两种RPC框架,相比Thrift,dubbo更加简单直接上例子。点击下载

1.服务端开发

  目录结构

Dubbo入门例子(不使用注册中心)_第1张图片

1.1pom文件


		
			com.alibaba
			dubbo
			2.6.2
		
		
			org.apache.curator
			curator-recipes
			4.0.1
		
	

1.2 Ihello.java

package dubbo.service;
public interface IHello {
	public String hi(String name,int age);
}

1.3HelloService

package dubbo.serviceImpl;

import java.util.Date;

import dubbo.service.IHello;

public class HelloService implements IHello {

	public String hi(String name, int age) {
		System.err.println("Provider接收到的是:" + name + "," + age);
		return "your name is:" + name + ",age is:" + age + "," + new Date().toString();
	}
}

1.4 Server

package dubbo.server;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Server {
	public static void main(String[] args) throws Exception {
		// 加载配置文件
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:dubbo_server.xml");
		ctx.start();
		System.err.println("启动了.....");
		System.in.read();
	}
}

1.5 dubbo_server.xm




	
	
	
   
	
	
	
	
	
	

2 客户端开发

目录结构

Dubbo入门例子(不使用注册中心)_第2张图片

2.1 MyClient.java

package dubbo.client;

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

import dubbo.service.IHello;

public class MyClient {
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:dubbo_client.xml");
		IHello hello = ctx.getBean("IHello", IHello.class);
		String str = hello.hi("张三", 89);
		System.err.println(":" + str);
	}
}

2.2 dubbo_client.xml




	
	
	

	
	

先运行服务端,在运行客户端,如下图

 Dubbo入门例子(不使用注册中心)_第3张图片

                                                          服务端开启

Dubbo入门例子(不使用注册中心)_第4张图片

                                                             客户端开启

Dubbo入门例子(不使用注册中心)_第5张图片

                                                    客户端开启后服务端窗口

3.注意Ihello的目录结构要一致

你可能感兴趣的:(RPC,RPC,Dubbo)