ICE java实现helloworld

9 篇文章 1 订阅
4 篇文章 1 订阅

目录

一 前言

二 实现

1、目录结构

2、java实现

3、spring实现(spring启动ice)

三 项目git  https://github.com/HandsomeMars/springboot-ice-demo


一 前言

1、基于上篇slice2java编译,本文接着使用slice2java实现简单的客户端和服务端

2、本文列举两种实现:

  • java main方法调用
  • 结合spring IOC容器管理

二 实现

1、目录结构

src
├─main
│  ├─java
│  │  ├─com
│  │  │  └─zyj
│  │  │      │  SpringbootIceApplication.java
│  │  │      │
│  │  │      ├─ice
│  │  │      │  ├─java     ##java实现
│  │  │      │  │  ├─client
│  │  │      │  │  │      IceClient.java
│  │  │      │  │  │
│  │  │      │  │  └─server
│  │  │      │  │          HelloServant.java
│  │  │      │  │          IceService.java
│  │  │      │  │
│  │  │      │  └─spring   ##spring结合
│  │  │      │      ├─client
│  │  │      │      │      IceClient.java
│  │  │      │      │
│  │  │      │      └─server
│  │  │      │              HelloServant.java
│  │  │      │              IceService.java
│  │  │      │
│  │  │      └─util
│  │  │              SpringContextUtil.java
│  │  │
│  │  └─slice2java        ##slice生成文件
│  │          Callback_HelloI_hello.java
│  │          HelloI.java
│  │          HelloIHolder.java
│  │          HelloIPrx.java
│  │          HelloIPrxHelper.java
│  │          HelloIPrxHolder.java
│  │          _Callback_HelloI_hello.java
│  │          _HelloIDisp.java
│  │          _HelloIOperations.java
│  │          _HelloIOperationsNC.java
│  │
│  └─resources
│      │  application.properties
│      │
│      └─slice
│              slice.bat
│              test.ice
│
└─test
    └─java
        └─com
            └─zyj
                    SpringbootIceApplicationTests.java

2、java实现

step1: HelloServant 实现服务端  _xxxDisp抽象类

package com.zyj.ice.java.server;

import Ice.Current;
import Ice.StringHolder;
import slice2java._HelloIDisp;

/**
 * @author by zyj
 * @version V1.0
 * @Description:
 * @Date 2019/8/6 21:13
 */
public class HelloServant extends _HelloIDisp {
    @Override
    public int hello(String instr, StringHolder outstr, Current __current) {
        System.out.println("server接受:"+instr+" 时间:"+System.currentTimeMillis());
        outstr.value="hello client";
        return 0;
    }
}

setp2:配置server 

package com.zyj.ice.java.server;

/**
 * @author by zyj
 * @version V1.0
 * @Description:
 * @Date 2019/8/6 21:13
 */
public class IceService {
    /**服务名*/
    private static final String SERVER_NAME="Hello";
    /**服务端点*/
    private static final String SERVER_ENDPOINT="tcp -p 10006";


    public static void main(String[] args) {
        //获取实现类 SpringContextUtil.getBean(helloServant)
        HelloServant helloServant=new HelloServant();

        //ice通信器
        Ice.Communicator communicator = null;
        try {
            //初始化ice通信器communicator,可以使用args传入一下ice初始化的参数如超时时间,线程池大小等
            communicator = Ice.Util.initialize(args);

            //创建一个名为queryEmployeeAdapter的适配器并且默认使用tcp协议  服务部署在10.4.30.81机器上 服务开启10006监听端口

            Ice.ObjectAdapter adapter = communicator.createObjectAdapterWithEndpoints(SERVER_NAME,SERVER_ENDPOINT);

            // 将servant与ice对象标识符建立映射关系,并添加到ice对象适配器中
            adapter.add(helloServant, Ice.Util.stringToIdentity(SERVER_NAME));

            // 激活对象适配器
            adapter.activate();

            System.out.println("服务启动");
            // 服务在退出之前一直保持监听状态
            communicator.waitForShutdown();

        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            //异常销毁通信器
            if(communicator != null){
                communicator.destroy();
            }
        }
    }


}

