Apache Dubbo 是一款高性能、轻量级的RPC框架
Dubbo官网
Apache Dubbo提供了六大核心能力:面向接口代理的高性能RPC调用,智能容错和负载均衡,服务自动注册和发现,高度可扩展能力,运行期流量调度,可视化的服务治理与运维
DubboAdmin官网
git clone https://github.com/apache/dubbo-admin.git
dubbo-admin-server/src/main/resources/application.properties
中指定注册中心地址mvn clean package -Dmaven.test.skip=true
cd dubbo-admin-server/target; java -jar dubbo-admin-server-0.1.jar
http://localhost:8080
之前克隆的代码,进入 dubbo-admin-ui
目录,
打开cmd命令行,执行
npm install
,然后执行npm run dev
,
即可访问DubboAdminWeb界面
访问地址
http://localhost:8082
使用Docker搭建Zk
docker run -d --name=zookeeper -p 2181:2181 zookeeper
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>2.7.8version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
<version>2.12.0version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
<version>2.12.0version>
dependency>
# 应用名称
spring.application.name=rpc-provider-demo
# 应用服务 WEB 访问端口
server.port=8070
dubbo.application.name=${spring.application.name}
demo.service.version=1.0.0
dubbo.scan.base-packages=com.example.rpcproviderdemo.jacksondemo.service
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
dubbo.registry.address=zookeeper://180.76.250.119:2181
接口类IShopRpc .java
public interface IShopRpc {
public String sayHello(String name);
public String ping();
public String test3(Integer aaa);
}
实现类ShopRpcImpl.java
import com.example.rpcproviderdemo.jacksondemo.service.IShopRpc;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
@DubboService(version = "1.0.0",group = "shop")
public class ShopRpcImpl implements IShopRpc {
private static Long countInvk =1L;
@Value("${dubbo.application.name}")
private String serviceName;
@Override
public String sayHello(String name) {
System.err.println("rpc调用成功了!");
return String.format("hhh ,%s : Hello, %s", serviceName, name);
}
@Override
public String ping() {
countInvk++;
System.err.println(countInvk);
return "pong";
}
@Override
public String test3(Integer aaa) {
return "success: "+aaa.toString();
}
}
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>2.7.8version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
<version>2.12.0version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
<version>2.12.0version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
# 应用名称
spring.application.name=rpc-consumer-demo
# 应用服务 WEB 访问端口
server.port=8071
dubbo.application.name=${spring.application.name}
demo.service.version=1.0.0
dubbo.scan.base-packages=com.example.rpcproviderdemo.jacksondemo.service
dubbo.protocol.name=dubbo
dubbo.registry.address=zookeeper://180.76.250.119:2181
在这个项目里再次定义IShopRpc.java,要另起一个包写接口,包名要和上一个服务中的IShopRpc接口的包名一致
package com.example.rpcproviderdemo.jacksondemo.service;
public interface IShopRpc {
public String sayHello(String name);
public String ping();
public String test3(Integer aaa);
}
不需要写实现类,因为rpc调用,只要知道provider服务暴露的接口就可以了。
这里直接写到启动类里,正常要写到业务层代码里。
package com.example.rpcconsumerdemo;
import com.example.rpcproviderdemo.jacksondemo.service.IShopRpc;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.Method;
import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@Slf4j
@EnableDubboConfig
@SpringBootApplication
public class RpcConsumerDemoApplication {
@DubboReference(
version = "1.0.0",
group = "shop",
timeout = 100,
methods = {
@Method(name = "sayHello", timeout = 300),
@Method(name = "ping", timeout = 200),
@Method(name = "test3", timeout = 200),
}
)
private IShopRpc shopService;
public static void main(String[] args) {
SpringApplication.run(RpcConsumerDemoApplication.class, args);
}
@Bean
public ApplicationRunner runner() {
return args ->{
log.warn(shopService.ping());
log.warn(shopService.sayHello("mercyblitz"));
log.info(shopService.test3(123456));
};
}
}
消费者服务的控制台
生产者服务的控制台
DubboAdminWeb界面
rpc生产者
https://gitee.com/lzh2019/rpc-provider-demo.git
rpc消费者
https://gitee.com/lzh2019/rpc-consumer-demo.git
dubbo框架以前是阿里内部的rpc框架,现在交由开源组织Apache维护,导致一些注解等用法发生了改变。
之前的dubbo教程,都会让你用dubbo提供的@Service 和 @Reference 注解 ,来标注 rpc接口。但是现在apache Dubbo做了更新。
从依赖包即可看出
org.apache.dubbo
dubbo-spring-boot-starter
2.7.8
现在官方给出的
dubbo-spring-boot-starter 中已经没有了之前的注解, 而是用 @DubboService 和
@DubboReference
来分别标注
服务提供者的rpc接口 和 服务消费者的 rpc接口。
apache dubbo 官方开发了一个新版的监控管理中心 dubboAdmin,该版本和阿里很早之前的dubboAdmin功能相差不大,只是用vue重构了,做成了前后端分离的web。
但是ApacheDubboAdmin里提供了负载均衡、服务关系、接口文档等功能增加了dubbo的可玩性,大家可以尝试一下。