import Ice.Current;
import com.mop.interf._MessageServiceIceDisp;
public class HelloWorldServiceIceImpl extends _HelloWorldServiceIceDisp{
private static final long serialVersionUID = -6954195427744180145L;
@Overridepublic voidsayHello(String name, Current __current) {System.out.println("hello " + name);}
}
}
import com.mop.ice.serv.HelloWorldServiceIceImpl ;import Ice.Communicator;import IceBox.Service;public class HelloWorldBoxService extends Ice.ObjectImpl implements Service {/****/private static final long serialVersionUID = 43338146304475187L;private Ice.ObjectAdapter _adapter;@Overridepublic void start(String name, Communicator ci, String[] args) {_adapter=ci.createObjectAdapter(name);_adapter.add(new HelloWorldServiceIceImpl(), Ice.Util.stringToIdentity("helloWorldService"));_adapter.activate();}@Overridepublic void stop() {if(_adapter!=null){_adapter.deactivate();}}}
<icegrid>
<application name="testIceBoxApp"> <!-- 必须设置应用的名称 --><replica-group id="testIceBoxReplicaGroup"> <!-- 对特定类型的icebox服务提供负载均衡策略 --><load-balancing type="adaptive" /> <!-- 有random、adaptive、round-robin、ordered 这4中负载均衡策略 --><object identity="IceBoxTest"type="::com::mop::interf::_MessageServiceIceDisp" /></replica-group>
<service-template id="iceboxServiceTemplate">
<parameter name="serviceClass"/>
<parameter name="serviceName"/>
<parameter name="adapterGroup"/>
<service name="${serviceName}" entry="${serviceClass}">
<adapter name="${serviceName}"
replica-group="${adapterGroup}"
endpoints="tcp"
/>
<property name="Ice.ThreadPool.Server.Size" value="10" />
<property name="Ice.ThreadPool.Client.Size" value="10" />
</service>
</service-template>
<node name="node_194">
<icebox id="testIceBox1" exe="/usr/java/default/bin/java" activation="on-demand">
<option>-server</option>
<option>-Xms64m</option>
<option>-Xmx256m</option>
<env>CLASSPATH=/data/iceservice/node/lib/helloworld</env>
<option>IceBox.Server</option><service-instance template="iceboxServiceTemplate"
serviceName="helloworldAdapter"
serviceClass="com.mop.ice.icebox.HelloWorldBoxService"
adapterGroup="testIceBoxReplicaGroup" />
</icebox>
</node></application>
</icegrid>
7) 启动服务
# ssh root@10.249.15.194
# cd /data/iceservice
# icegridregistry --Ice.Config=register_master.conf #启动主注册服务器# icegridregistry --Ice.Config=register_slave.conf #启动从注册服务器# icegridnode --Ice.Config=node1.conf #启动服务器节点1# icegridnode --Ice.Config=node2.conf #启动服务器节点2# icegridadmin -u test -p test --Ice.Default.Locator="IceGrid/Locator:tcp -p 4061:tcp -p 5061" -e "application add 'conf/helloworld.xml' "8、编写客户端进行测试public class IceClient {
public static void main(String[] args) {
int status = 0;
Communicator ic = null;
try {
String str = String.format("%s:%s -h %s -p %s", "IceGrid/Locator", "tcp", "127.0.0.1", "4061");
InitializationData localInitializationData = new InitializationData();localInitializationData.properties = Util.createProperties();
localInitializationData.properties.setProperty("Ice.Default.Locator", str);
ic = Util.initialize(localInitializationData);
HelloWorldPrx helloPrx = HelloWorldPrxHelper.checkedCast(ic.stringToProxy("helloWorldService@testIceBoxReplicaGroup")); //注意这个adapter要写部署描述文件中 replica-group 节点的 id 值
helloPrx.sayHello("Joe");
} catch (Ice.LocalException e) {
e.printStackTrace();
status = 1;
} catch (Exception e) {
System.err.println(e.getMessage());
status = 1;
}
if (ic != null) {
try {
ic.destroy();
} catch (Exception e) {
System.err.println(e.getMessage());
status = 1;
}
}System.exit(status);
}
}