Dubbo源代碼分析-configuration

本文深入分析Dubbo配置模块的工作原理,包括如何通过Spring初始化Dubbo组件、解析XML配置文件及配置对象的创建过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

dubbo的官方網站為:

http://code.alibabatech.com/wiki/display/dubbo/User+Guide#UserGuide-ServiceProvider

 

 

这个章节分析Dubboconfigure模块。

Spring启动的时候,会到spring.handlers文件中查找相应的namespacehandler, 并将其存入到mapping中。如Dubbo中的配置为:

http\://code.alibabatech.com/schema/dubbo=com.alibaba.dubbo.config.spring.schema.DubboNamespaceHandler

当我们自定义的namespace中包含xmlnshttp://code.alibabatech.com/schema/dubbo时,其相应的bean都会通过DubboNamespaceHandler这个类来构建。例如下面是provider.xml的配置

 

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        ">

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="hello-world-app"  />

    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry address="multicast://224.5.6.7:1234" />

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" />

    <!-- 和本地bean一样实现服务 -->
    <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" />

</beans>
 

接下来查看DubboNamespaceHandler的源代码,我们可以知道DubboNamespaceHandler继成了NamespaceHandlerSupport,并且在init方法里面注册了不同configure对象的DubboBeanDefinitionParser.

 

public class DubboNamespaceHandler extends NamespaceHandlerSupport {

                static {
                                Version.checkDuplicate(DubboNamespaceHandler.class);
                }

                public void init() {
                    registerBeanDefinitionParser("application", new DubboBeanDefinitionParser(ApplicationConfig.class, true));
        registerBeanDefinitionParser("module", new DubboBeanDefinitionParser(ModuleConfig.class, true));
        registerBeanDefinitionParser("registry", new DubboBeanDefinitionParser(RegistryConfig.class, true));
        registerBeanDefinitionParser("monitor", new DubboBeanDefinitionParser(MonitorConfig.class, true));
        registerBeanDefinitionParser("provider", new DubboBeanDefinitionParser(ProviderConfig.class, true));
        registerBeanDefinitionParser("consumer", new DubboBeanDefinitionParser(ConsumerConfig.class, true));
        registerBeanDefinitionParser("protocol", new DubboBeanDefinitionParser(ProtocolConfig.class, true));
        registerBeanDefinitionParser("service", new DubboBeanDefinitionParser(ServiceBean.class, true));
        registerBeanDefinitionParser("reference", new DubboBeanDefinitionParser(ReferenceBean.class, false));
        registerBeanDefinitionParser("annotation", new DubboBeanDefinitionParser(AnnotationBean.class, true));
    }

}
 

 

spring解析provider.xml文件时, 当其解析   <dubbo:application name="hello-world-app"  />这句配置时,spring会将这句配置封装成一个element对象,在NamespaceHandlerSupport类中findParserForElement(element, parserContext).parse(element, parserContext); 按照相应的localname查找parser(此时的localnameapplication)。查找到的对象为DubboNamespaceHandler 中创建的new DubboBeanDefinitionParser(ApplicationConfig.class, true)); 对象。

 

接下来调用DubboBeanDefinitionParser中的parse函数,将配置设置到beanDefinition中,最后通过spring创建相应的bean

 

