Ice系列--IceBox(2)

上一篇单纯的分析了一个ice服务OnlineBook,这里分析多个ice服务并进行一些服务之间的分析。

增加一个发送短信的服务,SMSService

(1) 定义slice文件

module message{
interface SMSService{
    void sendMSg(string msg);
    };
};

(2) SMSServiceImpl 代码

package com.smsice;

import Ice.Communicator;
import Ice.Current;
import Ice.ObjectAdapter;
import IceBox.Service;
import message._SMSServiceDisp;

public class SMSserviceImpl extends _SMSServiceDisp implements Service{

    private ObjectAdapter _adapter;
    @Override
    public void sendMSg(String msg, Current __current) {
        System.out.println("send msg:"+ msg);
    }

    @Override
    public void start(String name, Communicator communicator, String[] args) {
        _adapter  = communicator.createObjectAdapter(name);

        // 创建servant 
        Ice.Object object = this;

        _adapter.add(object, communicator.stringToIdentity(name));
        _adapter.activate();
        System.out.println(name +" started!!!");
    }

    @Override
    public void stop() {
        System.out.println(this._adapter.getName()+" stoped!!!");
        _adapter.destroy();
    }

}

(3) SMSClient 代码

package com.smsice;

import message.SMSServicePrx;
import message.SMSServicePrxHelper;

public class SMSServiceClient {

    public static void main(String[] args) {
        int status =0;
        Ice.Communicator ic=null;
        try{
            //初始化Communicator连接对象,负责处理客户端的连接,可以进行设置,如客户端连接数量
            ic = Ice.Util.initialize(args);

            // 客户端代理proxy对象,访问确定的endpoints,返回的对象是广义的
            Ice.ObjectPrx base = ic.stringToProxy("SMSService:tcp -h localhost -p 10000");

            // 类似于强制转换, 转换为具体的对象。
            SMSServicePrx smsproxy = SMSServicePrxHelper.checkedCast(base);
            if(smsproxy==null)
            {
                throw new Error("Invalid proxy");
            }
            // 实际上是调用服务器端的方法。


            smsproxy.sendMSg("This a SmsService method");

        }catch(Exception e){
            e.printStackTrace();
            status=1;
        }finally {
            if (ic!= null){
                ic.destroy();
            }
        }
        System.exit(status);
    }

}

(4) 修改config.properties

IceBox.Service.OnlineBookService=com.bookice.OnlineBookImpl prop1=1 prop2=2 prop3=3
#IceBox.Service.SMSService=com.SMSServiceI2
OnlineBookService.Endpoints=tcp -p 10001 -h localhost


# SMSService
IceBox.Service.SMSService =com.smsice.SMSserviceImpl
SMSService.Endpoints=tcp -p 10000 -h localhost

#service end
#service load order
IceBox.LoadOrder=OnlineBookService,SMSService
IceBox.UseSharedCommunicator.OnlineBookService=1
IceBox.UseSharedCommunicator.SMSService=1

运行日志

-- 17-8-17 14:37:10:165 IceBox.Server: ThreadPool: creating Ice.ThreadPool.Client: Size = 4, SizeMax = 100, SizeWarn = 40
-- 17-8-17 14:37:10:236 IceBox.Server: Network: attempting to bind to tcp socket 127.0.0.1:9999
-- 17-8-17 14:37:10:238 IceBox.Server: Network: listening for tcp connections at 127.0.0.1:9999
-- 17-8-17 14:37:10:245 IceBox.Server: ThreadPool: creating Ice.ThreadPool.Server: Size = 4, SizeMax = 100, SizeWarn = 40
-- 17-8-17 14:37:10:245 IceBox.Server: Network: published endpoints for object adapter `IceBox.ServiceManager':
   tcp -h localhost -p 9999
-- 17-8-17 14:37:10:313 IceBox.Server: ThreadPool: creating Ice.ThreadPool.Client: Size = 4, SizeMax = 100, SizeWarn = 40
-- 17-8-17 14:37:10:344 IceBox.Server: Network: attempting to bind to tcp socket 127.0.0.1:10001
-- 17-8-17 14:37:10:346 IceBox.Server: Network: listening for tcp connections at 127.0.0.1:10001
-- 17-8-17 14:37:10:354 IceBox.Server: ThreadPool: creating Ice.ThreadPool.Server: Size = 4, SizeMax = 100, SizeWarn = 40
-- 17-8-17 14:37:10:355 IceBox.Server: Network: published endpoints for object adapter `OnlineBookService':
   tcp -h localhost -p 10001
-- 17-8-17 14:37:10:355 IceBox.Server: Network: accepting tcp connections at 127.0.0.1:10001
OnlineBookService started!!!
-- 17-8-17 14:37:10:359 IceBox.Server: Network: attempting to bind to tcp socket 127.0.0.1:10000
-- 17-8-17 14:37:10:362 IceBox.Server: Network: listening for tcp connections at 127.0.0.1:10000
-- 17-8-17 14:37:10:362 IceBox.Server: Network: published endpoints for object adapter `SMSService':
   tcp -h localhost -p 10000
-- 17-8-17 14:37:10:362 IceBox.Server: Network: accepting tcp connections at 127.0.0.1:10000
SMSService started!!!
MyAppIceBox 1 ready
-- 17-8-17 14:37:10:363 IceBox.Server: Network: accepting tcp connections at 127.0.0.1:9999
-- 17-8-17 14:37:16:600 IceBox.Server: Network: accepted tcp connection
   local address = 127.0.0.1:10000
   remote address = 127.0.0.1:55936
-- 17-8-17 14:37:16:617 IceBox.Server: Network: tcp connection established
   local address = 127.0.0.1:10000
   remote address = 127.0.0.1:55936
send msg:This a SmsService method
-- 17-8-17 14:37:16:637 IceBox.Server: Network: closing tcp connection
   local address = 127.0.0.1:10000
   remote address = 127.0.0.1:55936

上面的配置中,两个服务共享一个communicator,也共享同一个SharedCommunicator。

假如上面的发送短信服务增加一个需求,通过发送短信完成Book订购。需要在SMService服务内部发起对OnlineBookService的接口调用,这里的SMSService则是一个OnlineBookService的客户端。

(5) 修改SMSServiceImpl

@Override
    public void sendMSg(String msg, Current __current) {
        if(msg.startsWith("book")){
            try{
                Ice.ObjectPrx base =_adapter.getCommunicator().stringToProxy("OnlineBookService:tcp -h localhost -p 10001");
                OnlineBookPrx bookproxy = OnlineBookPrxHelper.checkedCast( base);
                Message bookmsg= new Message();
                bookmsg.name="MrWang";
                bookmsg.type=3;
                bookmsg.price=99;
                bookmsg.valid=true;
                bookmsg.content="abcdef";
                Message retMsg = bookproxy.bookTick(bookmsg);
                System.out.println("retMsg:"+retMsg.content);

            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

(6) 运行Server和SMSClient

-- 17-8-17 14:54:28:329 IceBox.Server: Network: accepted tcp connection
   local address = 127.0.0.1:10000
   remote address = 127.0.0.1:56244
-- 17-8-17 14:54:28:329 IceBox.Server: Network: tcp connection established
   local address = 127.0.0.1:10000
   remote address = 127.0.0.1:56244
-- 17-8-17 14:54:28:352 IceBox.Server: ThreadPool: growing Ice.ThreadPool.Server: Size=2
BookTick to call. 
booktick called: abcdef
retMsg:JackTicket
-- 17-8-17 14:54:28:357 IceBox.Server: Network: closing tcp connection
   local address = 127.0.0.1:10000
   remote address = 127.0.0.1:56244

尽管日志中并没有体现出来从10000到10001 的访问过程,但是打印的信息已经证明确实访问了OnlineBookService

上面的一个明显的问题是: 在SMSServiceImpl中访问OnLineBookService必须固定一个Endpoints,写死的形式,并且上面是两个服务,如果多个服务则如何进行负载均衡。

以服务注册表Registry为依托的Service Locator组件,以及依赖其而诞生的强大的分布式框架-IceGrid来进行集群管理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值