背景:公司开发的系统在本地运行太过麻烦,并且该系统使用dubbo管理服务,使用rpc进行不同应用间的通信。某次开发的时候需要查看调用rpc接口后的返回结果,操作起来太麻烦。于是尝试本地使用java程序直接调用rpc接口进行调试。在此记录代码,抽空把它封装成一个测试dubbo的rpc接口的程序
java代码
package test.kgtone; import com.alibaba.dubbo.config.ApplicationConfig; import com.alibaba.dubbo.config.ReferenceConfig; import com.alibaba.dubbo.config.RegistryConfig; import com.alibaba.dubbo.config.utils.ReferenceConfigCache; import com.alibaba.dubbo.rpc.service.GenericService; import com.alibaba.fastjson.JSON; public class TestRpc { public static void main(String[] args) { //参数配置---------------------------------- String zk_addr = "zookeeper://172.21.65.26:2181"; int timeout = 300000; String group = "LS"; String ifce = "com.pt.poseidon.org.api.IOrgService"; String methodName = "getOrgByNo"; String[] paramTypes = new String[]{"java.lang.String"}; Object[] params = new Object[]{"35402"}; //服务调用代码-------------------------------- ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>(); reference.setApplication(new ApplicationConfig("mamb-test")); reference.setRegistry(new RegistryConfig(zk_addr)); reference.setGeneric(true); reference.setTimeout(timeout); reference.setGroup(group); reference.setInterface(ifce); ReferenceConfigCache cache = ReferenceConfigCache.getCache(); GenericService genericService = cache.get(reference); Object result = genericService.$invoke(methodName, paramTypes,params ); System.out.println(JSON.toJSONString(result)); } }
pom.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>priv.kgtone</groupId> <artifactId>dubbo-rpc-test</artifactId> <version>1.0.0</version> <dependencies> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.14.0-GA</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo-rpc-api --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.8.4</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo-rpc-api --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.12</version> </dependency> <!-- https://mvnrepository.com/artifact/com.101tec/zkclient --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.4</version> </dependency> <!--netty jar--> <dependency> <groupId>io.netty</groupId> <artifactId>netty</artifactId> <version>3.7.0.Final</version> </dependency> </dependencies> </project>
log4j.properties文件
# Global logging configuration debug #log4j.rootLogger=DEBUG, stdout log4j.rootLogger=ERROR, stdout # Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
此过程中踩过的坑如下:
1.刚开始忽视了log4j.properties的日志配置文件,认为不用输出什么日志,很快就可以弄好。实际上没有输出日志,很难定位异常的位置。
2.刚没有添加netty的依赖,报错程序启动后报错找不到服务提供者。后来添加上log4j的日志配置,很快定位到是netty的依赖没有加入的原因。