step3:IceClient 使用 xxxPrxHelper 获取 xxxPrx

package com.zyj.ice.java.client;

import Ice.StringHolder;
import slice2java.HelloIPrx;
import slice2java.HelloIPrxHelper;

/**
 * @author by zyj
 * @version V1.0
 * @Description:
 * @Date 2019/8/6 21:13
 */
public class IceClient {

    /**服务名*/
    private static final String SERVER_NAME="Hello";
    /**服务端点*/
    private static final String SERVER_ENDPOINT="tcp -h 127.0.0.1 -p 10006";


    public static void main(String[] args) {
        //ice通信器
        Ice.Communicator communicator = null;
        try {
            //初始化ice通信器communicator,可以使用args传入一下ice初始化的参数如超时时间,线程池大小等
            communicator = Ice.Util.initialize(args);

            //构建服务端的代理对象  服务端对象标识以 SERVER_NAME:SERVER_ENDPOINT 格式
            Ice.ObjectPrx op = communicator.stringToProxy(SERVER_NAME+":"+SERVER_ENDPOINT);

            //检查通用客户端代理op 是不是queryServer对象标识符所关联的ice对象的代理
            HelloIPrx qp = HelloIPrxHelper.checkedCast(op);

            if(qp == null){
                throw new Exception("qp == null");
            }

            //测试发送信息到户无端
            StringHolder holder=new StringHolder();
            int result = qp.hello("hello",holder);

            // 输出服务端返回结果
            System.out.println("client接受:"+holder.value+" 时间:"+System.currentTimeMillis());

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

        }
    }

}

step4:启动验证

启动server

 启动client

3、spring实现(spring启动ice)

step1:改造服务实现类(添加@Service注解)

package com.zyj.ice.spring.server;

import Ice.Current;
import Ice.StringHolder;
import org.springframework.stereotype.Service;
import slice2java._HelloIDisp;

/**
 * @author by zyj
 * @version V1.0
 * @Description:
 * @Date 2019/8/6 21:13
 */
@Service
public class HelloServant extends _HelloIDisp {
    @Override
    public int hello(String instr, StringHolder outstr, Current __current) {
        System.out.println("server接受:"+instr+" 时间:"+System.currentTimeMillis());
        outstr.value="hello client";
        return 0;
    }
}

 step2:改造server  主要防止ice阻塞,所以通过线程处理

package com.zyj.ice.spring.server;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author by zyj
 * @version V1.0
 * @Description:
 * @Date 2019/8/6 21:13
 */
@Service
public class IceService implements Runnable{

    /**服务名*/
    private static final String SERVER_NAME="Hello";
    /**服务端点*/
    private static final String SERVER_ENDPOINT="tcp -p 10006";

    @Autowired
    private HelloServant helloServant;

    @PostConstruct
    private void startIceServer() {
        //构造线程池启动当前任务
        LinkedBlockingQueue<Runnable> runnableList=new LinkedBlockingQueue<Runnable>();
        ThreadPoolExecutor threadPoolExecutor= new ThreadPoolExecutor(100,100,1L, TimeUnit.SECONDS,runnableList);
        threadPoolExecutor.execute(this);
    }


    @Override
    public void run() {
        //ice通信器
        Ice.Communicator communicator = null;
        try {
            //初始化ice通信器communicator,可以使用args传入一下ice初始化的参数如超时时间,线程池大小等
            communicator = Ice.Util.initialize();

            //创建一个名为queryEmployeeAdapter的适配器并且默认使用tcp协议  服务部署在10.4.30.81机器上 服务开启10006监听端口

            Ice.ObjectAdapter adapter = communicator.createObjectAdapterWithEndpoints(SERVER_NAME,SERVER_ENDPOINT);

            // 将servant与ice对象标识符建立映射关系,并添加到ice对象适配器中
            adapter.add(helloServant, Ice.Util.stringToIdentity(SERVER_NAME));

            // 激活对象适配器
            adapter.activate();

            System.out.println("服务启动");
            // 服务在退出之前一直保持监听状态
            communicator.waitForShutdown();

        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            //异常销毁通信器
            if(communicator != null){
                communicator.destroy();
            }
        }

    }
}

