• [编织消息框架][JAVA核心技术]动态代理应用11-水平扩展实现


    由于示例,远程服务地址配置在properties文件,通过QMConfig类加载,最优方式是上节介绍过,放在共享内存上,只需要维护一份数据即可,如放在redis上

        /** 服务地址<service,address> **/
        @FieldValue("QM.REMOTE_SERVICE")
        public Map<String, String> REMOTE_SERVICE = new HashMap<>();
    
        public boolean isRemoteService(Class<?> target) {
            if (!target.isInterface()) {
                target = ReflectUtil.getInterfaceForAnno(target, QModel.class);
            }
            return REMOTE_SERVICE.containsKey(target.getName());
        }

     qm.properties 加上

    QM.REMOTE_SERVICE={"com.eyu.onequeue.rpc.ITestEnhance":"xxx.xxx.xxx.xxx:22222"}

    QRpcFactory.class 添加代码

        public static <T> T loadSystemService(Class<T> target) {
            T ret = null;
            if (QMConfig.getInstance().isRemoteService(target)) {
                // read config
                String address = QMConfig.getInstance().REMOTE_SERVICE.get(target.getName());
                ret = loadSendProxy(target, address);
            } else {
                ret = loadLocalProxy(target);
            }
            return ret;
        }

    测试代码:

        @Test
        public void testSystemService() {
            Set<Class<?>> values = new HashSet<>();
            PackageScanner.scan((clz) -> {
                QModel modelAnno = ReflectUtil.getAnno(clz, QModel.class);
                if (modelAnno == null) {
                    return;
                }
                values.add(clz);
            } , "com.eyu.onequeue");
    
            for (Class<?> clz : values) {
                if (clz.isInterface()) {
                    if (!QMConfig.getInstance().isRemoteService(clz)) {
                        continue;
                    }
                    QRpcFactory.registerSendProxy(clz);
                    System.out.println("registerSendProxy : " + clz);
                } else {
                    if (QMConfig.getInstance().isRemoteService(clz)) {
                        continue;
                    }
                    try {
                        QRpcFactory.registerReceiveProxy(clz.newInstance());
                        System.out.println("registerReceiveProxy : " + clz);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
    
            //test removet
            ITestEnhance obj = QRpcFactory.loadSystemService(ITestEnhance.class);
            obj.a(1d);
            //test local
            TestObject obj1 = QRpcFactory.loadSystemService(TestObject.class);
            obj1.a(1, "b");
        }
  • 相关阅读:
    day1 生活大爆炸版石头剪刀布
    友谊赛
    再数17
    素数统计
    day1 LGTB玩THD
    day1 LGTB学分块
    day1 LGTB玩扫雷
    组合(1-m中选n个数)
    二分查找法,加递归,之前做了一个没加递归,结果就废了
    以前的一个程序,死循环,骚年,卡爆你的电脑吧
  • 原文地址:https://www.cnblogs.com/solq111/p/6722243.html
Copyright © 2020-2023  润新知