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

[ 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_SETTINGS
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}

buildscript {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}

plugins {
    id("org.jetbrains.kotlin.jvm") version "2.0.21" apply false
    id("org.jetbrains.kotlin.kapt") version "2.0.21" apply false
    id("org.jetbrains.kotlin.plugin.spring") version "2.0.21" apply false
    id("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"
    // commons
    api("io.github.hellogoogle2000:kotlin-commons:1.0.19")
    // kotlin
    api("org.jetbrains.kotlin:kotlin-reflect:2.0.21")
    // spring
    api("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")
    // dubbo
    api("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.hello

import 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 DubboProviderApplication

fun main(args: Array<String>) {
    runApplication<DubboProviderApplication>(*args)
}
Dubbo Provider Service
package x.spring.hello.service

interface DubboConfigService {

    fun loadConfig(): String
}
package x.spring.hello.service

import 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.hello

import 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 DubboConsumerApplication

fun main(args: Array<String>) {
    runApplication<DubboConsumerApplication>(*args)
}
Dubbo Consumer Controller
package x.spring.hello.service

interface DubboConfigService {

    fun loadConfig(): String
}
package x.spring.hello.controller

import 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 {

    @DubboReference
    private 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.kler.cn/a/536676.html

相关文章:

  • 为什么要设计DTO类/什么时候设置DTO类?
  • 4.PPT:日月潭景点介绍【18】
  • LeetCode-197. 上升的温度
  • DeepSeek LLM(初代)阅读报告
  • 括号生成(回溯法详解)
  • BUU28 [GXYCTF2019]BabySQli1
  • 【论文写作】深度学习无线通信领域的一些国际著名期刊
  • 15:00面试,15:07就出来了,问的问题有点变态。。。
  • 项目实训:表白墙,图书管理系统
  • windows 10/11 开启wsl2运行linux 使用cuda方法
  • Flink CDC YAML:面向数据集成的 API 设计
  • Excel 融合 deepseek
  • JumpServer堡垒机管理服务器与数据库资产
  • 【论文阅读】Adversarial Detection: Attacking Object Detection in Real Time
  • 前端 CSS 动态设置样式::class、:style 等技巧详解
  • 基于WOA鲸鱼优化的TCN时间卷积神经网络时间序列预测算法matlab仿真
  • 【玩转 Postman 接口测试与开发2_019】第15章:利用 Postman 初探 API 性能测试(含实战截图)
  • FFmpeg使用GPU编解码,及在C++代码中实现FFmpeg使用GPU编解码
  • C# LINQ与集合类 数据操作
  • postgresql-15(yum安装教程)
  • 让文物“活”起来,以3D数字化技术传承文物历史文化!
  • [RabbitMQ] 常见面试题汇总 工作流程 消息可靠性 消息顺序性 幂等性 高级特性 延迟队列 仲裁队列 工作模式 消息积压 推拉模式
  • easyxor
  • 赛博算命之 ”梅花易数“ 的 “JAVA“ 实现 ——从玄学到科学的探索
  • element-ui rate 组件源码分享
  • zsh: command not found: pip