Dubbo-HelloWorld

dubbo

zookeeper 注册中心

dubbo.registry.address=zookeeper://localhost:2181
import java.util.List;

public interface SampleService {

	String sayHello(String name);

	public List getUsers();

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

import bhz.dubbo.sample.provider.SampleService;

public class SampleServiceImpl implements SampleService {
	
	public String sayHello(String name) {
		return "Hello " + name;
	}

	public List getUsers() {
		List list = new ArrayList();
		User u1 = new User();
		u1.setName("jack");
		u1.setAge(20);
		u1.setSex("m");

		User u2 = new User();
		u2.setName("tom");
		u2.setAge(21);
		u2.setSex("m");

		User u3 = new User();
		u3.setName("rose");
		u3.setAge(19);
		u3.setSex("w");

		list.add(u1);
		list.add(u2);
		list.add(u3);
		return list;
	}
}
import java.io.Serializable;

public class User implements Serializable {
	private static final long serialVersionUID = 1L;
	private int age;
	private String name;
	private String sex;

	public User() { 
		super();
	}

	public User(int age, String name, String sex) {
		super();
		this.age = age;
		this.name = name;
		this.sex = sex;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

}
<?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
        ">

	<!-- 具体的实现bean -->
	<bean id="sampleService" class="com.dubbo.sample.provider.impl.SampleServiceImpl" />

	<!-- 提供方应用信息,用于计算依赖关系 -->
	<dubbo:application name="sample-provider" />

	<!-- 使用zookeeper注册中心暴露服务地址 -->
	<dubbo:registry address="zookeeper://localhost:2181" />

	<!-- 用dubbo协议在20880端口暴露服务 -->
	<dubbo:protocol name="dubbo" port="20880" />

	<!-- 声明需要暴露的服务接口  写操作可以设置retries=0 避免重复调用SOA服务 -->
	<dubbo:service retries="0" interface="com.dubbo.sample.provider.SampleService" ref="sampleService" />

</beans>
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {

	public static void main(String[] args) throws Exception {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "sample-provider.xml" });
		context.start(); 
		System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
	}
}

 

import java.util.List;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import bhz.dubbo.sample.provider.SampleService;

public class Consumer {

	public static void main(String[] args) throws Exception {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "sample-consumer.xml" });
		context.start();
			
		SampleService sampleService = (SampleService) context.getBean("sampleService");
		String hello = sampleService.sayHello("tom");
		System.out.println(hello);
		
		List list = sampleService.getUsers();
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				System.out.println(list.get(i));
			}
		}
		System.in.read();
	}

}
import java.util.List;

public interface SampleService {

	String sayHello(String name);

	public List getUsers();

}
<?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="sample-consumer" />

	<dubbo:registry address="zookeeper://localhost:2181" />

	<!-- 生成远程服务代理,可以像使用本地bean一样使用demoService 检查级联依赖关系 默认为true 当有依赖服务的时候,需要根据需求进行设置 -->
	<dubbo:reference id="sampleService" check="false"
		interface="com.dubbo.sample.provider.SampleService" />

</beans>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于dubbo的代码实例可以通过以下步骤来实现[^2]: 1. 首先,确保你已经安装了Zookeeper,并启动了Zookeeper服务。 2. 创建一个Maven项目,并在pom.xml文件中添加Dubbo和Zookeeper的依赖。 3. 创建一个接口,定义需要暴露的服务方法。 ```java public interface HelloService { String sayHello(String name); } ``` 4. 创建一个实现类,实现接口中定义的服务方法。 ```java public class HelloServiceImpl implements HelloService { @Override public String sayHello(String name) { return "Hello, " + name + "!"; } } ``` 5. 在resources目录下创建一个dubbo配置文件dubbo-provider.xml,配置Dubbo的服务提供者。 ```xml <?xml version="1.0" encoding="UTF-8"?> <dubbo:application name="dubbo-demo-provider" /> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <dubbo:protocol name="dubbo" port="20880" /> <dubbo:service interface="com.example.HelloService" ref="helloService" /> <bean id="helloService" class="com.example.HelloServiceImpl" /> ``` 6. 创建一个启动类,用于启动Dubbo服务提供者。 ```java public class Provider { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-provider.xml"); context.start(); System.in.read(); } } ``` 7. 创建一个消费者项目,重复步骤2和3,创建一个接口和实现类。 8. 在resources目录下创建一个dubbo配置文件dubbo-consumer.xml,配置Dubbo的服务消费者。 ```xml <?xml version="1.0" encoding="UTF-8"?> <dubbo:application name="dubbo-demo-consumer" /> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <dubbo:reference id="helloService" interface="com.example.HelloService" /> ``` 9. 创建一个启动类,用于启动Dubbo服务消费者。 ```java public class Consumer { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-consumer.xml"); HelloService helloService = (HelloService) context.getBean("helloService"); String result = helloService.sayHello("World"); System.out.println(result); } } ``` 以上是一个基于Dubbo的简单代码实例,通过配置Dubbo的服务提供者和消费者,可以实现分布式的服务调用。你可以根据自己的需求进行扩展和定制。如果你想了解更多关于Dubbo的使用和原理,可以参考Dubbo的官方文档和源码[^1]。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值