private static BeanDefinition parse(Element element, ParserContext parserContext, Class<?> beanClass, boolean required) {
        RootBeanDefinition beanDefinition = new RootBeanDefinition();
        beanDefinition.setBeanClass(beanClass);
        beanDefinition.setLazyInit(false);
        String id = element.getAttribute("id");
        if ((id == null || id.length() == 0) && required) {
                String generatedBeanName = element.getAttribute("name");
                if (generatedBeanName == null || generatedBeanName.length() == 0) {
                    if (ProtocolConfig.class.equals(beanClass)) {
                        generatedBeanName = "dubbo";
                    } else {
                        generatedBeanName = element.getAttribute("interface");
                    }
                }
                if (generatedBeanName == null || generatedBeanName.length() == 0) {
                                generatedBeanName = beanClass.getName();
                }
            id = generatedBeanName;
            int counter = 2;
            while(parserContext.getRegistry().containsBeanDefinition(id)) {
                id = generatedBeanName + (counter ++);
            }
        }
        if (id != null && id.length() > 0) {
            if (parserContext.getRegistry().containsBeanDefinition(id))  {
                                throw new IllegalStateException("Duplicate spring bean id " + id);
                }
            //如果ID不冲突,注册bean definition
            parserContext.getRegistry().registerBeanDefinition(id, beanDefinition);
            beanDefinition.getPropertyValues().addPropertyValue("id", id);
        }
        if (ProtocolConfig.class.equals(beanClass)) {
            for (String name : parserContext.getRegistry().getBeanDefinitionNames()) {
                BeanDefinition definition = parserContext.getRegistry().getBeanDefinition(name);
                PropertyValue property = definition.getPropertyValues().getPropertyValue("protocol");
                if (property != null) {
                    Object value = property.getValue();
                    if (value instanceof ProtocolConfig && id.equals(((ProtocolConfig) value).getName())) {
                        definition.getPropertyValues().addPropertyValue("protocol", new RuntimeBeanReference(id));
                    }
                }
            }
        } else if (ServiceBean.class.equals(beanClass)) {
            String className = element.getAttribute("class");
            if(className != null && className.length() > 0) {
                RootBeanDefinition classDefinition = new RootBeanDefinition();
                classDefinition.setBeanClass(ReflectUtils.forName(className));
                classDefinition.setLazyInit(false);
                parseProperties(element.getChildNodes(), classDefinition);
                beanDefinition.getPropertyValues().addPropertyValue("ref", new BeanDefinitionHolder(classDefinition, id + "Impl"));
            }
        } else if (ProviderConfig.class.equals(beanClass)) {
            parseNested(element, parserContext, ServiceBean.class, true, "service", "provider", id, beanDefinition);
        } else if (ConsumerConfig.class.equals(beanClass)) {
            parseNested(element, parserContext, ReferenceBean.class, false, "reference", "consumer", id, beanDefinition);
        }
        Set<String> props = new HashSet<String>();
        ManagedMap parameters = null;
        for (Method setter : beanClass.getMethods()) {
            String name = setter.getName();
            if (name.length() > 3 && name.startsWith("set")
                    && Modifier.isPublic(setter.getModifiers())
                    && setter.getParameterTypes().length == 1) {
                Class<?> type = setter.getParameterTypes()[0];
                String property = StringUtils.camelToSplitName(name.substring(3, 4).toLowerCase() + name.substring(4), "-");
                props.add(property);
                Method getter = null;
                try {
                    getter = beanClass.getMethod("get" + name.substring(3), new Class<?>[0]);
                } catch (NoSuchMethodException e) {
                    try {
                        getter = beanClass.getMethod("is" + name.substring(3), new Class<?>[0]);
                    } catch (NoSuchMethodException e2) {
                    }
                }
                if (getter == null
                        || ! Modifier.isPublic(getter.getModifiers())
                        || ! type.equals(getter.getReturnType())) {
                    continue;
                }
                if ("parameters".equals(property)) {
                    parameters = parseParameters(element.getChildNodes(), beanDefinition);
                } else if ("methods".equals(property)) {
                    parseMethods(id, element.getChildNodes(), beanDefinition, parserContext);
                } else if ("arguments".equals(property)) {
                    parseArguments(id, element.getChildNodes(), beanDefinition, parserContext);
                } else {
                    String value = element.getAttribute(property);
                    if (value != null) {
                                value = value.trim();
                                if (value.length() > 0) {
                                                if ("registry".equals(property) && RegistryConfig.NO_AVAILABLE.equalsIgnoreCase(value)) {
                                RegistryConfig registryConfig = new RegistryConfig();
                                registryConfig.setAddress(RegistryConfig.NO_AVAILABLE);
                                beanDefinition.getPropertyValues().addPropertyValue(property, registryConfig);
                            } else if ("registry".equals(property) && value.indexOf(',') != -1) {
                                                                parseMultiRef("registries", value, beanDefinition, parserContext);
                            } else if ("provider".equals(property) && value.indexOf(',') != -1) {
                                parseMultiRef("providers", value, beanDefinition, parserContext);
                            } else if ("protocol".equals(property) && value.indexOf(',') != -1) {
                                parseMultiRef("protocols", value, beanDefinition, parserContext);
                            } else {
                                Object reference;
                                if (isPrimitive(type)) {
                                    if ("async".equals(property) && "false".equals(value)
                                            || "timeout".equals(property) && "0".equals(value)
                                            || "delay".equals(property) && "0".equals(value)
                                            || "version".equals(property) && "0.0.0".equals(value)
                                            || "stat".equals(property) && "-1".equals(value)
                                            || "reliable".equals(property) && "false".equals(value)) {
                                        // 兼容旧版本xsd中的default值
                                        value = null;
                                    }
                                    reference = value;
                                } else if ("protocol".equals(property)
                                        && ExtensionLoader.getExtensionLoader(Protocol.class).hasExtension(value)
                                        && (! parserContext.getRegistry().containsBeanDefinition(value)
                                                || ! ProtocolConfig.class.getName().equals(parserContext.getRegistry().getBeanDefinition(value).getBeanClassName()))) {
                                    if ("dubbo:provider".equals(element.getTagName())) {
                                        logger.warn("Recommended replace <dubbo:provider protocol=\"" + value + "\" ... /> to <dubbo:protocol name=\"" + value + "\" ... />");
                                    }
                                    // 兼容旧版本配置
                                    ProtocolConfig protocol = new ProtocolConfig();
                                    protocol.setName(value);
                                    reference = protocol;
                                } else if ("monitor".equals(property)
                                        && (! parserContext.getRegistry().containsBeanDefinition(value)
                                                || ! MonitorConfig.class.getName().equals(parserContext.getRegistry().getBeanDefinition(value).getBeanClassName()))) {
                                    // 兼容旧版本配置
                                    reference = convertMonitor(value);
                                } else if ("onreturn".equals(property)) {
                                    int index = value.lastIndexOf(".");
                                    String returnRef = value.substring(0, index);
                                    String returnMethod = value.substring(index + 1);
                                    reference = new RuntimeBeanReference(returnRef);
                                    beanDefinition.getPropertyValues().addPropertyValue("onreturnMethod", returnMethod);
                                } else if ("onthrow".equals(property)) {
                                    int index = value.lastIndexOf(".");
                                    String throwRef = value.substring(0, index);
                                    String throwMethod = value.substring(index + 1);
                                    reference = new RuntimeBeanReference(throwRef);
                                    beanDefinition.getPropertyValues().addPropertyValue("onthrowMethod", throwMethod);
                                } else {
                                    if ("ref".equals(property) && parserContext.getRegistry().containsBeanDefinition(value)) {
                                        BeanDefinition refBean = parserContext.getRegistry().getBeanDefinition(value);
                                        if (! refBean.isSingleton()) {
                                            throw new IllegalStateException("The exported service ref " + value + " must be singleton! Please set the " + value + " bean scope to singleton, eg: <bean id=\"" + value+ "\" scope=\"singleton\" ...>");
                                        }
                                    }
                                    reference = new RuntimeBeanReference(value);
                                }
                                                        beanDefinition.getPropertyValues().addPropertyValue(property, reference);
                            }
                                }
                    }
                }
            }
        }
        NamedNodeMap attributes = element.getAttributes();
        int len = attributes.getLength();
        for (int i = 0; i < len; i++) {
            Node node = attributes.item(i);
            String name = node.getLocalName();
            if (! props.contains(name)) {
                if (parameters == null) {
                    parameters = new ManagedMap();
                }
                String value = node.getNodeValue();
                parameters.put(name, new TypedStringValue(value, String.class));
            }
        }
        if (parameters != null) {
            beanDefinition.getPropertyValues().addPropertyValue("parameters", parameters);
        }
        return beanDefinition;
    }
 

 

 

