Dubbo测试代码(二)
2018-01-15 16:31
183 查看
我创建了4个Maven工程
parent(pom)和3个modul
我事先配好了SSM Web工程 当然你也可以写单元测试 进行测试
所用的maven依赖:
父工程:
其次 consumer工程和provider工程 都要添加 公共的接口api依赖:
此时 我模拟RPC调用
web 网页发起请求 进入Consumer工程 查询学生列表
consumer工程 调用服务 provider工程 提供服务 返回学生list 给consumer工程 :
代码如下:
1.接口工程 编写接口程序:
2.服务端工程代码:
其次 添加dubbo服务端配置文件 注意添加 schema 约束头文件
服务端干了两件事 第一 实现接口工程的方法 ,对接口方法做出业务的实现。第二件 配置文件中 向注册中心注册服务 并给出dubbo:service 中的ref =”#”(相当于bean id= # > /bean>) 以及接口
3.客户端代码:
客户端配置文件
客户端 就干了一件事: 向注册中心订阅服务,将UserService的实现类 以userService的名称 注入Controller中。即可完成远程调用。
启动细节:
首先启动服务之前 先启动zookeeper zookeeper的配置不多说了
如果是单机 就是
如果是集群 就是
要知道 启动完zookeeper应该先启动服务端project,然后在启动consumer端的工程,否则Spring会报错,那么问题:如果 有一个核心工程 和另一个核心工程 两个互相调用 怎么办呢?
如果互相调用 可以在提供一个配置文件 将服务端/消费端 的配置写入 即可 ,注意:application 只能有一个
两个工程互相调用 就是配置项的东西了
check=”false” 在订阅服务时不检查 服务端是否提供服务。
另附上 dubbo web监控界面
需要 dubbo-admin工程 github上下载 https://github.com/alibaba/dubbo
进入 dubbo-admin 目录 打包:
mvn package -Dmaven.skip.test=true
打包成功后:把dubbo-admin-2.5.4-SNAPSHOT.war放到tomcat的webapps目录下
我改成了dubbo
修改tomcat 端口 不要是8080 我改成8090 因为zookeeper jetty管理窗口是 8080 防止冲突
启动tomcat 输入 localhost:8090/dubbo(项目名)/
管理员账号密码:root
游客账号密码:guest
parent(pom)和3个modul
dubbo-api //服务端和消费端共用的接口工程 dubbo-consumer //消费端 dubbo-parent //父工程 提供jar dubbo-provider //服务端
我事先配好了SSM Web工程 当然你也可以写单元测试 进行测试
所用的maven依赖:
父工程:
//除去SSM和连接池 log4j 等等的 dubbo必须的依赖 <!-- Dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <!-- Zookeeper --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.3.6</version> <exclusions> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency>
其次 consumer工程和provider工程 都要添加 公共的接口api依赖:
<!-- 依赖接口工程 --> <dependency> <groupId>dubbo-demo</groupId> <artifactId>dubbo-api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
此时 我模拟RPC调用
web 网页发起请求 进入Consumer工程 查询学生列表
consumer工程 调用服务 provider工程 提供服务 返回学生list 给consumer工程 :
代码如下:
1.接口工程 编写接口程序:
package cn.dubbo.interf; import java.util.List; public interface UserService { public List findStuNameById(String stuId); //我这里根据一个id 查出一堆学生 不要在意细节 }
2.服务端工程代码:
package cn.dubbo.controller; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Service; import cn.dubbo.interf.UserService; @Service("userService") public class UserServiceImpl implements UserService{ @Override public List findStuNameById(String stuId) { List<String> stuList = new ArrayList<String>(); stuList.add("何睿"); stuList.add("张三"); return stuList; } }
其次 添加dubbo服务端配置文件 注意添加 schema 约束头文件
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 定义服务端的应用名 所属用户 --> <dubbo:application name="hr-provider-provider" owner="hr" /> <!-- 向zookeeper注册 --> <dubbo:registry address="zookeeper://localhost:2181" /> <!-- 暴露服务 端口 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 定义服务 实现定义好的公共接口 --> <dubbo:service interface="cn.dubbo.interf.UserService" ref="userService" protocol="dubbo" > <dubbo:method name="findStuNameById" timeout="3000" /> </dubbo:service> </beans>
服务端干了两件事 第一 实现接口工程的方法 ,对接口方法做出业务的实现。第二件 配置文件中 向注册中心注册服务 并给出dubbo:service 中的ref =”#”(相当于bean id= # > /bean>) 以及接口
3.客户端代码:
package cn.dubbo.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import cn.dubbo.interf.UserService; @Controller public class StuController { @Autowired private UserService userService; @RequestMapping("/name") @ResponseBody public List find1(){ return userService.findStuNameById("1"); } }
客户端配置文件
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 定义调用端的应用名称 所属用户 --> <dubbo:application name="hr-consumer-consumer" owner="hr"/> <!-- 向zookeeper 订阅 --> <dubbo:registry address="zookeeper://localhost:2181"/> <!-- 调用端调用接口 --> <dubbo:reference interface="cn.dubbo.interf.UserService" id="userService" check="false"/> </beans>
客户端 就干了一件事: 向注册中心订阅服务,将UserService的实现类 以userService的名称 注入Controller中。即可完成远程调用。
启动细节:
首先启动服务之前 先启动zookeeper zookeeper的配置不多说了
如果是单机 就是
<dubbo:registry address="zookeeper://localhost:2181"/>
如果是集群 就是
<dubbo:registry address="zookeeper://node1:2181,node2:2181,node3:2181"/> //多插一句嘴 肯定是奇数个节点 保证选举正常
要知道 启动完zookeeper应该先启动服务端project,然后在启动consumer端的工程,否则Spring会报错,那么问题:如果 有一个核心工程 和另一个核心工程 两个互相调用 怎么办呢?
如果互相调用 可以在提供一个配置文件 将服务端/消费端 的配置写入 即可 ,注意:application 只能有一个
两个工程互相调用 就是配置项的东西了
check=”false” 在订阅服务时不检查 服务端是否提供服务。
另附上 dubbo web监控界面
需要 dubbo-admin工程 github上下载 https://github.com/alibaba/dubbo
进入 dubbo-admin 目录 打包:
mvn package -Dmaven.skip.test=true
打包成功后:把dubbo-admin-2.5.4-SNAPSHOT.war放到tomcat的webapps目录下
我改成了dubbo
修改tomcat 端口 不要是8080 我改成8090 因为zookeeper jetty管理窗口是 8080 防止冲突
启动tomcat 输入 localhost:8090/dubbo(项目名)/
管理员账号密码:root
游客账号密码:guest
相关文章推荐
- 代码行号测试
- 你都使用哪些工具来测试代码的性能?
- 代码插入测试
- 代码着色测试
- PHP 代码优化测试【Benchmark数据测试】
- maven编译不执行Juiit的测试代码
- c# for 代码测试
- 测试代码运行状态和预期不一致时的处理
- 全程测试,从需求到设计到代码,集中人力来解决每个环节遇到的问题
- Python黑帽子--黑客与渗透测试编程之道 python3 实现代码
- c++测试代码时间
- CSDN博客插入代码测试
- 本地Eclipse中Tomcat运行测试环境代码
- 在Java内存模型中测试并发程序代码
- 测试代码的执行时间:%time和%timeit
- 非常实用的Junit3与Junit4 测试 以及两者平滑过渡(高兼容性)实例代码
- imx6q led灯驱动及测试代码(自动创建设备文件v1)
- 动态重新加载Class机制之代码测试
- 动态生成代码测试090325
- Eclipse+Java+OpenCV310环境搭建和代码测试