Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的RPC实现服务的输出和输入功能,可以和Spring框架无缝集成。Dubbo是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现
dubbo核心节点之间的调用关系:
节点说明:
调用关系说明
服务容器负责启动,加载,运行服务提供者。
服务提供者在启动时,向注册中心注册自己提供的服务。
服务消费者在启动时,向注册中心订阅自己所需的服务。
注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.12.RELEASE
com.example
demo
0.0.1-SNAPSHOT
dubbo
Demo project for Spring Boot
8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-devtools
runtime
true
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
maven依赖
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.12.RELEASE
com.example
dubbo-api
0.0.1-SNAPSHOT
dubbo-api
Demo project for Spring Boot
8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-devtools
runtime
true
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-maven-plugin
InfoService类
package com.byd.service;
public interface InfoService {
String getInfo();
}
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.12.RELEASE
com.example
dubbo-provider
0.0.1-SNAPSHOT
dubbo-provider
Demo project for Spring Boot
8
org.springframework.boot
spring-boot-starter-web
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
2.2.5.RELEASE
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-config
2.2.5.RELEASE
com.alibaba.cloud
spring-cloud-starter-dubbo
2.2.5.RELEASE
com.example
demo
0.0.1-SNAPSHOT
compile
com.example
dubbo-api
0.0.1-SNAPSHOT
compile
com.example
demo
0.0.1-SNAPSHOT
compile
com.example
demo
0.0.1-SNAPSHOT
compile
org.apache.commons
commons-lang3
3.12.0
org.springframework.boot
spring-boot-maven-plugin
** InfoServiceImpl类**
package com.byd.service;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;
// dubbo提供的Service注解,用于声明对外暴露服务
// Service引入的是org.apache.dubbo.config.annotation.Service包
@Component
@DubboService
public class InfoServiceImpl implements InfoService {
@Override
public String getInfo() {
return "hello,这里是dubbo-provider模块!";
}
}
** 启动类**
package com.byd;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDubbo
@EnableDiscoveryClient
@SpringBootApplication
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
System.out.println("dubbo服务提供者8101启动了");
}
}
application.yml配置文件
server:
port: 8101
spring:
application:
name: dubbo-provider
dubbo:
registry:
address: nacos://127.0.0.1:8848 #注册地址
application:
name: dubbo-provider #应用名
protocol:
name: dubbo #dubbo协议
port: 20890 #协议端口
scan:
base-packages: com.byd #扫包范围
provider:
timeout: 30000 #超时时间
** pom依赖**
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.12.RELEASE
com.example
dubbo-consumer
0.0.1-SNAPSHOT
dubbo-consumer
Demo project for Spring Boot
8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
com.example
dubbo-api
0.0.1-SNAPSHOT
compile
org.springframework.boot
spring-boot-starter-test
test
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
2.2.5.RELEASE
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-config
2.2.5.RELEASE
com.alibaba.cloud
spring-cloud-starter-dubbo
2.2.5.RELEASE
org.apache.commons
commons-lang3
3.12.0
org.springframework.boot
spring-boot-maven-plugin
** InfoController类**
package com.byd.controller;
import com.byd.service.InfoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class InfoController {
//dumbo提供的Reference注解,用于调用远程服务
@DubboReference(check = false)
private InfoService infoService;
@GetMapping("/getInfo")
public String getInfo(){
return infoService.getInfo();
}
}
** 启动类**
package com.byd;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDubbo
@EnableDiscoveryClient
@SpringBootApplication
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
System.out.println("dubbo服务消费者8102启动了");
}
}
application.yml配置文件
server:
port: 8102
spring:
application:
name: dubbo-consumer
dubbo:
registry:
address: nacos://127.0.0.1:8848 #注册地址
application:
name: dubbo-consumer #应用名
protocol:
name: dubbo #dubbo协议
port: 20890 #协议端口
consumer:
timeout: 30000 #超时时间