Springboot3.X整合Dubbo3.XSpringCloudAlibaba微服务 2022.0 + Springboot3.X 集成 Dubbo实现对外调用http内部调用RPC

1 篇文章 0 订阅
1 篇文章 0 订阅

前言

近期自己新开了一套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版本

 			<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 @简明说

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值