nacos java配置与Python配置匹配

一、获取并处理两边的配置

1.有python配置

文件名p.py

c = 3
d = True
a = "1"
b = 3
e = "http://192.168.1.182:8198/police/oauth/place/batch/putOrPost"

2.有nacos java配置

已转为字典

data={'A':{'a':"1",'b':2},'c':3,'d':True,'B':{'C':{'D':{'e':'http://192.168.1.182:8198/police/oauth/place/batch/putOrPost'}}}}

获取nacos配置并转为dict参考

import nacos
import yaml
from pprint import pprint                               # 用于字典美化输出

SERVER_ADDRESSES = "192.168.7.161:8848"
NAMESPACE = "public"                                    # namespace id

"""get conf from nacos"""
data_id = "application-dev.yml"
group = "police-org"
def get_nacos_conf(data_id, group):
    try:
        client = nacos.NacosClient(SERVER_ADDRESSES, namespace=NAMESPACE, username="nacos", password="nacos")
    except Exception as e:
        print(f'连接失败{e}')
        raise e
    else:
        nacos_conf = client.get_config(data_id, group, 5000)  # 
    return nacos_conf
conf_java = yaml.load(get_nacos_conf(data_id, group))      # 转为字典,,有warning不管

二、配置匹配

import p
from jsonpath import jsonpath


data={'A':{'a':"1"},'b':3,'d':True,'B':{'C':{'D':{'e':'http://192.168.1.182:8198/police/oauth/place/batch/putOrPost'}}}}


for i in dir(p):# 获取成员
    if i != "os" and "__" not in i: # 剔除非配置成员
        #print(i,eval("".join(["p.", i])))# 利用eval函数将字符串转为表达式
        conf_from_python = eval("".join(["p.", i]))         #格式没问题,i自动按照顺序输出a b c...
        
        try:
            conf_from_java = jsonpath(data, ''.join(['$..',i]))[0]# jsonpath(dict, key), 
            #print(conf_from_java,type(conf_from_java))
            
            if conf_from_python == conf_from_java:
                print('参数{}匹配'.format(i))
            else:
                print('参数{}不匹配,请检查'.format(i))

        except TypeError:
            print("参数{}不存在".format(i)) #conf_from_java中的参数conf_from_python必须存在,所以跳过

你可能感兴趣的:(大数据开发,运维,大数据,python,java)