springAbstractApplicationContext类调用方法obtainFreshBeanFactory生成所以的beandefinitionregisterListeners方法将注册实现了applicationListener的类,因为ServiceBean类实现了ApplicationListener接口,serviceBean将被预先被实例化。又因为serviceBean实现了initialBean接口,在serviceBean被实例化后,将调用serviceBeanafterPropertiesSet方法。最后将实例化后的serviceBean对象加到添加到applicationListener的类表中。

 

public void refresh() throws BeansException, IllegalStateException {
                                synchronized (this.startupShutdownMonitor) {
                                                // Prepare this context for refreshing.
                                                prepareRefresh();

                                                // Tell the subclass to refresh the internal bean factory.
                                                ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

                                                                // Check for listener beans and register them.
                                                                registerListeners();

                                                                // Instantiate all remaining (non-lazy-init) singletons.
                                                                finishBeanFactoryInitialization(beanFactory);

                                                                // Last step: publish corresponding event.
                                                                finishRefresh();
                }
 

 

 

afterPropertiesSet方法中实例化各个configure对象,如providerConfigure等,并将对象设置到ServiceBean中。

 

public void afterPropertiesSet() throws Exception {
        if (getProvider() == null) {
            Map<String, ProviderConfig> providerConfigMap = applicationContext == null ? null  : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ProviderConfig.class, false, false);
            if (providerConfigMap != null && providerConfigMap.size() > 0) {
                Map<String, ProtocolConfig> protocolConfigMap = applicationContext == null ? null  : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ProtocolConfig.class, false, false);
                if ((protocolConfigMap == null || protocolConfigMap.size() == 0)
                        && providerConfigMap.size() > 1) { // 兼容旧版本
                    List<ProviderConfig> providerConfigs = new ArrayList<ProviderConfig>();
                    for (ProviderConfig config : providerConfigMap.values()) {
                        if (config.isDefault() != null && config.isDefault().booleanValue()) {
                            providerConfigs.add(config);
                        }
                    }
                    if (providerConfigs.size() > 0) {
                        setProviders(providerConfigs);
                    }
                } else {
                    ProviderConfig providerConfig = null;
                    for (ProviderConfig config : providerConfigMap.values()) {
                        if (config.isDefault() == null || config.isDefault().booleanValue()) {
                            if (providerConfig != null) {
                                throw new IllegalStateException("Duplicate provider configs: " + providerConfig + " and " + config);
                            }
                            providerConfig = config;
                        }
                    }
                    if (providerConfig != null) {
                        setProvider(providerConfig);
                    }
                }
            }
        }
        if (getApplication() == null
                && (getProvider() == null || getProvider().getApplication() == null)) {
            Map<String, ApplicationConfig> applicationConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ApplicationConfig.class, false, false);
            if (applicationConfigMap != null && applicationConfigMap.size() > 0) {
                ApplicationConfig applicationConfig = null;
                for (ApplicationConfig config : applicationConfigMap.values()) {
                    if (config.isDefault() == null || config.isDefault().booleanValue()) {
                        if (applicationConfig != null) {
                            throw new IllegalStateException("Duplicate application configs: " + applicationConfig + " and " + config);
                        }
                        applicationConfig = config;
                    }
                }
                if (applicationConfig != null) {
                    setApplication(applicationConfig);
                }
            }
        }
        if (getModule() == null
                && (getProvider() == null || getProvider().getModule() == null)) {
            Map<String, ModuleConfig> moduleConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ModuleConfig.class, false, false);
            if (moduleConfigMap != null && moduleConfigMap.size() > 0) {
                ModuleConfig moduleConfig = null;
                for (ModuleConfig config : moduleConfigMap.values()) {
                    if (config.isDefault() == null || config.isDefault().booleanValue()) {
                        if (moduleConfig != null) {
                            throw new IllegalStateException("Duplicate module configs: " + moduleConfig + " and " + config);
                        }
                        moduleConfig = config;
                    }
                }
                if (moduleConfig != null) {
                    setModule(moduleConfig);
                }
            }
        }
        if ((getRegistries() == null || getRegistries().size() == 0)
                && (getProvider() == null || getProvider().getRegistries() == null || getProvider().getRegistries().size() == 0)
                && (getApplication() == null || getApplication().getRegistries() == null || getApplication().getRegistries().size() == 0)) {
            Map<String, RegistryConfig> registryConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, RegistryConfig.class, false, false);
            if (registryConfigMap != null && registryConfigMap.size() > 0) {
                List<RegistryConfig> registryConfigs = new ArrayList<RegistryConfig>();
                for (RegistryConfig config : registryConfigMap.values()) {
                    if (config.isDefault() == null || config.isDefault().booleanValue()) {
                        registryConfigs.add(config);
                    }
                }
                if (registryConfigs != null && registryConfigs.size() > 0) {
                    super.setRegistries(registryConfigs);
                }
            }
        }
        if (getMonitor() == null
                && (getProvider() == null || getProvider().getMonitor() == null)
                && (getApplication() == null || getApplication().getMonitor() == null)) {
            Map<String, MonitorConfig> monitorConfigMap = applicationContext == null ? null : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, MonitorConfig.class, false, false);
            if (monitorConfigMap != null && monitorConfigMap.size() > 0) {
                MonitorConfig monitorConfig = null;
                for (MonitorConfig config : monitorConfigMap.values()) {
                    if (config.isDefault() == null || config.isDefault().booleanValue()) {
                        if (monitorConfig != null) {
                            throw new IllegalStateException("Duplicate monitor configs: " + monitorConfig + " and " + config);
                        }
                        monitorConfig = config;
                    }
                }
                if (monitorConfig != null) {
                    setMonitor(monitorConfig);
                }
            }
        }
        if ((getProtocols() == null || getProtocols().size() == 0)
                && (getProvider() == null || getProvider().getProtocols() == null || getProvider().getProtocols().size() == 0)) {
            Map<String, ProtocolConfig> protocolConfigMap = applicationContext == null ? null  : BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ProtocolConfig.class, false, false);
            if (protocolConfigMap != null && protocolConfigMap.size() > 0) {
                List<ProtocolConfig> protocolConfigs = new ArrayList<ProtocolConfig>();
                for (ProtocolConfig config : protocolConfigMap.values()) {
                    if (config.isDefault() == null || config.isDefault().booleanValue()) {
                        protocolConfigs.add(config);
                    }
                }
                if (protocolConfigs != null && protocolConfigs.size() > 0) {
                    super.setProtocols(protocolConfigs);
                }
            }
        }
        if (getPath() == null || getPath().length() == 0) {
            if (beanName != null && beanName.length() > 0
                    && getInterface() != null && getInterface().length() > 0
                    && beanName.startsWith(getInterface())) {
                setPath(beanName);
            }
        }
        if (! isDelay()) {
            export();
        }
    }
 

 

 

