前言
近期自己新开了一套SpringCloud Alibaba微服务项目,接口使用了对外HTTP,内部RPC的设计,具体点说就是外部用户或客户端通过Nginx访问到Gateway网关再分发到各个服务,内部各个服务之间统一使用Dubbo RPC进行通信。下面是Springboot3.x集成Dubbo的分享:
1. 需要的关键依赖
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
<version>3.1.6</version>
</dependency>
<!--微服务组件 start-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2022.0.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2022.0.0.0-RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
2. 启动程序入口注解
2.1. 生产者
@EnableDubbo
@EnableAsync
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
2.2 消费者
@EnableDubbo
@EnableAsync
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class AdminApplication {
public static void main(String[] args) {
SpringApplication.run(AdminApplication.class, args);
}
}
2.3 dubbo配置
dubbo:
application:
id: ${spring.application.name}
name: ${dubbo.application.id}
registry:
address: nacos://127.0.0.1:8848?username=nacos&password=houcloud
group: dubbo
metadata-report:
address: nacos://127.0.0.1:8848?username=nacos&password=houcloud
protocol:
name: dubbo
port: -1
config-center:
timeout: 8000
provider:
threads: 20000
threadpool: fixed
loadbalance: roundrobin
timeout: 5000
executes:
filter: customExceptionFilter,-exception
scan:
base-packages: com.houcloud.application.admin.rpc
consumer:
check: false
3. Dubbo 公共 interface
public interface RpcUserService {
// 删除用户令牌
Boolean removeUserToken(Long userId)
}
4. 提供者UserApplication实现Dubbo 公共 interface
@DubboService(interfaceClass = RpcUserService.class)
public class RpcUserServiceImpl implements RpcUserService {
@Resource
private UserRedisTokenStore userRedisTokenStore;
@Override
public Boolean removeUserToken(Long userId){
return userRedisTokenStore.removeByUserId(userId);
}
}
5. 消费者AdminApplication调用Dubbo 公共 interface
@DubboReference
private RpcUserService rpcUserService;
public Boolean lockUser(Long userId) {
boolean update = userService.lambdaUpdate().eq(User::getId, userId).set(User::getLocked, true).update();
if(!update){
return false
}
return rpcUserService.removeUserToken(userId);
}
启动报错
在集成Dubbo的过程中出现了java.lang.NoClassDefFoundError: org/springframework/beans/factory/config/InstantiationAwareBeanPostProcessorAdapter
异常
第一反应是以为缺少jar包,补了好几个相关dubbo和nacos的包也无济于事
91 [main] ERROR [org.springframework.boot.SpringApplication] SpringApplication.java:820 - Application run failed
java.lang.NoClassDefFoundError: org/springframework/beans/factory/config/InstantiationAwareBeanPostProcessorAdapter
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1012)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:862)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:760)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:681)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:639)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
解决问题
由于是新开项目,用的都是最新的版本,统一使用了2022版本的Springcloud,原因是因为最新Dubbo的3.1.x没有支持到Springboot3.x,所有需要用到 3.2.0-beta版本文章来源:https://www.toymoban.com/news/detail-608019.html
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
<version>3.2.0-beta.5</version>
</dependency>
创作声明:本文由个人@简明说原创,不允许转载,更多微服务分享可关注bilibili @简明说文章来源地址https://www.toymoban.com/news/detail-608019.html
到了这里,关于Springboot3.X整合Dubbo3.XSpringCloudAlibaba微服务 2022.0 + Springboot3.X 集成 Dubbo实现对外调用http内部调用RPC的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!