step3:改造客户端(客户端连接服务端后,可以保持服务端xxxPrx对象持续调用)

package com.zyj.ice.spring.client;

import Ice.StringHolder;
import org.springframework.stereotype.Service;
import slice2java.HelloIPrx;
import slice2java.HelloIPrxHelper;

import javax.annotation.PostConstruct;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author by zyj
 * @version V1.0
 * @Description:
 * @Date 2019/8/6 21:13
 */
@Service
public class IceClient implements Runnable{

    /**服务名*/
    private static final String SERVER_NAME="Hello";
    /**服务端点*/
    private static final String SERVER_ENDPOINT="tcp -h 127.0.0.1 -p 10006";


    @PostConstruct
    private void startIceClient() {
        //构造线程启动客户端
        LinkedBlockingQueue<Runnable> runnableList=new LinkedBlockingQueue<Runnable>();
        ThreadPoolExecutor threadPoolExecutor= new ThreadPoolExecutor(100,100,1L, TimeUnit.SECONDS,runnableList);
        threadPoolExecutor.execute(this);
    }


    @Override
    public void run() {
        //ice通信器
        Ice.Communicator communicator = null;
        try {
            //初始化ice通信器communicator,可以使用args传入一下ice初始化的参数如超时时间,线程池大小等
            communicator = Ice.Util.initialize();

            //构建服务端的代理对象  服务端对象标识以 SERVER_NAME:SERVER_ENDPOINT 格式
            Ice.ObjectPrx op = communicator.stringToProxy(SERVER_NAME+":"+SERVER_ENDPOINT);

            //检查通用客户端代理op 是不是queryServer对象标识符所关联的ice对象的代理
            HelloIPrx qp = HelloIPrxHelper.checkedCast(op);

            if(qp == null){
                throw new Exception("qp == null");
            }

            //测试发送信息到户无端
            StringHolder holder=new StringHolder();
            int result = qp.hello("hello",holder);

            // 输出服务端返回结果
            System.out.println("client请求结果:"+holder.value+" 时间:"+System.currentTimeMillis());

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

        }
    }
}

step4 启动

三 项目git  https://github.com/HandsomeMars/springboot-ice-demo

https://github.com/HandsomeMars/springboot-ice-demo

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Iceberg CDC(Change Data Capture)是一种常见的数据同步方式,用于将数据库中的变更同步到其他系统或数据仓库中。Iceberg CDC 通常包括以下步骤: 1. 捕获变更:通过轮询数据库的事务日志或使用数据库提供的 CDC 功能捕获变更。对于每个变更,记录变更的类型、时间戳、表名、主键值和变更的数据。 2. 转换数据:将捕获的变更数据转换为目标系统可接受的格式。通常需要进行一些数据转换和规范化,确保数据的一致性和完整性。 3. 传输数据:将转换后的数据传输到目标系统。可以通过消息队列、HTTP 接口等方式传输数据。 4. 应用变更:在目标系统中应用变更。通常需要维护一个增量的数据集,以便每次同步只应用新增、修改和删除的变更,避免重复数据和性能问题。 Iceberg CDC 的实现可以基于开源的 CDC 工具如 Debezium、Maxwell 等,也可以自行开发。通常需要考虑以下问题: 1. 数据库日志格式:不同的数据库使用不同的日志格式,需要了解数据库日志的格式和捕获方式。 2. 数据转换:需要将捕获的变更数据转换为目标系统可接受的格式。可以使用开源的 ETL 工具如 Apache NiFi、Apache Kafka Connect 等,也可以自行开发。 3. 数据传输:需要选择适合的数据传输方式,如消息队列、HTTP 接口等。 4. 数据一致性和完整性:需要确保数据的一致性和完整性,尤其是在多个目标系统中同步数据时。可以使用事务、幂等性等技术来保证数据的一致性和完整性。 总之,Iceberg CDC 是一种非常常见的数据同步方式,可以帮助组织实现数据的实时同步和集成。但是,实现 Iceberg CDC 需要考虑多种因素,包括数据库日志格式、数据转换、数据传输、数据一致性和完整性等,需要仔细设计和实现

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mars'Ares

请我喝杯咖啡吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值