ClusterDB & MyCat 与 Apollo对接 实战

Posted 微创智云

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ClusterDB & MyCat 与 Apollo对接 实战相关的知识,希望对你有一定的参考价值。

为什么选择Apollo

具体可以看 如下比对:

https://github.com/ctripcorp/apollo/files/983064/default.pdf

比较相对清楚

 

Apollo配置

下载1.1.2

如何配置查看如下 连接

https://github.com/ctripcorp/apollo/wiki/Java%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97

 

 

(重点)Apollo与数据库中间件的结合

 

package io.clusterdb.config;

 

import java.io.BufferedReader;

import java.io.ByteArrayInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.util.HashMap;

import java.util.Map;

import java.util.Properties;

 

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

 

import com.ctrip.framework.apollo.Config;

import com.ctrip.framework.apollo.ConfigChangeListener;

import com.ctrip.framework.apollo.ConfigFile;

import com.ctrip.framework.apollo.ConfigService;

import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;

import com.ctrip.framework.apollo.model.ConfigChange;

import com.ctrip.framework.apollo.model.ConfigChangeEvent;

 

import io.mycat.util.StringUtil;

 

/****

 * Apollo配置中心使用入口管理

 * @author Fx_demon

 */

publicclass ApolloConfigManager {

    privatestaticfinal Logger logger = LoggerFactory.getLogger(ApolloConfigManager.class);

   

    privatestatic String DEFAULT_VALUE = "undefined";

    private Config config;

   

    /****

     * 配置中心是否启用  默认为未启动,需要手动设置为true

     * META-INF/app.properties文件中      conf/wrapper.conf启动参数配置

     */

    privateboolean  apolloEnabled = false;

   

    /****

     * 初始化参数信息:

       -Dapollo.enabled=false

       -Denv=DEV

       -Dapp.id=ClusterDB

       -Dapollo.cluster=

       -Dapollo.cacheDir=C:\opt\data\simple-cache-dir

       -Dapollo.meta=http://localhost:8080

     */

    privatevoid pringInitInfo() {

      

       apolloEnabled = Boolean.parseBoolean(System.getProperty("apollo.enabled"));

       this.setApolloEnabled(apolloEnabled);

      

       String env = System.getProperty("env");

       String appId = System.getProperty("app.id");

       String apolloCluster = System.getProperty("apollo.cluster");

       String apolloCacheDir = System.getProperty("apollo.cacheDir");

       String apolloMeta = System.getProperty("apollo.meta");

       logger.info("===============================================");

       logger.info(" Apollo "+ (getApolloEnabled()?"启用,初始化成功":" 禁用  ") );

      

       if(logger.isDebugEnabled() ) {

           logger.debug(" 配置中心初始化参数信息: \n apolloEnabled={},  app.id={}, env={},  apollo.cluster={},  apollo.cacheDir={},  apollo.meta={} .",

                  getApolloEnabled(), appId, env, apolloCluster, apolloCacheDir, apolloMeta);

       }

       logger.info("===============================================");

    }

   

    privatestaticvolatile ApolloConfigManager singleton;

 

    private ApolloConfigManager() {

   

    this.pringInitInfo();

   

    ConfigChangeListenerchangeListener = new ConfigChangeListener() {

           @Override

           publicvoid onChange(ConfigChangeEvent changeEvent) {

              logger.info("Changes fornamespace {}", changeEvent.getNamespace());

              for (String key : changeEvent.changedKeys()) {

                  ConfigChange change = changeEvent.getChange(key);

                  logger.info("Change -key: {}, oldValue: {}, newValue: {}, changeType: {}", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType());

                 

                  //TODO监听到配置变更自动发起reload 配置信息

                 

              }

           }

       };

       config = ConfigService.getAppConfig();

       config.addChangeListener(changeListener);

    }

 

    publicstatic ApolloConfigManagergetInstance() {

        if (singleton == null) {

            synchronized(ApolloConfigManager.class) {

                if (singleton == null) {

                    singleton = new ApolloConfigManager();

                }

            }

        }

        returnsingleton;

    }

   

    /***

     * 获取properties格式namespace的配置

     * @param key

     * @return

     */

    public String getConfig(String key) {

       String result = config.getProperty(key, DEFAULT_VALUE);

       logger.info(String.format("Loading key : %s with value: %s", key, result));

       returnresult;

    }

   

    /***

     * 获取非properties格式namespace的配置

     * @param key

     * @return

     */

    public String getConfigFile(String key) {

       ConfigFile configFile = ConfigService.getConfigFile(key, ConfigFileFormat.XML);

       String result = configFile.getContent();

       logger.info(String.format("Loading key : %s with value: %s", key, result));

       returnresult;

    }

   

    /***

     * String –> InputStream

     * @param str

     * @return

     */

    public InputStream strToInputStrem(String str) {

       InputStream is  =  null ;

       if(!StringUtil.isEmpty(str)) {

           is = new ByteArrayInputStream(str.getBytes());

       }

       returnis;

    }

   

    publicboolean getApolloEnabled() {

       returnapolloEnabled;

    }

 

    publicvoid setApolloEnabled(booleanapolloEnabled) {

       this.apolloEnabled = apolloEnabled;

    }

   

   

    publicstaticvoid main(String[] args) throws IOException {

       ApolloConfigManager apolloConfigDemo = ApolloConfigManager.getInstance();

        

       apolloConfigDemo.getConfig("kk");

       apolloConfigDemo.getConfigFile("server");

    }

 

}

 

 

修改MyCat此类

XMLServerLoader.java

ApolloConfigManager apolloConfigManager =  ApolloConfigManager.getInstance();

        if(apolloConfigManager.getApolloEnabled()) {

            StringxmlStr = apolloConfigManager.getConfigFile("server");

            xml = apolloConfigManager.strToInputStrem(xmlStr);

        }else {

                xml = XMLServerLoader.class.getResourceAsStream("/server.xml");

        }

 

 

 

首先在Apollo中配置如下操作



 


 

ClusterDB & MyCat 与 Apollo对接 实战


ClusterDB & MyCat 与 Apollo对接 实战


点击 发布 到此Apollo中server.xml文件配置完成。

 

启动配置如下:


 

-Dapollo.enabled=true

-Denv=DEV

-Dapp.id=SampleApp

-Dapollo.cluster=

-Dapollo.cacheDir=C:\opt\data\simple-cache-dir

-Dapollo.meta=http://localhost:8080

 

 


以上是关于ClusterDB & MyCat 与 Apollo对接 实战的主要内容,如果未能解决你的问题,请参考以下文章

Mycat分表分库  

Mycat分表分库 

MyCat & MySQL多节点的Docker虚拟化实践

mycat的安装与配置

关于MyCAT字符集的验证

c++能访问mycat的吗