AbstractApplicationContext 类调用finishRefresh方法后,将publish 一个ContextRefreshedEvent的通知。因为serviceBean注册了applicationListener,那么serviceBean将接收到contextRefreshedEvent的通知。将调用ServiceBeanonApplicationEvent方法。最后调用export方法暴露接口。

 

protected synchronized void doExport() {
       //检查各种配置
        checkApplication();
        checkRegistry();
        checkProtocol();
        appendProperties(this);
        checkStubAndMock(interfaceClass);
        if (path == null || path.length() == 0) {
            path = interfaceName;
        }
        //暴露
        doExportUrls();
    }

 

private void doExportUrls() {
        List<URL> registryURLs = loadRegistries(true);
        for (ProtocolConfig protocolConfig : protocols) {
            //export
            doExportUrlsFor1Protocol(protocolConfig, registryURLs);
        }
    }
 

 

 

 

 

 

 

 

<think>嗯,用户的问题是关于整合dubbo-spring-boot-starter和jasypt-spring-boot-starter时无法正确读取明文的问题。我需要先理解这两个库的作用以及它们为什么会冲突。 首先,dubbo-spring-boot-starter是Dubbo与Spring Boot集成的启动器,用于简化分布式服务的开发。而jasypt-spring-boot-starter则是用来加密配置文件中的敏感信息,比如数据库密码,它在应用启动时自动解密这些加密的值。 用户遇到的问题可能是当两者一起使用时,Dubbo的配置项(如注册中心地址)中的加密值没有被正确解密,导致连接失败。这可能是因为这两个库的Bean加载顺序有问题,或者Jasypt的解密处理器没有在Dubbo配置加载之前生效。 接下来,我需要回忆Spring Boot的启动流程,尤其是Bean的加载顺序和属性解析的机制。Spring Boot在启动时会先加载application.properties或application.yml中的配置,然后创建相应的Bean。如果Dubbo的配置在加载时,Jasypt的解密处理器还没有准备好,那么加密的配置项就无法被解密,导致Dubbo使用原始的加密字符串去连接注册中心,自然会失败。 可能的解决方案包括: 1. 确保Jasypt的配置正确,比如加密前缀和后缀是否正确,密钥是否设置正确。 2. 检查配置文件的加载顺序,确保Jasypt的解密处理器在Dubbo配置加载之前已经初始化。 3. 使用@DependsOn注解强制Dubbo的Bean依赖于Jasypt的解密处理器,确保解密处理器先被加载。 4. 在Dubbo的配置中使用明文占位符,如${jasypt.decrypt(加密字符串)},但需要确认Jasypt是否支持这种写法,或者是否需要自定义解析器。 5. 手动解密Dubbo的配置项,例如在@Configuration类中,通过Environment对象获取加密的配置值,然后手动解密后再设置到Dubbo的配置Bean中。 需要验证这些方法的可行性。比如,使用@DependsOn可能有效,因为可以控制Bean的初始化顺序。而手动解密可能需要编写额外的代码,但更灵活。 另外,参考用户提供的引用内容,特别是引用[3]中的@EnableDubbo注解,可能需要确保在启用Dubbo之前,Jasypt已经完成了配置的解密。或者检查是否有属性源(PropertySource)的顺序问题,导致Dubbo的配置没有被正确覆盖或解密。 还要考虑Jasypt的版本是否与Spring Boot版本兼容,是否有已知的冲突问题。有时候升级其中一个依赖可以解决冲突。 总结可能的解决步骤: 1. 检查Jasypt配置是否正确,包括密钥和加密格式。 2. 调整Bean的加载顺序,确保Jasypt先于Dubbo加载。 3. 使用@DependsOn注解。 4. 手动解密配置项。 5. 检查依赖版本兼容性。 现在需要将这些步骤以清晰的方式呈现给用户,并给出具体的代码示例和配置示例,同时生成相关问题,帮助用户进一步排查可能的问题。</think>### 整合dubbo-spring-boot-starter与jasypt-spring-boot-starter的明文读取问题解决方案 #### 问题原因分析 1. **加载顺序冲突** Jasypt的解密处理器可能未在Dubbo配置加载前初始化,导致加密值未被解密。例如,Dubbo的注册中心地址配置项(如`dubbo.registry.address=ENC(加密字符串)`)可能直接读取到加密字符串[^2]。 2. **属性解析机制差异** Jasypt通过`@EnableEncryptableProperties`动态解密配置,而Dubbo可能提前读取了未解密的原始值[^3]。 --- #### 具体解决步骤 ##### 1. **验证Jasypt基础配置** 确保Jasypt的加密前缀/后缀和密钥正确: ```yaml jasypt: encryptor: password: your_secret_key # 需通过环境变量或启动参数传递 algorithm: PBEWithMD5AndDES iv-generator-classname: org.jasypt.iv.NoIvGenerator ``` ##### 2. **调整Bean加载顺序** 在Dubbo配置类中添加`@DependsOn`强制依赖Jasypt解密处理器: ```java @Configuration @DependsOn("encryptablePropertySourceProcessor") // 确保Jasypt先初始化 public class DubboConfig { // Dubbo的@Bean定义 } ``` ##### 3. **手动解密Dubbo配置项** 在自定义配置类中主动解密加密值: ```java @Configuration public class CustomDubboConfig { @Autowired private Environment env; @Bean public RegistryConfig registryConfig() { String encryptedAddress = env.getProperty("dubbo.registry.address"); String decryptedAddress = decrypt(encryptedAddress); // 调用Jasypt解密方法 RegistryConfig registryConfig = new RegistryConfig(); registryConfig.setAddress(decryptedAddress); return registryConfig; } } ``` ##### 4. **检查属性源覆盖顺序** 确保Jasypt的属性源优先级高于Dubbo配置: ```properties # 在application.properties中显式指定顺序 spring.config.location=classpath:/application.yml ``` --- #### 验证流程 1. 启动时添加Jasypt密钥参数: `java -jar app.jar -Djasypt.encryptor.password=your_key` 2. 检查Dubbo日志输出: ```log [DUBBO] RegistryConfig using address: nacos://127.0.0.1:8848 # 确认是解密后的明文 ``` --- #### 配置示例 ```yaml # application.yml dubbo: registry: address: ENC(加密字符串) # Jasypt加密后的值 protocol: name: dubbo port: 20880 ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值