Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.7版本与spring cloud euerka bean创建冲突 #449

Closed
bruce-qin opened this issue Feb 18, 2019 · 10 comments
Closed

2.7版本与spring cloud euerka bean创建冲突 #449

bruce-qin opened this issue Feb 18, 2019 · 10 comments
Assignees
Milestone

Comments

@bruce-qin
Copy link

错误日志:
Field propertyResolver in org.springframework.cloud.netflix.eureka.EurekaClientConfigBean required a single bean, but 2 were found:
- dubboScanBasePackagesPropertyResolver: defined by method 'dubboScanBasePackagesPropertyResolver' in class path resource [org/apache/dubbo/spring/boot/autoconfigure/DubboRelaxedBinding2AutoConfiguration.class]
- environment: a programmatically registered singleton

org.apache.dubbo.spring.boot.autoconfigure.DubboRelaxedBinding2AutoConfiguration

public class DubboRelaxedBinding2AutoConfiguration {
    @Bean(name = BASE_PACKAGES_PROPERTY_RESOLVER_BEAN_NAME)
    public PropertyResolver dubboScanBasePackagesPropertyResolver(ConfigurableEnvironment environment) {
        ConfigurableEnvironment propertyResolver = new AbstractEnvironment() {
            protected void customizePropertySources(MutablePropertySources propertySources) {
                Map<String, Object> dubboScanProperties = PropertySourcesUtils.getSubProperties(environment, DUBBO_SCAN_PREFIX);
                propertySources.addLast(new MapPropertySource("dubboScanProperties", dubboScanProperties));
            }
        };
        ConfigurationPropertySources.attach(propertyResolver);
        return propertyResolver;
    }

org.springframework.cloud.netflix.eureka.EurekaClientConfigBean

public class EurekaClientConfigBean implements EurekaClientConfig, Ordered {
        @Autowired(required = false)
	PropertyResolver propertyResolver;
@ynjqqqnt
Copy link

与archaius冲突
Field env in org.springframework.cloud.netflix.archaius.ArchaiusAutoConfiguration$PropagateEventsConfiguration required a single bean, but 2 were found:
- dubboScanBasePackagesPropertyResolver: defined by method 'dubboScanBasePackagesPropertyResolver' in class path resource [org/apache/dubbo/spring/boot/autoconfigure/DubboRelaxedBinding2AutoConfiguration.class]
- environment: a programmatically registered singleton

@mercyblitz mercyblitz self-assigned this Feb 21, 2019
@mercyblitz mercyblitz added this to the 2.7.1 milestone Feb 21, 2019
@mercyblitz
Copy link
Contributor

Thanks, it's a bug and be resolved in 2.7.1

@teaho2015
Copy link

@mercyblitz 您好,我的项目中,需要对接别人的feign接口sdk,同时我自己项目提供dubbo接口,但是在引入别人的feign sdk时,启动却报了类似的错误,

  • ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.core.env.Environment' available: expected single matching bean but found 2: dubboScanBasePackagesPropertyResolver,environment

版本:
org.apache.dubbo:dubbo-spring-boot-starter:2.7.0
org.springframework.cloud:spring-cloud-starter-openfeign:jar:2.0.2.RELEASE

请问是同一个bug吗,还是说我的配置问题

@luger1990
Copy link

@mercyblitz 您好,我的项目中,需要对接别人的feign接口sdk,同时我自己项目提供dubbo接口,但是在引入别人的feign sdk时,启动却报了类似的错误,

  • ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.core.env.Environment' available: expected single matching bean but found 2: dubboScanBasePackagesPropertyResolver,environment

版本:
org.apache.dubbo:dubbo-spring-boot-starter:2.7.0
org.springframework.cloud:spring-cloud-starter-openfeign:jar:2.0.2.RELEASE

请问是同一个bug吗,还是说我的配置问题

我也遇到这个问题了

@mercyblitz
Copy link
Contributor

mercyblitz commented Feb 25, 2019

If your Spring Boot/Cloud 2 applications met with this issue , like below:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field env in org.springframework.cloud.netflix.archaius.ArchaiusAutoConfiguration$PropagateEventsConfiguration required a single bean, but 2 were found:
	- dubboScanBasePackagesPropertyResolver: defined by method 'dubboScanBasePackagesPropertyResolver' in class path resource [org/apache/dubbo/spring/boot/autoconfigure/DubboRelaxedBinding2AutoConfiguration.class]
	- environment: a programmatically registered singleton

Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

Please add these code for your application to resolve this issue:

