[ Spring] Integrate Spring Boot Dubbo with Nacos 2025
文章目录
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