2.1.Multicast注册中心
不需要启动任何中心节点,只要广播地址一样,就可以互相发现
组播受网络结构限制,只适合小规模应用或开发阶段使用。
组播地址段: 224.0.0.0 - 239.255.255.255
注意:multicast地址不能配成127.0.0.1,也不能配成机器的IP地址,必须是D段广播地址,也就是:224.0.0.0到239.255.255.255之间的任意地址
1. 提供方启动时广播自己的地址。
2. 消费方启动时广播订阅请求。
3. 提供方收到订阅请求时,单播自己的地址给订阅者,如果设置了unicast=false,则广播给订阅者。
4. 消费方收到提供方地址时,连接该地址进行RPC调用。
<dubbo:registry address="multicast://224.5.6.7:1234" />
Or:
<dubbo:registry protocol="multicast" address="224.5.6.7:1234" />
为了减少广播量,Dubbo缺省使用单播发送提供者地址信息给消费者,
如果一个机器上同时启了多个消费者进程,消费者需声明unicast=false,否则只会有一个消费者能收到消息:
<dubbo:registry address="multicast://224.5.6.7:1234?unicast=false" />
Or:
<dubbo:registry protocol="multicast" address="224.5.6.7:1234">
<dubbo:parameter key="unicast" value="false" />
</dubbo:registry>
服务提供者
定义服务接口: (该接口需单独打包,在服务提供方和消费方共享)
DemoService.java
package com.alibaba.dubbo.demo.provider;
import java.util.List;
/**
*
* 项目名称:dubboprovider
* 类名称:DemoService
* 类描述:提供者接口
* 创建人:YinXiangBing
* 创建时间:2015年8月20日 上午9:09:49
* @version 1.0
*
*/
public interface DemoService {
String sayHello(String name);
}
在服务提供方实现接口:(对服务消费方隐藏实现)
DemoServiceImpl.java
package com.alibaba.dubbo.demo.provider.impl;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.dubbo.demo.provider.DemoService;
/**
*
* 项目名称:dubboprovider
* 类名称:DemoServiceImpl
* 类描述: 提供者实现
* 创建人:YinXiangBing
* 创建时间:2015年8月20日 上午9:10:30
* @version 1.0
*
*/
public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
return "Hello " + name;
}
}
用Spring配置声明暴露服务:
<?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="demoService" class="com.alibaba.dubbo.demo.provider.impl.DemoServiceImpl" />
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="hello-world-app"/>
<!-- 使用multicast广播注册中心暴露服务地址-->
<dubbo:registry address="multicast://224.5.6.7:1234"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.alibaba.dubbo.demo.provider.DemoService"
ref="demoService" />
</beans>
加载Spring配置:
package com.alibaba.dubbo.demo.provider;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
*
* 项目名称:dubboprovider
* 类名称:Provider
* 类描述: 加载Spring配置
* 创建人:YinXiangBing
* 创建时间:2015年8月20日 上午9:12:07
* @version 1.0
*
*/
public class Provider {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
context.start();
System.in.read(); //为保证服务一直开着,利用输入流的阻塞来模拟,按任意键退出
}
}
服务消费者
通过Spring配置引用远程服务:
<?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/s