  • Add "spring.main.allow-bean-definition-overriding" into application.properties
spring.main.allow-bean-definition-overriding = true
  • Re-define the bean named dubboScanBasePackagesPropertyResolver:
    // Bugfix code  to resolve Environment Beans conflict 
    @Primary
    @Bean(name = BASE_PACKAGES_PROPERTY_RESOLVER_BEAN_NAME)
    public PropertyResolver dubboScanBasePackagesPropertyResolver(ConfigurableEnvironment environment) {
        ConfigurableEnvironment propertyResolver = new AbstractEnvironment() {
            protected void customizePropertySources(MutablePropertySources propertySources) {
                Map<String, Object> dubboScanProperties = PropertySourcesUtils.getSubProperties(environment, DUBBO_SCAN_PREFIX);
                propertySources.addLast(new MapPropertySource("dubboScanProperties", dubboScanProperties));
            }
        };
        ConfigurationPropertySources.attach(propertyResolver);
        return new DelegatingPropertyResolver(propertyResolver);
    }


    private static class DelegatingPropertyResolver implements PropertyResolver {

        private final PropertyResolver delegate;

        DelegatingPropertyResolver(PropertyResolver delegate) {
            Assert.notNull(delegate, "The delegate of PropertyResolver must not be null");
            this.delegate = delegate;
        }

        @Override
        public boolean containsProperty(String key) {
            return delegate.containsProperty(key);
        }

        @Override
        @Nullable
        public String getProperty(String key) {
            return delegate.getProperty(key);
        }

        @Override
        public String getProperty(String key, String defaultValue) {
            return delegate.getProperty(key, defaultValue);
        }

        @Override
        @Nullable
        public <T> T getProperty(String key, Class<T> targetType) {
            return delegate.getProperty(key, targetType);
        }

        @Override
        public <T> T getProperty(String key, Class<T> targetType, T defaultValue) {
            return delegate.getProperty(key, targetType, defaultValue);
        }

        @Override
        public String getRequiredProperty(String key) throws IllegalStateException {
            return delegate.getRequiredProperty(key);
        }

        @Override
        public <T> T getRequiredProperty(String key, Class<T> targetType) throws IllegalStateException {
            return delegate.getRequiredProperty(key, targetType);
        }

        @Override
        public String resolvePlaceholders(String text) {
            return delegate.resolvePlaceholders(text);
        }

