zookeeper for the distribution center (store payment information)

zookeeper as distribution center (store sensitive information)

Premise: Recently in the project need to use payment interface, Alipay or micro-channel payment, according to the official document, you need to configure some such notify-url, or app-private-keyother information, that information is sensitive, and is a co-development, in order to avoid some of the unsafe things, and decided to this information is stored in the zookeeper, which is the main reason, another reason is the need to change the configuration once, the change can be directly zookeeper without restarting backend services, here involves the related underlying zookeeper long connections and NIO network programming, not repeat them here.

1, first install the zookeeper on the server side, the default port is 2181, the installation tutorial does not explain

2, in this unit (Window 10) mounted ZKUI (ZK visualized client), to facilitate the management zookeeper

3, the mounting zookeeper java client (Curator)

4, reads the configuration information and set listeners


@Configuration
class PaymentConfig {

    @Value("\${custom-config.zookeeper.url}")
    lateinit var zkUrl: String

    @Value("\${custom-config.zookeeper.zfb-nodename}")
    lateinit var zfbNodeName: String

    @Value("\${custom-config.zookeeper.wx-nodename}")
    lateinit var wxNodeName: String

    val remoteProperties: Properties = Properties()

    companion object {
        /**
         * zookeeper节点映射在本地的键值
         */
        private const val ALI_APP_ID = "/pay-server-config/pay.alipay/app-id"
        private const val ALI_APP_PRIVATE_KEY = "/pay-server-config/pay.alipay/app-private-key"
        private const val ALI_NOTIFY_URL = "/pay-server-config/pay.alipay/notify-url"
        private const val ALI_PUBLIC_KEY = "/pay-server-config/pay.alipay/public-key"

        private const val WX_NOTIFY_URL = "/pay-server-config/pay.wxpay/notify-url"
        private const val WX_APP_ID = "/pay-server-config/pay.wxpay/app-id"
        private const val WX_APP_PRIVATE_KEY = "/pay-server-config/pay.wxpay/app-private-key"
        private const val WX_MCH_ID = "/pay-server-config/pay.wxpay/mch-id"
    }

    @PostConstruct
    fun init(){
        setPaymentConfigFromZk(remoteProperties)
    }

    fun setPaymentConfigFromZk(remoteProperties: Properties){
        val zkClient: CuratorFramework = CuratorFrameworkFactory.newClient(zkUrl, RetryOneTime(1000))
        zkClient.start()
        try {
            val zfbConfigNames = zkClient.children.forPath("/$zfbNodeName")
            val wxConfigNames = zkClient.children.forPath("/$wxNodeName")
            for (configName in zfbConfigNames) {
                val value = String(zkClient.data.forPath("/$zfbNodeName/$configName"))
                remoteProperties["/$zfbNodeName/$configName"] = value
            }
            for (configName in wxConfigNames) {
                val value = String(zkClient.data.forPath("/$wxNodeName/$configName"))
                remoteProperties["/$wxNodeName/$configName"] = value
            }
            setListener(zkClient, zfbNodeName, remoteProperties)
            setListener(zkClient, wxNodeName, remoteProperties)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun setListener(zkClient: CuratorFramework, uri: String, remoteProperties: Properties){
        val treeCache = TreeCache(zkClient, "/$uri")
        treeCache.start()
        treeCache.listenable.addListener(TreeCacheListener(
                fun(client: CuratorFramework, event: TreeCacheEvent) {
                    when (event.type) {
                        TreeCacheEvent.Type.NODE_UPDATED -> {
                            val value = String(event.data.data)
                            remoteProperties[event.data.path] = value
                            println("Payment URL has changed, the changed is ${String(event.data.data)}")
                            println("Payment URL has changed, the path is ${event.data.path}")
                        }
                        else -> {
                            print("Now, get the config from zookeeper ${event.data}")
                        }
                    }
                }
        )
        )
    }
}

Effect that the above code: zookeeper read value in the initialization time, and assigns to the local Properties, key zookeeper full path to the node, the value of the Value leaf node, then using varying TreeCache listening client, when a leaf after the value of the node is changed, changing the original Properties, replaced with the new value.

Guess you like

Origin www.cnblogs.com/tian874540961/p/11958559.html