Apache Dubbo是一款微服务开发框架,它提供了 RPC通信 与 微服务治理 两大关键能力
Nacos致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。
停更维护
了plugins {
id 'java'
id 'org.springframework.boot' version '2.7.0' //springboot依赖版本
id 'io.spring.dependency-management' version '1.0.11.RELEASE' //spring的依赖管理插件版本
}
//版本控制
ext {
set('springCloudVersion', "2021.0.3") //SpringCloud依赖版本
set('springCloudAlibabaVersion', "2021.0.1.0") //SpringCloudAlibaba依赖版本
set('springBootVersion', "2.7.0")
}
//全部模块通用配置
allprojects{
// 项目的默认名称和版本
group ='com.threesides'
version ='1.0.0-SNAPSHOT'
// 指定JDK版本
sourceCompatibility = '11'
// 指定仓库地址
repositories {
// aliyun镜像
maven { url 'https://maven.aliyun.com/repository/public' }
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
maven { url 'https://maven.aliyun.com/repository/spring' }
maven { url 'https://maven.aliyun.com/repository/grails-core' }
maven { url 'https://maven.aliyun.com/repository/apache-snapshots' }
// 本地仓库
mavenLocal()
// 中央️仓库
mavenCentral()
}
}
// 子模块配置
subprojects {
// 使用插件
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
//子模块通用依赖
dependencies {
testImplementation "org.springframework.boot:spring-boot-starter-test:${springBootVersion}"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${springBootVersion}"
}
//子模块依赖管理
dependencyManagement {
dependencies {
dependency "org.springframework.boot:spring-boot-starter:${springBootVersion}"
}
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
mavenBom "com.alibaba.cloud:spring-cloud-alibaba-dependencies:${springCloudAlibabaVersion}"
}
}
}
启动器
以及依赖group 'com.threesides'
version '1.0.0-SNAPSHOT'
jar.enabled = true
bootJar.enabled = true
dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test' //测试环境
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
implementation group: 'com.alibaba.cloud', name: 'spring-cloud-starter-alibaba-nacos-discovery' //注册中心,这里使用的是alibaba-nacos,其他注册中心也可
implementation group: 'com.alibaba.cloud', name: 'spring-cloud-starter-alibaba-nacos-config' //配置中心,这里使用的是alibaba-nacos
implementation 'org.springframework.cloud:spring-cloud-starter-gateway' //网关 gateway 项目不能同时依赖spring-boot-starter-web
implementation 'org.springframework.cloud:spring-cloud-loadbalancer' // 负载均衡 SpringCloud 版本在 2021.0.x以上必须引依赖
implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap'
}
test {
useJUnitPlatform()
}
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
server:
port: 1101
spring:
application:
name: demo-gateway
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
public interface DemoInterface {
public String demo(String message);
}
group 'com.threesides'
version '1.0.0-SNAPSHOT'
archivesBaseName = "demo-provider"
jar.enabled = true
bootJar.enabled = true
dependencies {
// 引入本地的 xxxx模块
implementation project(':demo-interface')
// 引入 spring-cloud 依赖
// 注册中心,这里使用的是alibaba-nacos,其他注册中心也可。
implementation group: 'com.alibaba.cloud', name: 'spring-cloud-starter-alibaba-nacos-discovery'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web'
//dubbo
implementation 'org.apache.dubbo:dubbo-spring-boot-starter:3.0.8'
implementation 'com.alibaba.nacos:nacos-client:2.1.0' // 由于我使用的客户端是2.1.0
//由于报错 java.lang.ClassNotFoundException: com.google.common.collect.Maps
//引入guava
implementation 'com.google.guava:guava:31.1-jre'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}
test {
useJUnitPlatform()
}
server:
port: 8010
# dubbo的配置
dubbo:
application:
name: demo-provider
registry:
address: nacos://127.0.0.1:8848
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
@EnableDubbo
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
import org.apache.dubbo.config.annotation.DubboService;
@DubboService
public class DemoProviderImpl implements DemoInterface {
@Override
public String demo(String message) {
System.out.println(message);
return message+"成功了";
}
}
group 'com.threesides'
version '1.0.0-SNAPSHOT'
archivesBaseName = "demo-consumer"
jar.enabled = true
bootJar.enabled = true
dependencies {
// 引入本地的 xxxx模块
implementation project(':demo-interface')
// 引入 spring-cloud 依赖
// 注册中心,这里使用的是alibaba-nacos,其他注册中心也可。
implementation group: 'com.alibaba.cloud', name: 'spring-cloud-starter-alibaba-nacos-discovery'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web'
//dubbo
implementation 'org.apache.dubbo:dubbo-spring-boot-starter:3.0.8'
implementation 'com.alibaba.nacos:nacos-client:2.1.0' // 由于我使用的客户端是2.1.0
//由于报错 java.lang.ClassNotFoundException: com.google.common.collect.Maps
//引入guava
implementation 'com.google.guava:guava:31.1-jre'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}
test {
useJUnitPlatform()
}
server:
port: 8011
spring:
application:
name: demo-consumer
cloud:
nacos:
discovery:
server-addr: http://127.0.0.1:8848 # 注册中心 http://ip:端口号
dubbo:
registry:
address: nacos://127.0.0.1:8848
register: false
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
@EnableDubbo
@SpringBootApplication
public class ComsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ComsumerApplication.class, args);
}
}
@RestController
@RequestMapping("/demo")
public class DemoConsumerController {
@Autowired
private DemoConsumerService demoConsumerService;
@GetMapping("/getProviderService")
public String getProviderService() {
return demoConsumerService.getProviderService();
}
}
import org.apache.dubbo.config.annotation.DubboReference;
@Service
public class DemoConsumerService {
@DubboReference
private DemoInterface demoInterface;
public String getProviderService() {
return demoInterface.demo("使用Dubbo调用");
}
}
这里引入Nacos的配置中心的使用
implementation group: 'com.alibaba.cloud', name: 'spring-cloud-starter-alibaba-nacos-config'
spring:
cloud:
gateway:
discovery:
locator:
enabled: true #开启网关拉取nacos的服务
routes:
- id: demo-consumer
uri: lb://demo-consumer
predicates:
- Path=/demo/**
spring:
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
file-extension: yaml
refresh-enabled: true # 是否动态刷新,默认为false
# 支持多个共享dataId的配置,优先级小于extension-configs,shared-configs是一个集合
shared-configs[0]:
# 网关 通用配置可以定义在这个里面
dataId: demo-gateway.yaml # 配置文件名dataId
group: DEFAULT_GROUP # 默认为DEFAULT_GROUP
这里使用的Restful Fast Request
插件测试
http://localhost:1101/demo/getProviderService