        @Override
        public String resolveRequiredPlaceholders(String text) throws IllegalArgumentException {
            return delegate.resolveRequiredPlaceholders(text);
        }
    }

@sohu234
Copy link

sohu234 commented Mar 1, 2019

to resolve this problem ,i add @primary annotation on dubboScanBasePackagesPropertyResolver() method, eg:
@bean(name = BASE_PACKAGES_PROPERTY_RESOLVER_BEAN_NAME)
@primary
public PropertyResolver dubboScanBasePackagesPropertyResolver(ConfigurableEnvironment environment) {
ConfigurableEnvironment propertyResolver = new AbstractEnvironment() {
protected void customizePropertySources(MutablePropertySources propertySources) {
Map<String, Object> dubboScanProperties = PropertySourcesUtils.getSubProperties(environment, DUBBO_SCAN_PREFIX);
propertySources.addLast(new MapPropertySource("dubboScanProperties", dubboScanProperties));
}
};
ConfigurationPropertySources.attach(propertyResolver);
return propertyResolver;
}

@mercyblitz
Copy link
Contributor

mercyblitz commented Mar 6, 2019

@sohu234 Thanks for your code, I have updated my bugfix code.

@thewkgithub
Copy link

各位好.我使用上述的两种方式.还是有如何的错误提示,现在不知如何是好?

dubbo版本2.7.0
springboot版本2.1.3
springcloud版本Greenwich.SR1

@mercyblitz
Copy link
Contributor

各位好.我使用上述的两种方式.还是有如何的错误提示,现在不知如何是好?

dubbo版本2.7.0
springboot版本2.1.3
springcloud版本Greenwich.SR1

Please attach your error messages, it works in my side.

@totzcc
Copy link

totzcc commented Mar 26, 2019

@mercyblitz

According to the above method, I can't solve my problem.

@primary is not woking

I debug at @primary function, That not entry my debug point

@SpringBootApplication
@EnableDiscoveryClient
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
dubbo:
  registry:
    address: zk1.test1.akucun.com,zk2.test1.akucun.com,zk3.test1.akucun.com
    port: 2181
    protocol: zookeeper
  application:
    name: ddc-datacenter-dubbo
server:
  port: 8085
eureka:
  client:
    proxy-user-name: merchant
    proxy-password: 123456
    service-url:
      defaultZone: http://${eureka.client.proxy-user-name}:${eureka.client.proxy-password}@172.19.1.233:8080/eureka/
spring:
  application:
    name: ddc-datecenter-dubbo
  main:
    allow-bean-definition-overriding: true
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>2.1.1.RELEASE</version>
</dependency>

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>


<!-- Dubbo Spring Boot Starter -->
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>2.7.0</version>
</dependency>
<!-- Apache Dubbo  -->
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo</artifactId>
    <version>${dubbo.version}</version>
    <exclusions>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
        </exclusion>
        <exclusion>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
        </exclusion>
        <exclusion>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </exclusion>
    </exclusions>
</dependency>
***************************
APPLICATION FAILED TO START
***************************

Description:

Field propertyResolver in org.springframework.cloud.netflix.eureka.EurekaClientConfigBean required a single bean, but 2 were found:
	- dubboScanBasePackagesPropertyResolver: defined by method 'dubboScanBasePackagesPropertyResolver' in class path resource [org/apache/dubbo/spring/boot/autoconfigure/DubboRelaxedBinding2AutoConfiguration.class]
	- environment: a programmatically registered singleton

Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

Disconnected from the target VM, address: '127.0.0.1:64527', transport: 'socket'

Process finished with exit code 1

This is my config

@Configuration
public class ConfigBean {
    // Bugfix code  to resolve Environment Beans conflict
    @Primary
    @Bean(name = BASE_PACKAGES_PROPERTY_RESOLVER_BEAN_NAME)
    public PropertyResolver dubboScanBasePackagesPropertyResolver(ConfigurableEnvironment environment) {
        ConfigurableEnvironment propertyResolver = new AbstractEnvironment() {
            protected void customizePropertySources(MutablePropertySources propertySources) {
                Map<String, Object> dubboScanProperties = PropertySourcesUtils.getSubProperties(environment, DUBBO_SCAN_PREFIX);
                propertySources.addLast(new MapPropertySource("dubboScanProperties", dubboScanProperties));
            }
        };
        ConfigurationPropertySources.attach(propertyResolver);
        return new DelegatingPropertyResolver(propertyResolver);
    }


    private static class DelegatingPropertyResolver implements PropertyResolver {

        private final PropertyResolver delegate;

        DelegatingPropertyResolver(PropertyResolver delegate) {
            Assert.notNull(delegate, "The delegate of PropertyResolver must not be null");
            this.delegate = delegate;
        }

        @Override
        public boolean containsProperty(String key) {
            return delegate.containsProperty(key);
        }

        @Override
        @Nullable
        public String getProperty(String key) {
            return delegate.getProperty(key);
        }

        @Override
        public String getProperty(String key, String defaultValue) {
            return delegate.getProperty(key, defaultValue);
        }

        @Override
        @Nullable
        public <T> T getProperty(String key, Class<T> targetType) {
            return delegate.getProperty(key, targetType);
        }

        @Override
        public <T> T getProperty(String key, Class<T> targetType, T defaultValue) {
            return delegate.getProperty(key, targetType, defaultValue);
        }

        @Override
        public String getRequiredProperty(String key) throws IllegalStateException {
            return delegate.getRequiredProperty(key);
        }

        @Override
        public <T> T getRequiredProperty(String key, Class<T> targetType) throws IllegalStateException {
            return delegate.getRequiredProperty(key, targetType);
        }

        @Override
        public String resolvePlaceholders(String text) {
            return delegate.resolvePlaceholders(text);
        }

        @Override
        public String resolveRequiredPlaceholders(String text) throws IllegalArgumentException {
            return delegate.resolveRequiredPlaceholders(text);
        }
    }
}

@mercyblitz mercyblitz unpinned this issue Apr 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants