深入浅出学习 DubboSpringBoot 与 Dubbo 整合
Posted 思想累积
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了深入浅出学习 DubboSpringBoot 与 Dubbo 整合相关的知识,希望对你有一定的参考价值。
1、 Dubbo 与 SpringBoot 整合
Apache Dubbo Spring Boot Projecthttps://github.com/apache/dubbo-spring-boot-project
1.1 SpringBoot 整合 Dubbo(一)
导入 dubbo-starter 依赖,在 配置文件中配置属性,使用 @DubboService
暴露服务,使用 DubboReference
引用服务
导入 maven 依赖
<!-- Dubbo Spring Boot Starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>5.1.0</version>
</dependency>
yml 配置文件
dubbo:
application:
name: user-service-provider # 应用名称
registry:
address: 127.0.0.1:2181 # 服务地址
protocol: zookkkper
protocol:
name: dubbo
port: 20880 # 端口占用
monitor:
protocol: registry
java 代码
provider 提供者
启动类使用 @EnableDubbo
注解暴露服务
package com.sqdkk.demo;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo // 开启基于注解的 dubbo 功能
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
使用 @DubboService
注解注册 Service
@DubboService
@Component
public class ProviderServiceImpl implements ProviderService {
}
consumer 消费者
使用 @DubboReference
注解引用该服务
1.2 SpringBoot 整合 Dubbo(二)
导入 dubbo-starter 依赖
启动类使用 @ImportResource(localtions="classpath:provider.xml")
导入 dubbo 配置文件
1.3 SpringBoot 整合 Dubbo(三)
使用 API 方式配置 Dubbo,API 属性与配置项一对一,将每一个组件手动创建到容器中
ApplicationConfig.setName("xxx")
对应 <dubbo:application name="xxx" />
启动类使用 @DubboComponentScan(basePackages = "")
注解,
provider 服务提供者
import org.apache.dubbo.rpc.config.ApplicationConfig;
import org.apache.dubbo.rpc.config.RegistryConfig;
import org.apache.dubbo.rpc.config.ProviderConfig;
import org.apache.dubbo.rpc.config.ServiceConfig;
import com.xxx.XxxService;
import com.xxx.XxxServiceImpl;
// 服务实现
XxxService xxxService = new XxxServiceImpl();
// 当前应用配置
ApplicationConfig application = new ApplicationConfig();
application.setName("xxx");
// 连接注册中心配置
RegistryConfig registry = new RegistryConfig();
registry.setAddress("10.20.130.230:9090");
registry.setUsername("aaa");
registry.setPassword("bbb");
// 服务提供者协议配置
ProtocolConfig protocol = new ProtocolConfig();
protocol.setName("dubbo");
protocol.setPort(12345);
protocol.setThreads(200);
// 注意:ServiceConfig为重对象,内部封装了与注册中心的连接,以及开启服务端口
// 服务提供者暴露服务配置
ServiceConfig<XxxService> service = new ServiceConfig<XxxService>(); // 此实例很重,封装了与注册中心的连接,请自行缓存,否则可能造成内存和连接泄漏
service.setApplication(application);
service.setRegistry(registry); // 多个注册中心可以用setRegistries()
service.setProtocol(protocol); // 多个协议可以用setProtocols()
service.setInterface(XxxService.class);
service.setRef(xxxService);
service.setVersion("1.0.0");
// 暴露及注册服务
service.export();
consumer 服务消费者
import org.apache.dubbo.rpc.config.ApplicationConfig;
import org.apache.dubbo.rpc.config.RegistryConfig;
import org.apache.dubbo.rpc.config.ConsumerConfig;
import org.apache.dubbo.rpc.config.ReferenceConfig;
import com.xxx.XxxService;
// 当前应用配置
ApplicationConfig application = new ApplicationConfig();
application.setName("yyy");
// 连接注册中心配置
RegistryConfig registry = new RegistryConfig();
registry.setAddress("10.20.130.230:9090");
registry.setUsername("aaa");
registry.setPassword("bbb");
// 注意:ReferenceConfig为重对象,内部封装了与注册中心的连接,以及与服务提供方的连接
// 引用远程服务
ReferenceConfig<XxxService> reference = new ReferenceConfig<XxxService>(); // 此实例很重,封装了与注册中心的连接以及与提供者的连接,请自行缓存,否则可能造成内存和连接泄漏
reference.setApplication(application);
reference.setRegistry(registry); // 多个注册中心可以用setRegistries()
reference.setInterface(XxxService.class);
reference.setVersion("1.0.0");
// 和本地bean一样使用xxxService
XxxService xxxService = reference.get(); // 注意:此代理对象内部封装了所有通讯细节,对象较重,请缓存复用
以上是关于深入浅出学习 DubboSpringBoot 与 Dubbo 整合的主要内容,如果未能解决你的问题,请参考以下文章
《深入浅出Python机器学习(段小手)》PDF代码+《推荐系统与深度学习》PDF及代码+《自然语言处理理论与实战(唐聃)》PDF代码源程序
分享《深入浅出深度学习:原理剖析与python实践》PDF+源码+黄安埠
分享《深入浅出深度学习:原理剖析与python实践》PDF+源代码
深入浅出图神经网络|GNN原理解析☄学习笔记图信号处理与图卷积神经网络