当前位置: 首页 > news >正文

[ Spring] Integrate Spring Boot Dubbo with Nacos 2025

文章目录

          • Dubbo Project Structure
          • Declare Plugins and Repositories
          • Introduce Dependencies
          • Dubbo Consumer Properties
          • Dubbo Provider Application
          • Dubbo Provider Service
          • Dubbo Consumer Properties
          • Dubbo Consumer Application
          • Dubbo Consumer Controller
          • Command References

Dubbo Project Structure
  • provider : provide service implementation
  • consumer : calling service offered by provider
  • registry center : roled by nacos, register service that can be discovered by consumer
  • config center : roled by nacos, offer cloud configurations
Declare Plugins and Repositories
pluginManagement {repositories {gradlePluginPortal()google()mavenCentral()}
}dependencyResolutionManagement {repositoriesMode = RepositoriesMode.PREFER_SETTINGSrepositories {gradlePluginPortal()google()mavenCentral()}
}buildscript {repositories {gradlePluginPortal()google()mavenCentral()}
}plugins {id("org.jetbrains.kotlin.jvm") version "2.0.21" apply falseid("org.jetbrains.kotlin.kapt") version "2.0.21" apply falseid("org.jetbrains.kotlin.plugin.spring") version "2.0.21" apply falseid("org.springframework.boot") version "3.4.1" apply false
}include("dubbo-provider")
include("dubbo-consumer")
Introduce Dependencies

suitable for both provider and consumer modules

plugins {id("org.jetbrains.kotlin.jvm")id("org.jetbrains.kotlin.kapt")id("org.jetbrains.kotlin.plugin.spring")id("org.springframework.boot")
}java {toolchain {languageVersion = JavaLanguageVersion.of(17)}
}dependencies {val springBootVersion = "3.4.2"val springCloudVersion = "4.2.0"val springCloudAlibabaVersion = "2023.0.3.2"// commonsapi("io.github.hellogoogle2000:kotlin-commons:1.0.19")// kotlinapi("org.jetbrains.kotlin:kotlin-reflect:2.0.21")// springapi("org.springframework.boot:spring-boot-starter:$springBootVersion")api("org.springframework.boot:spring-boot-starter-web:$springBootVersion")api("org.springframework.boot:spring-boot-starter-actuator:$springBootVersion")api("org.springframework.cloud:spring-cloud-starter-bootstrap:$springCloudVersion")// dubboapi("org.apache.dubbo:dubbo-spring-boot-starter:3.3.3")api("org.apache.dubbo:dubbo-nacos-spring-boot-starter:3.3.3")api("com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery:$springCloudAlibabaVersion")
}
Dubbo Consumer Properties
# service
server.port=10003
spring.application.name=dubbo-consumer
spring.profiles.active=dev
spring.devtools.add-properties=false
# dubbo
dubbo.application.name=dubbo-consumer
dubbo.application.qos-port=20003
dubbo.registry.address=nacos://localhost:8848
dubbo.registry.username=nacos
dubbo.registry.password=nacos
dubbo.protocol.name=dubbo
dubbo.protocol.port=30003
# nacos
spring.cloud.nacos.discovery.server-addr=localhost:8848
spring.cloud.nacos.discovery.username=nacos
spring.cloud.nacos.discovery.password=nacos
Dubbo Provider Application
package x.spring.helloimport org.apache.dubbo.config.spring.context.annotation.DubboComponentScan
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.cloud.client.discovery.EnableDiscoveryClient@SpringBootApplication
@EnableDiscoveryClient
@DubboComponentScan(basePackages = ["x.spring.hello"])
class DubboProviderApplicationfun main(args: Array<String>) {runApplication<DubboProviderApplication>(*args)
}
Dubbo Provider Service
package x.spring.hello.serviceinterface DubboConfigService {fun loadConfig(): String
}
package x.spring.hello.serviceimport org.apache.dubbo.config.annotation.DubboService@DubboService
class DubboConfigServiceImpl : DubboConfigService {override fun loadConfig(): String {return "dubbo-provider-config"}
}
Dubbo Consumer Properties
# service
server.port=10003
spring.application.name=dubbo-consumer
spring.profiles.active=dev
spring.devtools.add-properties=false
# dubbo
dubbo.application.name=dubbo-consumer
dubbo.application.qos-port=20003
dubbo.registry.address=nacos://localhost:8848
dubbo.registry.username=nacos
dubbo.registry.password=nacos
dubbo.protocol.name=dubbo
dubbo.protocol.port=30003
# nacos
spring.cloud.nacos.discovery.server-addr=localhost:8848
spring.cloud.nacos.discovery.username=nacos
spring.cloud.nacos.discovery.password=nacos
Dubbo Consumer Application
package x.spring.helloimport org.apache.dubbo.config.spring.context.annotation.DubboComponentScan
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.cloud.client.discovery.EnableDiscoveryClient@SpringBootApplication
@EnableDiscoveryClient
@DubboComponentScan(basePackages = ["x.spring.hello"])
class DubboConsumerApplicationfun main(args: Array<String>) {runApplication<DubboConsumerApplication>(*args)
}
Dubbo Consumer Controller
package x.spring.hello.serviceinterface DubboConfigService {fun loadConfig(): String
}
package x.spring.hello.controllerimport org.apache.dubbo.config.annotation.DubboReference
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
import x.spring.hello.service.DubboConfigService@RestController
class DubboConfigController {@DubboReferenceprivate lateinit var service: DubboConfigService@GetMapping("/dubbo/config")fun load(): String {return service.loadConfig()}
}
Command References
nacos=sudo bash -f bin/startup.sh -m standalone
url=http://localhost:8848/nacos
url=http://localhost:10003/dubbo/config
http://www.lryc.cn/news/532360.html

相关文章:

  • 【3分钟极速部署】在本地快速部署deepseek
  • 【QT笔记】使用QScrollArea实现多行文本样式显示
  • 大模型中提到的超参数是什么
  • 【Uniapp-Vue3】z-paging插件组件实现触底和下拉加载数据
  • UE虚幻引擎No Google Play Store Key:No OBB found报错如何处理
  • OKHttp拦截器解析
  • STM32标准库移植RT-Thread nano
  • c++11总结26——std::regex
  • langchain教程-12.Agent/工具定义/Agent调用工具/Agentic RAG
  • leetcode_双指针 125.验证回文串
  • ML.NET库学习001:基于PCA的信用卡异常检查之样本处理与训练
  • 【华为OD机考】华为OD笔试真题解析(1)--AI处理器组合
  • edu小程序挖掘严重支付逻辑漏洞
  • 力扣 279. 完全平方数
  • 鸿蒙生态潮起:开发者的逐浪之旅
  • Diskgenius系统迁移之后无法使用USB启动
  • Kafka 可靠性探究—副本刨析
  • 我的博文天地测试报告
  • EtherCAT主站IGH-- 35 -- IGH之pdo_list.h/c文件解析
  • 嵌入式开发神器:Buildroot的介绍和使用方法
  • JavaScript系列(61)--边缘计算应用开发详解
  • 【LeetCode】day15 142.环形链表II
  • 代理对象与目标对象
  • 【Kubernetes Pod间通信-第3篇】Kubernetes中Pod与ClusterIP服务之间的通信
  • DNN(深度神经网络)近似 Lyapunov 函数
  • 128陷阱
  • PromptSource和LangChain哪个更好
  • 构成正方形的数量:算法深度剖析与实践
  • Redis持久化-秒杀系统设计
  • 音视频入门基础:RTP专题(8)——使用Wireshark分析RTP