关于dubbo:html
一个远程服务调用的分布式框架,调用协议一般包含传输协议和序列化协议。Dubbo自己支持多种远程调用方式,例如Dubbo RPC(二进制序列化 + tcp协议)、http invoker(二进制序列化 + http协议)、hessian(二进制序列化 + http协议)、WebServices (文本序列化 + http协议)等。java
官网介绍地址node
节点 | 角色说明 |
---|---|
Provider |
暴露服务的服务提供方 |
Consumer |
调用远程服务的服务消费方 |
Registry |
服务注册与发现的注册中心 |
Monitor |
统计服务的调用次数和调用时间的监控中心 |
Container |
服务运行容器 |
管理控制台的编译安装:python
# 克隆项目到本地,并编译安装和启动(若是是Windows下,则在powershell进行) git clone https://github.com/apache/incubator-dubbo-ops.git
# 切到项目根目录 cd incubator-dubbo-admin-develop
# 编译构建 mvn clean package
# 修改配置文件,指定注册中心地址
dubbo-admin-server/src/main/resources/application-production.properties
# 主要的配置有:git
github
admin.registry.address=zookeeper://127.0.0.1:2181算法
spring
# 启动服务
cd dubbo-distribution/target
java -jar dubbo-admin-0.1.jar
# 或如下命令启动服务
mvn --projects dubbo-admin-server spring-boot:run
启动完成后,直接访问http://localhost:8080
如何测试dubbo接口:shell
Python hessian+http的方式调用:apache
一、dubbo项目中,增长hessian方式序列化,及相关依赖。下图为xml配置方式示例。
二、获取接口地址(可在管理台查看)、方法及方法的入参。
三、安装 python-hessian
python -m pip install python-hessian
四、编写Python脚本调用接口
# coding=utf-8 import pytest from pyhessian.client import HessianProxy class TestDubbo(object): url = "http://169.254.210.145:1234/" interface = "com.ymxdclass.user.service.UserService" full_url = url + interface # full_url = "http://169.254.210.145:8888/com.xdclass.user.service.FileService" def testsayHelloWithSpec(self): params = u"什么我调用成功了吗" # 建立链接对象 service = HessianProxy(self.full_url) # 重载方法__call()__里发送二进制数据进行请求,调用方法 res = service.sayHello(params) assert "什么我调用成功了吗" in res print(res) # @pytest.mark.skip() def testsayHelloWithInt(self): params = 123 service = HessianProxy(self.full_url) res = service.sayHello(params) assert 123 in res print(res) if __name__ == "__main__": pytest.main(["-q","TestDubbo.py"])
java调用脚本
1 import com.ymxdclass.user.service.UserService; 2 import org.junit.BeforeClass; 3 import org.junit.Test; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
6 public class ConsumerTest { 7 static ClassPathXmlApplicationContext context; 8 static UserService userService; 9 @BeforeClass 10 public static void beforeClass(){ 11 if(context==null) { 12 // 默认从类路径中加载配置文件 13 context = new ClassPathXmlApplicationContext("consumer.xml"); 14 System.out.println("load"); 15 // 在Spring中还提供了Lifecycle接口,Lifecycle中包含start/stop方法,实现此接口后Spring保证在启动的时候调用其start方法开始生命周期,主要用于控制异步处理过程 16 context.start(); 17 // System.out.println("start"); 18 }
// 建立接口实例(定义接口的引用变量,再引用实现了该接口的实例) 19 userService=(UserService) context.getBean("userService"); 20 21 } 22 @Test 23 public void consumerTestCase1(){
// 调用方法 24 String hello = userService.sayHello("world"); 25 assertThat(hello,containsString("world"); 26 System.out.println(hello); 27 } 28 29 }