Spring cloud alibaba dubbo + nacos 整合报错 Error creating bean with name ‘feignTargeter‘解决办法

完整报错信息如下

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘feignTargeter’ defined in class path resource [org/springframework/cloud/openfeign/FeignAutoConfiguration D e f a u l t F e i g n T a r g e t e r C o n f i g u r a t i o n . c l a s s ] : I n i t i a l i z a t i o n o f b e a n f a i l e d ; n e s t e d e x c e p t i o n i s j a v a . l a n g . I l l e g a l A c c e s s E r r o r : c l a s s o r g . s p r i n g f r a m e w o r k . c l o u d . o p e n f e i g n . DefaultFeignTargeterConfiguration.class]: Initialization of bean failed; nested exception is java.lang.IllegalAccessError: class org.springframework.cloud.openfeign. DefaultFeignTargeterConfiguration.class]:Initializationofbeanfailed;nestedexceptionisjava.lang.IllegalAccessError:classorg.springframework.cloud.openfeign.Proxy79 cannot access its superinterface org.springframework.cloud.openfeign.Targeter
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean 0 ( A b s t r a c t B e a n F a c t o r y . j a v a : 324 )   [ s p r i n g − b e a n s − 5.2.12. R E L E A S E . j a r : 5.2.12. R E L E A S E ] a t o r g . s p r i n g f r a m e w o r k . b e a n s . f a c t o r y . s u p p o r t . D e f a u l t S i n g l e t o n B e a n R e g i s t r y . g e t S i n g l e t o n ( D e f a u l t S i n g l e t o n B e a n R e g i s t r y . j a v a : 234 )   [ s p r i n g − b e a n s − 5.2.12. R E L E A S E . j a r : 5.2.12. R E L E A S E ] a t o r g . s p r i n g f r a m e w o r k . b e a n s . f a c t o r y . s u p p o r t . A b s t r a c t B e a n F a c t o r y . d o G e t B e a n ( A b s t r a c t B e a n F a c t o r y . j a v a : 322 )   [ s p r i n g − b e a n s − 5.2.12. R E L E A S E . j a r : 5.2.12. R E L E A S E ] a t o r g . s p r i n g f r a m e w o r k . b e a n s . f a c t o r y . s u p p o r t . A b s t r a c t B e a n F a c t o r y . g e t B e a n ( A b s t r a c t B e a n F a c t o r y . j a v a : 202 )   [ s p r i n g − b e a n s − 5.2.12. R E L E A S E . j a r : 5.2.12. R E L E A S E ] a t o r g . s p r i n g f r a m e w o r k . b e a n s . f a c t o r y . s u p p o r t . D e f a u l t L i s t a b l e B e a n F a c t o r y . p r e I n s t a n t i a t e S i n g l e t o n s ( D e f a u l t L i s t a b l e B e a n F a c t o r y . j a v a : 897 )   [ s p r i n g − b e a n s − 5.2.12. R E L E A S E . j a r : 5.2.12. R E L E A S E ] a t o r g . s p r i n g f r a m e w o r k . c o n t e x t . s u p p o r t . A b s t r a c t A p p l i c a t i o n C o n t e x t . f i n i s h B e a n F a c t o r y I n i t i a l i z a t i o n ( A b s t r a c t A p p l i c a t i o n C o n t e x t . j a v a : 879 )   [ s p r i n g − c o n t e x t − 5.2.12. R E L E A S E . j a r : 5.2.12. R E L E A S E ] a t o r g . s p r i n g f r a m e w o r k . c o n t e x t . s u p p o r t . A b s t r a c t A p p l i c a t i o n C o n t e x t . r e f r e s h ( A b s t r a c t A p p l i c a t i o n C o n t e x t . j a v a : 551 )   [ s p r i n g − c o n t e x t − 5.2.12. R E L E A S E . j a r : 5.2.12. R E L E A S E ] a t o r g . s p r i n g f r a m e w o r k . b o o t . S p r i n g A p p l i c a t i o n . r e f r e s h ( S p r i n g A p p l i c a t i o n . j a v a : 758 ) [ s p r i n g − b o o t − 2.3.7. R E L E A S E . j a r : 2.3.7. R E L E A S E ] a t o r g . s p r i n g f r a m e w o r k . b o o t . S p r i n g A p p l i c a t i o n . r e f r e s h ( S p r i n g A p p l i c a t i o n . j a v a : 750 ) [ s p r i n g − b o o t − 2.3.7. R E L E A S E . j a r : 2.3.7. R E L E A S E ] a t o r g . s p r i n g f r a m e w o r k . b o o t . S p r i n g A p p l i c a t i o n . r e f r e s h C o n t e x t ( S p r i n g A p p l i c a t i o n . j a v a : 405 ) [ s p r i n g − b o o t − 2.3.7. R E L E A S E . j a r : 2.3.7. R E L E A S E ] a t o r g . s p r i n g f r a m e w o r k . b o o t . S p r i n g A p p l i c a t i o n . r u n ( S p r i n g A p p l i c a t i o n . j a v a : 315 ) [ s p r i n g − b o o t − 2.3.7. R E L E A S E . j a r : 2.3.7. R E L E A S E ] a t o r g . s p r i n g f r a m e w o r k . b o o t . S p r i n g A p p l i c a t i o n . r u n ( S p r i n g A p p l i c a t i o n . j a v a : 1237 ) [ s p r i n g − b o o t − 2.3.7. R E L E A S E . j a r : 2.3.7. R E L E A S E ] a t o r g . s p r i n g f r a m e w o r k . b o o t . S p r i n g A p p l i c a t i o n . r u n ( S p r i n g A p p l i c a t i o n . j a v a : 1226 ) [ s p r i n g − b o o t − 2.3.7. R E L E A S E . j a r : 2.3.7. R E L E A S E ] a t c o m . e x a m p l e . d u b b o p r o v i d e r . D u b b o P r o v i d e r A p p l i c a t i o n . m a i n ( D u b b o P r o v i d e r A p p l i c a t i o n . j a v a : 12 ) [ c l a s s e s / : n a ] a t s u n . r e f l e c t . N a t i v e M e t h o d A c c e s s o r I m p l . i n v o k e 0 ( N a t i v e M e t h o d )   [ n a : 1.8. 0 1 61 ] a t s u n . r e f l e c t . N a t i v e M e t h o d A c c e s s o r I m p l . i n v o k e ( N a t i v e M e t h o d A c c e s s o r I m p l . j a v a : 62 )   [ n a : 1.8. 0 1 61 ] a t s u n . r e f l e c t . D e l e g a t i n g M e t h o d A c c e s s o r I m p l . i n v o k e ( D e l e g a t i n g M e t h o d A c c e s s o r I m p l . j a v a : 43 )   [ n a : 1.8. 0 1 61 ] a t j a v a . l a n g . r e f l e c t . M e t h o d . i n v o k e ( M e t h o d . j a v a : 498 )   [ n a : 1.8. 0 1 61 ] a t o r g . s p r i n g f r a m e w o r k . b o o t . d e v t o o l s . r e s t a r t . R e s t a r t L a u n c h e r . r u n ( R e s t a r t L a u n c h e r . j a v a : 49 ) [ s p r i n g − b o o t − d e v t o o l s − 2.3.7. R E L E A S E . j a r : 2.3.7. R E L E A S E ] C a u s e d b y : j a v a . l a n g . I l l e g a l A c c e s s E r r o r : c l a s s o r g . s p r i n g f r a m e w o r k . c l o u d . o p e n f e i g n . 0(AbstractBeanFactory.java:324) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879) ~[spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.12.RELEASE.jar:5.2.12.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) [spring-boot-2.3.7.RELEASE.jar:2.3.7.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.3.7.RELEASE.jar:2.3.7.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:405) [spring-boot-2.3.7.RELEASE.jar:2.3.7.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.3.7.RELEASE.jar:2.3.7.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.7.RELEASE.jar:2.3.7.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.7.RELEASE.jar:2.3.7.RELEASE] at com.example.dubboprovider.DubboProviderApplication.main(DubboProviderApplication.java:12) [classes/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_161] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_161] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_161] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_161] at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.3.7.RELEASE.jar:2.3.7.RELEASE] Caused by: java.lang.IllegalAccessError: class org.springframework.cloud.openfeign. 0(AbstractBeanFactory.java:324) [springbeans5.2.12.RELEASE.jar:5.2.12.RELEASE]atorg.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) [springbeans5.2.12.RELEASE.jar:5.2.12.RELEASE]atorg.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) [springbeans5.2.12.RELEASE.jar:5.2.12.RELEASE]atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) [springbeans5.2.12.RELEASE.jar:5.2.12.RELEASE]atorg.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897) [springbeans5.2.12.RELEASE.jar:5.2.12.RELEASE]atorg.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879) [springcontext5.2.12.RELEASE.jar:5.2.12.RELEASE]atorg.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) [springcontext5.2.12.RELEASE.jar:5.2.12.RELEASE]atorg.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)[springboot2.3.7.RELEASE.jar:2.3.7.RELEASE]atorg.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)[springboot2.3.7.RELEASE.jar:2.3.7.RELEASE]atorg.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:405)[springboot2.3.7.RELEASE.jar:2.3.7.RELEASE]atorg.springframework.boot.SpringApplication.run(SpringApplication.java:315)[springboot2.3.7.RELEASE.jar:2.3.7.RELEASE]atorg.springframework.boot.SpringApplication.run(SpringApplication.java:1237)[springboot2.3.7.RELEASE.jar:2.3.7.RELEASE]atorg.springframework.boot.SpringApplication.run(SpringApplication.java:1226)[springboot2.3.7.RELEASE.jar:2.3.7.RELEASE]atcom.example.dubboprovider.DubboProviderApplication.main(DubboProviderApplication.java:12)[classes/:na]atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod) [na:1.8.0161]atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) [na:1.8.0161]atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [na:1.8.0161]atjava.lang.reflect.Method.invoke(Method.java:498) [na:1.8.0161]atorg.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)[springbootdevtools2.3.7.RELEASE.jar:2.3.7.RELEASE]Causedby:java.lang.IllegalAccessError:classorg.springframework.cloud.openfeign.Proxy79 cannot access its superinterface org.springframework.cloud.openfeign.Targeter
at java.lang.reflect.Proxy.defineClass0(Native Method) ~[na:1.8.0_161]
at java.lang.reflect.Proxy.access 300 ( P r o x y . j a v a : 228 )   [ n a : 1.8. 0 1 61 ] a t j a v a . l a n g . r e f l e c t . P r o x y 300(Proxy.java:228) ~[na:1.8.0_161] at java.lang.reflect.Proxy 300(Proxy.java:228) [na:1.8.0161]atjava.lang.reflect.ProxyProxyClassFactory.apply(Proxy.java:642) ~[na:1.8.0_161]
at java.lang.reflect.Proxy P r o x y C l a s s F a c t o r y . a p p l y ( P r o x y . j a v a : 557 )   [ n a : 1.8. 0 1 61 ] a t j a v a . l a n g . r e f l e c t . W e a k C a c h e ProxyClassFactory.apply(Proxy.java:557) ~[na:1.8.0_161] at java.lang.reflect.WeakCache ProxyClassFactory.apply(Proxy.java:557) [na:1.8.0161]atjava.lang.reflect.WeakCacheFactory.get(WeakCache.java:230) ~[na:1.8.0_161]
at java.lang.reflect.WeakCache.get(WeakCache.java:127) ~[na:1.8.0_161]
at java.lang.reflect.Proxy.getProxyClass0(Proxy.java:419) ~[na:1.8.0_161]
at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:719) ~[na:1.8.0_161]
at com.alibaba.cloud.dubbo.openfeign.TargeterBeanPostProcessor.postProcessAfterInitialization(TargeterBeanPostProcessor.java:75) ~[spring-cloud-starter-dubbo-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:430) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1798) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.12.RELEASE.jar:5.2.12.RELEASE]
… 20 common frames omitted

解决办法: 去掉springboot devtools热启动包,依赖冲突导致的。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
    <optional>true</optional>
</dependency>

版本冲突和依赖冲突往往折磨人

如果要保留热启动包则拦截bean修改TargeterBeanPostProcessor.postProcessAfterInitialization方法

public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException {
        if (ClassUtils.isPresent("org.springframework.cloud.openfeign.Targeter", this.classLoader)) {
            Class<?> beanClass = ClassUtils.getUserClass(bean.getClass());
            Class<?> targetClass = ClassUtils.resolveClassName("org.springframework.cloud.openfeign.Targeter", this.classLoader);
            if (targetClass.isAssignableFrom(beanClass)) {
                return Proxy.newProxyInstance(beanClass.getClassLoader(), new Class[]{targetClass}, new TargeterInvocationHandler(bean, this.environment, beanClass.getClassLoader(), this.dubboServiceMetadataRepository, this.dubboGenericServiceFactory, this.contextFactory));
            }
        }
 
        return bean;
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
下面是Spring Cloud Alibaba + Spring Boot + Dubbo + Nacos + Mybatis Plus + MySQL 项目搭建步骤: 1. 创建一个Spring Boot项目,并添加以下依赖: ```xml <!--Spring Boot依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--Dubbo依赖--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> <version>2.2.1.RELEASE</version> </dependency> <!--Nacos依赖--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.2.1.RELEASE</version> </dependency> <!--Mybatis Plus依赖--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3</version> </dependency> <!--MySQL依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> ``` 2. 配置application.yml文件,添加以下内容: ```yaml spring: application: name: service-provider # 服务名称 datasource: url: jdbc:mysql://localhost:3306/db_example # 数据库URL username: root # 数据库用户名 password: root # 数据库密码 driver-class-name: com.mysql.cj.jdbc.Driver # 数据库驱动 mybatis-plus: mapper-locations: classpath:mapper/*.xml # Mybatis Plus的mapper文件路径 dubbo: application: name: dubbo-service-provider # Dubbo应用名称 registry: address: nacos://localhost:8848 # Nacos注册中心地址 protocol: name: dubbo # Dubbo协议名称 port: 20880 # Dubbo协议端口号 ``` 3. 创建一个数据模型类,例如: ```java @Data public class User { private Long id; private String username; private String password; private String email; } ``` 4. 创建一个Mapper类,例如: ```java @Mapper public interface UserMapper extends BaseMapper<User> { } ``` 5. 创建一个Service类,例如: ```java @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { } ``` 6. 创建一个Controller类,例如: ```java @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @GetMapping("/{id}") public User getUser(@PathVariable Long id) { return userService.getById(id); } } ``` 7. 在Nacos控制台中添加服务提供者的配置信息。 8. 启动服务提供者,并在Nacos控制台中查看服务是否注册成功。 9. 创建一个服务消费者项目,并添加以下依赖: ```xml <!--Spring Boot依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--Dubbo依赖--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-dubbo</artifactId> <version>2.2.1.RELEASE</version> </dependency> <!--Nacos依赖--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.2.1.RELEASE</version> </dependency> ``` 10. 配置application.yml文件,添加以下内容: ```yaml spring: application: name: service-consumer # 服务名称 dubbo: application: name: dubbo-service-consumer # Dubbo应用名称 registry: address: nacos://localhost:8848 # Nacos注册中心地址 ``` 11. 创建一个Service类,例如: ```java @Service public class UserServiceImpl implements UserService { @Reference private UserService userService; @Override public User getUser(Long id) { return userService.getById(id); } } ``` 12. 创建一个Controller类,例如: ```java @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @GetMapping("/{id}") public User getUser(@PathVariable Long id) { return userService.getUser(id); } } ``` 13. 启动服务消费者,访问http://localhost:8080/user/{id},查看服务是否调用成功。 以上就是Spring Cloud Alibaba + Spring Boot + Dubbo + Nacos + Mybatis Plus + MySQL项目搭建步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HumoChen99

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值