sofarpc是目前在着手使用的一个rpc框架,然鹅,一直整合不成功,现在提取出一个简单工程来排错。注意,使用的sofarpc的版本是5.5.0。
有中文路径的话会提示下面类似的错误:
log4j:ERROR Could not parse url 巴拉巴拉巴拉巴拉com.sun.org.apache.xerces.internal.util.URI$MalformedURIException: Path contains invalid character: 巴拉巴拉巴拉巴拉
因为sofarpc会读取自己jar包里面的一个log-conf.xml文件,初始化log4j,不过log4j压根不认中文路径,都会报错的。所以,项目目录不要有中文,也不要有特殊字符,或者空格,这是最省事的做法。
这个问题有点大了,各种各样的原因都有的,下面我就说说自己的一个例子以作参考。
新建一个项目,如下:
我都是直接抄网上的,还有抄官网的例子,下面是各个java文件:
package net.hello.rpc;
public interface HelloService {
public String sayHello(String name);
}
package net.hello.rpc;
public class HelloServiceImpl implements HelloService {
public String sayHello(String name) {
return "hello " + name;
}
}
package net.hello.main;
import com.alipay.sofa.rpc.config.ProviderConfig;
import com.alipay.sofa.rpc.config.RegistryConfig;
import com.alipay.sofa.rpc.config.ServerConfig;
import com.alipay.sofa.rpc.context.RpcRuntimeContext;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;
import net.hello.rpc.HelloService;
import net.hello.rpc.HelloServiceImpl;
/**
*
*
*
*
* @author GengZhang
*/
public class ZookeeperBoltServerMain {
/**
* slf4j Logger for this class
*/
private final static Logger LOGGER = LoggerFactory.getLogger(ZookeeperBoltServerMain.class);
public static void main(String[] args) {
/**
* 运行需要pom.xml里增加依赖
org.apache.curator
curator-recipes
test
*/
RegistryConfig registryConfig = new RegistryConfig()
.setProtocol("zookeeper")
.setAddress("127.0.0.1:2181");
ServerConfig serverConfig = new ServerConfig()
.setPort(22101)
.setDaemon(false);
ProviderConfig providerConfig = new ProviderConfig()
.setInterfaceId(HelloService.class.getName())
.setRef(new HelloServiceImpl())
.setServer(serverConfig)
.setRegistry(registryConfig);
providerConfig.export();
LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
}
}
package net.hello.main;
import com.alipay.sofa.rpc.config.ConsumerConfig;
import com.alipay.sofa.rpc.config.RegistryConfig;
import com.alipay.sofa.rpc.context.RpcRuntimeContext;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;
import net.hello.rpc.HelloService;
/**
*
*
*
*
* @author GengZhang
*/
public class ZookeeperBoltClientMain {
/**
* slf4j Logger for this class
*/
private final static Logger LOGGER = LoggerFactory.getLogger(ZookeeperBoltClientMain.class);
public static void main(String[] args) throws InterruptedException {
/**
* 运行需要pom.xml里增加依赖
org.apache.curator
curator-recipes
test
*/
RegistryConfig registryConfig = new RegistryConfig()
.setProtocol("zookeeper")
.setAddress("127.0.0.1:2181");
ConsumerConfig consumerConfig = new ConsumerConfig()
.setInterfaceId(HelloService.class.getName())
.setRegistry(registryConfig)
.setTimeout(3000);
HelloService helloService = consumerConfig.refer();
LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
try {
while (true) {
try {
String s = helloService.sayHello("xxx");
LOGGER.warn("{}", s);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
try {
Thread.sleep(2000);
} catch (Exception e) {
}
}
} catch (Exception e) {
LOGGER.error("", e);
}
synchronized (ZookeeperBoltClientMain.class) {
while (true) {
ZookeeperBoltClientMain.class.wait();
}
}
}
}
注意,这个项目是gradle项目,gradle文件如下:
plugins {
id 'java'
}
group 'net.w2p'
version '1.0-SNAPSHOT'
/***定义项目环境变量***/
String _ENV_="test" //默认是测试环境
if(System.getProperty("env")!=null&&!System.getProperty("env").isEmpty()){
String tmpEnv=System.getProperty("env").toLowerCase().trim();
if(!tmpEnv.equals("test")
&&!tmpEnv.equals("ppe")
&&!tmpEnv.equals("product")){
println "!!!您自定义的环境无效!环境变量只能在test、ppe、product中三选一!!";
}
else{
_ENV_=tmpEnv;
}
}
println "当前指定环境为:$_ENV_"
/***定义项目环境变量 end***/
/***rootProject配置 开始***/
/***这个配置中心是需要手工配置的。***/
ext{
AppSetting = [
//--环境变量由系统自动判断及接收,在编译时候添加 -Denv=test或者ppe或者product即可。
"env" : _ENV_,
//配置中心服务器地址
"xxl_conf_admin_address":"http://localhost:7788/",
//配置中心的密钥--注意,要与xxl-conf-admin设置的密钥一致,没有的话就留空
"xxl_conf_access_token":"",
//--配置中心会在本地建立一个镜像文件
"xxl_conf_mirrorfile":
"/home/too-white/data/applogs/xxl-conf/xxl-conf-mirror-sample.properties"
]
}
/***rootProject配置 结束***/
/***所有项目共通***/
allprojects {
sourceCompatibility = 1.8
targetCompatibility = 1.8
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'groovy'
ext{
/***常见或主要第三方依赖版本号定义 begin***/
globalSpringVersion = "5.1.4.RELEASE"
globalSpringDataJpaVersion ="2.1.2.RELEASE"
globalSpringBootVersion = '2.1.1.RELEASE'
globalFastJsonVersion="1.2.54"
globalMyBatisVersion="3.4.6"
globalMyBatisSpringVersion="1.3.2" //mybatis-spring
globalGoogleGuavaVersion="27.0.1-jre"
globalDom4jVersion="1.6.1"
globalJavaMailVersion="1.4.7"
globalJsoupVersion="1.11.3" //--一个过滤html危险字符串api,用于web安全
globalQuartzVersion="2.3.0"
globalFlexmarkVersion="0.34.32" //--java对markdown语法的解释以及翻译api
globalPostgresqlJdbcDriverVersion="42.2.5"
globalQiniuSdkVersion="7.2.18"//--七牛上传下载客户端sdk
globalApacheAntVersion="1.10.5"
globalGoogleZXingVersion="3.3.3"
globalLog4jVersion="1.2.17"
globalSlf4jVersion="1.7.25"
globalRedisClientVersion="2.10.1"
globalFreemarkerVersion="2.3.28"
globalSpringBootStaterVersionOfMyBatis="1.3.2"
globalMysqlJdbcDriverVersion="5.1.40"
globalApacheCommonLang3Version="3.8.1"
globalDruidVertion="1.1.12"
globalFastDfsClientVersion = "1.27"
globalSofaRpcVersion="5.5.0"
globalCuratorVersion="4.1.0" //java zookeeper客户端
/***常见或主要第三方依赖版本号定义 end***/
/****常见或者程序主要引用依赖定义 begin****/
//--这个是spring boot要直接compile进去的框架。
ref4SpringBoot=[
/***spring boot 相关依赖***/
"org.springframework.boot:spring-boot:$globalSpringBootVersion",
"org.springframework.boot:spring-boot-starter:$globalSpringBootVersion",
"org.springframework.boot:spring-boot-starter-web:$globalSpringBootVersion",
"org.springframework.boot:spring-boot-starter-freemarker:$globalSpringBootVersion",
"org.springframework.boot:spring-boot-devtools:$globalSpringBootVersion"
]
//--这个是spring boot要compileOnly的类库
ref4SpringBootProvided=[
"org.springframework.boot:spring-boot-dependencies:$globalSpringBootVersion",
]
//--这个是spring boot的测试框架,用testCompile导入
ref4SpringBootTest=[
"org.springframework.boot:spring-boot-starter-test:$globalSpringBootVersion"
]
//--spring框架api
ref4SpringFramework=[
"org.springframework:spring-web:$globalSpringVersion",
"org.springframework:spring-webmvc:$globalSpringVersion",
"org.springframework:spring-jdbc:$globalSpringVersion",
"org.springframework:spring-context-support:$globalSpringVersion",
"org.springframework.data:spring-data-jpa:$globalSpringDataJpaVersion",
"org.springframework:spring-test:$globalSpringVersion"
]
//--jsp&servlet等javaweb容器api,通常都用 compileOnly引用的。
ref4JspAndServletApi=[
"javax.servlet:javax.servlet-api:3.1.0",
"javax.servlet.jsp:jsp-api:2.2",
"javax.servlet.jsp.jstl:javax.servlet.jsp.jstl-api:1.2.1"
]
//--jstl等java web的tag标准api,引入的话要用compile
ref4Jstl=[
'taglibs:standard:1.1.2',
'jstl:jstl:1.2'
]
//--mybatis
ref4MyBatis=[
"org.mybatis:mybatis:$globalMyBatisVersion"
]
ref4MybatisSpring=[
"org.mybatis:mybatis-spring:$globalMyBatisSpringVersion"
]
//--这是apache common 类库引用的地址
ref4ApacheCommons = [
'commons-lang:commons-lang:2.6',
'commons-logging:commons-logging:1.2',
'commons-io:commons-io:2.5',
'commons-fileupload:commons-fileupload:1.3.2',
'commons-codec:commons-codec:1.10',
'commons-beanutils:commons-beanutils:1.9.3',
'commons-httpclient:commons-httpclient:3.1',
'org.apache.httpcomponents:fluent-hc:4.3.6',
'org.apache.httpcomponents:httpclient:4.5.3',
'org.apache.httpcomponents:httpclient-cache:4.5.3',
'org.apache.httpcomponents:httpcore:4.4.8',
'org.apache.httpcomponents:httpmime:4.5.3',
'org.jfree:jfreechart:1.0.19',
'org.apache.velocity:velocity:1.7',
'org.apache.poi:poi:3.16'
]
//--redis client
ref4RedisClient=["redis.clients:jedis:$globalRedisClientVersion"]
ref4Freemarker=["org.freemarker:freemarker:$globalFreemarkerVersion"]
//--这是阿里云短信引用的第三方类库
ref4AliYunSms=[
'com.aliyun:aliyun-java-sdk-core:3.2.8',
'com.aliyun:aliyun-java-sdk-dysmsapi:1.1.0'
]
//--阿里云图片裁剪
ref4AliSimpleImage=[
'com.alibaba:simpleimage:1.2.3'
]
//--阿里fast json引用地址
ref4FastJson=["com.alibaba:fastjson:$globalFastJsonVersion"]
//--json-lib引用地址
ref4JsonLib=["net.sf.json-lib:json-lib:2.4:jdk15"]
//--jdom1&jdom2以及相关api
ref4Jdom=[
'org.jdom:jdom2:2.0.6',
'org.jdom:jdom:1.1.3',
'joda-time:joda-time:2.9.7'
]
//--google guava
ref4GoogleGuava=["com.google.guava:guava:$globalGoogleGuavaVersion"]
//--dom4j
ref4Dom4j=["dom4j:dom4j:$globalDom4jVersion"]
ref4JavaMail=["javax.mail:mail:$globalJavaMailVersion"]
ref4Jsoup=["org.jsoup:jsoup:$globalJsoupVersion"]
ref4Quartz=[
"org.quartz-scheduler:quartz:$globalQuartzVersion",
"org.quartz-scheduler:quartz-jobs:$globalQuartzVersion"
]
ref4Flexmark=[
"com.vladsch.flexmark:flexmark-all:$globalFlexmarkVersion"
]
ref4PostgresqlJdbcDriver=[
"org.postgresql:postgresql:$globalPostgresqlJdbcDriverVersion"
]
ref4QiuniuSdkVersion=[
"com.qiniu:qiniu-java-sdk:$globalQiniuSdkVersion"
]
ref4ApacheAnt=["org.apache.ant:ant:$globalApacheAntVersion"]
//--二维码
ref4ZXing=[
"com.google.zxing:core:$globalGoogleZXingVersion",
"com.google.zxing:javase:$globalGoogleZXingVersion"
]
ref4Log4j=["log4j:log4j:$globalLog4jVersion"]
ref4Slf4jToLog4j=["org.slf4j:slf4j-log4j12:$globalSlf4jVersion"]
ref4Druid=["com.alibaba:druid:$globalDruidVertion"]
ref4FastdfsClient=["cn.bestwu:fastdfs-client-java:$globalFastDfsClientVersion"]
ref4SofaRpc=["com.alipay.sofa:sofa-rpc-all:$globalSofaRpcVersion"]
//curator--zk客户端导入
ref4Curator=["org.apache.curator:curator-framework:$globalCuratorVersion","org.apache.curator:curator-recipes:$globalCuratorVersion"]
/****常见或者程序主要引用依赖定义 end****/
}
idea {
module {
inheritOutputDirs = true
}
}
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
tasks.withType(GroovyCompile) {
groovyOptions.encoding = "MacRoman"
}
repositories {
maven{
//更换为阿里的仓库
url 'http://maven.aliyun.com/nexus/content/groups/public'
}
//有些jar包在中央仓库是没有的,需要手动添加上去
// flatDir { dirs 'local_jars' }
// mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
/****自定义全局任务 begin****/
task compileConfig{
}
compileConfig << {
println "正在编译替换配置中心的配置文件,请稍后......."
println "根项目目录为:$rootProject.rootDir"
println "当前模块目录为:$project.projectDir"
/***将配置文件复制到子模块的对应位置--当然,会替换掉相关变量****/
copy {
from "${rootProject.rootDir}/conf/"
into "${project.projectDir}/src/main/resources/conf"
filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: rootProject.AppSetting)
}
}
/****自定义全局任务 end****/
}
dependencies{
//--redis
compile ref4RedisClient
//【http相关api】
compileOnly ref4JspAndServletApi
compile ref4Jstl
//【spring 框架】
compile ref4SpringFramework
//【mybatis】
compile ref4MyBatis
compile ref4MybatisSpring
//【apache commons】
compile ref4ApacheCommons
//--sofarpc 相关
compile ref4SofaRpc
compile ref4Curator
compile ref4Slf4jToLog4j
}
解释一下,sofarpc用的版本是5.5.0,而zk客户端用的版本是:
4.1.0
运行zookeeperBoltServerMain:
没有任何报错。。额,估计是发布任务成功了。美滋滋,下面用命令行连接zk,看看zk的节点有没有添加到:
用zk做注册中心最后都是要往里面插入节点的,没有插入的话就表示连接不上或者压根没注册服务,简单的命令用法请看:
ZooKeeper系列3:ZooKeeper命令、命令行工具及简单操作
下面直接操作。
心凉了半截。。。节点只有zookeeper。。这个估计不会是sofarpc添加的。。。也就是sofarpc没有添加节点。
执行客户端:
一直卡住,死也不说什么错。。停止了也没报错。。。麻烦了。。
不停尝试,不过,在偶然情况下看到了日志记录的痕迹:
这家伙原来都不输出错误,将错误全部都放到账号下面的logs文件夹里面!!!
一查:
果然有。。。。然后逐个逐个打开文件看看,欸,发现问题了:
2019-01-28 12:54:59,005 INFO main - Welcome! Loading SOFA RPC Framework : 5.5.0_20181229155045, PID is:6456
2019-01-28 12:54:59,025 INFO main - Install Module: fault-tolerance
2019-01-28 12:54:59,042 INFO main - Install Module: sofaTracer-resteasy
2019-01-28 12:54:59,096 INFO main - The module lookout does not need to be loaded.
2019-01-28 12:54:59,096 INFO main - Install Module: sofaTracer
2019-01-28 12:54:59,109 INFO main - Export provider config : net.hello.rpc.HelloService::bolt with bean id rpc-cfg-0
2019-01-28 12:54:59,500 WARN main - Catch exception when register to registry: rpc-cfg-1
java.lang.NoSuchMethodError: org.apache.curator.framework.api.CreateBuilder.creatingParentContainersIfNeeded()Lorg/apache/curator/framework/api/ProtectACLCreateModePathAndBytesable;
at com.alipay.sofa.rpc.registry.zk.ZookeeperRegistry.registerProviderUrls(ZookeeperRegistry.java:373)
at com.alipay.sofa.rpc.registry.zk.ZookeeperRegistry.register(ZookeeperRegistry.java:330)
at com.alipay.sofa.rpc.bootstrap.DefaultProviderBootstrap.register(DefaultProviderBootstrap.java:346)
at com.alipay.sofa.rpc.bootstrap.DefaultProviderBootstrap.doExport(DefaultProviderBootstrap.java:192)
at com.alipay.sofa.rpc.bootstrap.DefaultProviderBootstrap.export(DefaultProviderBootstrap.java:106)
at com.alipay.sofa.rpc.config.ProviderConfig.export(ProviderConfig.java:524)
at net.hello.main.ZookeeperBoltServerMain.main(ZookeeperBoltServerMain.java:52)
2019-01-28 12:54:59,502 WARN main - started at pid 6456
2019-01-28 13:00:36,731 INFO main - Welcome! Loading SOFA RPC Framework : 5.5.0_20181229155045, PID is:7454
2019-01-28 13:00:36,770 INFO main - Install Module: fault-tolerance
2019-01-28 13:00:36,791 INFO main - Install Module: sofaTracer-resteasy
2019-01-28 13:00:36,842 INFO main - The module lookout does not need to be loaded.
2019-01-28 13:00:36,842 INFO main - Install Module: sofaTracer
2019-01-28 13:00:36,851 INFO main - Refer consumer config : bolt://net.hello.rpc.HelloService: with bean id rpc-cfg-0
2019-01-28 13:00:37,013 WARN main - Catch exception when subscribe from registry: rpc-cfg-1, but you can ignore if it's called by JVM shutdown hook
java.lang.NoSuchMethodError: org.apache.curator.framework.api.CreateBuilder.creatingParentContainersIfNeeded()Lorg/apache/curator/framework/api/ProtectACLCreateModePathAndBytesable;
at com.alipay.sofa.rpc.registry.zk.ZookeeperRegistry.subscribeConsumerUrls(ZookeeperRegistry.java:656)
at com.alipay.sofa.rpc.registry.zk.ZookeeperRegistry.subscribe(ZookeeperRegistry.java:561)
at com.alipay.sofa.rpc.bootstrap.DefaultConsumerBootstrap.subscribeFromRegistries(DefaultConsumerBootstrap.java:338)
at com.alipay.sofa.rpc.bootstrap.DefaultConsumerBootstrap.subscribe(DefaultConsumerBootstrap.java:261)
at com.alipay.sofa.rpc.client.AbstractCluster.init(AbstractCluster.java:144)
at com.alipay.sofa.rpc.bootstrap.DefaultConsumerBootstrap.refer(DefaultConsumerBootstrap.java:152)
at com.alipay.sofa.rpc.config.ConsumerConfig.refer(ConsumerConfig.java:926)
at net.hello.main.ZookeeperBoltClientMain.main(ZookeeperBoltClientMain.java:42)
2019-01-28 13:01:07,126 WARN main - started at pid 7454
2019-01-28 13:01:07,127 INFO main - Load tracer impl success: sofaTracer, com.alipay.sofa.rpc.tracer.sofatracer.RpcSofaTracer@6a192cfe
2019-01-28 13:01:07,189 ERROR main - RPC-02306: 没有获得服务[net.hello.rpc.HelloService:1.0]的调用地址,请检查服务是否已经推送
com.alipay.sofa.rpc.core.exception.SofaRouteException: RPC-02306: 没有获得服务[net.hello.rpc.HelloService:1.0]的调用地址,请检查服务是否已经推送
at com.alipay.sofa.rpc.client.AbstractCluster.noAvailableProviderException(AbstractCluster.java:423)
at com.alipay.sofa.rpc.client.AbstractCluster.select(AbstractCluster.java:358)
at com.alipay.sofa.rpc.client.FailoverCluster.doInvoke(FailoverCluster.java:64)
at com.alipay.sofa.rpc.client.AbstractCluster.invoke(AbstractCluster.java:285)
at com.alipay.sofa.rpc.client.ClientProxyInvoker.invoke(ClientProxyInvoker.java:83)
at net.hello.rpc.HelloService_proxy_0.sayHello(HelloService_proxy_0.java)
at net.hello.main.ZookeeperBoltClientMain.main(ZookeeperBoltClientMain.java:51)
2019-01-28 13:01:09,194 ERROR main - RPC-02306: 没有获得服务[net.hello.rpc.HelloService:1.0]的调用地址,请检查服务是否已经推送
com.alipay.sofa.rpc.core.exception.SofaRouteException: RPC-02306: 没有获得服务[net.hello.rpc.HelloService:1.0]的调用地址,请检查服务是否已经推送
at com.alipay.sofa.rpc.client.AbstractCluster.noAvailableProviderException(AbstractCluster.java:423)
at com.alipay.sofa.rpc.client.AbstractCluster.select(AbstractCluster.java:358)
at com.alipay.sofa.rpc.client.FailoverCluster.doInvoke(FailoverCluster.java:64)
at com.alipay.sofa.rpc.client.AbstractCluster.invoke(AbstractCluster.java:285)
at com.alipay.sofa.rpc.client.ClientProxyInvoker.invoke(ClientProxyInvoker.java:83)
at net.hello.rpc.HelloService_proxy_0.sayHello(HelloService_proxy_0.java)
at net.hello.main.ZookeeperBoltClientMain.main(ZookeeperBoltClientMain.java:51)
2019-01-28 13:01:11,199 ERROR main - RPC-02306: 没有获得服务[net.hello.rpc.HelloService:1.0]的调用地址,请检查服务是否已经推送
com.alipay.sofa.rpc.core.exception.SofaRouteException: RPC-02306: 没有获得服务[net.hello.rpc.HelloService:1.0]的调用地址,请检查服务是否已经推送
at com.alipay.sofa.rpc.client.AbstractCluster.noAvailableProviderException(AbstractCluster.java:423)
at com.alipay.sofa.rpc.client.AbstractCluster.select(AbstractCluster.java:358)
at com.alipay.sofa.rpc.client.FailoverCluster.doInvoke(FailoverCluster.java:64)
at com.alipay.sofa.rpc.client.AbstractCluster.invoke(AbstractCluster.java:285)
at com.alipay.sofa.rpc.client.ClientProxyInvoker.invoke(ClientProxyInvoker.java:83)
at net.hello.rpc.HelloService_proxy_0.sayHello(HelloService_proxy_0.java)
at net.hello.main.ZookeeperBoltClientMain.main(ZookeeperBoltClientMain.java:51)
2019-01-28 13:01:13,204 ERROR main - RPC-02306: 没有获得服务[net.hello.rpc.HelloService:1.0]的调用地址,请检查服务是否已经推送
com.alipay.sofa.rpc.core.exception.SofaRouteException: RPC-02306: 没有获得服务[net.hello.rpc.HelloService:1.0]的调用地址,请检查服务是否已经推送
最重要的错误是:
java.lang.NoSuchMethodError: org.apache.curator.framework.api.CreateBuilder.creatingParentContainersIfNeeded()Lorg/apache/curator/framework/api/ProtectACLCreateModePathAndBytesable;
at com.alipay.sofa.rpc.registry.zk.ZookeeperRegistry.registerProviderUrls(ZookeeperRegistry.java:373)
at com.alipay.sofa.rpc.registry.zk.ZookeeperRegistry.register(ZookeeperRegistry.java:330)
at com.alipay.sofa.rpc.bootstrap.DefaultProviderBootstrap.register(DefaultProviderBootstrap.java:346)
at com.alipay.sofa.rpc.bootstrap.DefaultProviderBootstrap.doExport(DefaultProviderBootstrap.java:192)
at com.alipay.sofa.rpc.bootstrap.DefaultProviderBootstrap.export(DefaultProviderBootstrap.java:106)
at com.alipay.sofa.rpc.config.ProviderConfig.export(ProviderConfig.java:524)
at net.hello.main.ZookeeperBoltServerMain.main(ZookeeperBoltServerMain.java:52)
意思是curator里面没有需要的方法—额,估计是版本冲突了,我引入的curator是最新的。。。sofarpc不支持最新的!
没有给出curator版本号啊。。。。难怪我会找最新的了,因为没给出来。。。
查找缺失的方法,可以看到有同仁用过这方法了:
Zookeeper客户端Curator使用详解
用的是2.12.0,查查官网,这个版本是不是很旧了:
欸???这些版本号都不是版本越大时间越新的,看来误解了,各个大版本应该是对应不同的端的。。
版本2一直迭代,最新的是2.13.0,好了,改改:
zk上用命令查看节点:
好了,已经可以运行了。不过,有点bug还要跟进,因为日志又有错了:
这次是fastjson错误:
2019-01-28 14:02:25,964 ERROR main - Error when load extension of extensible interface com.alipay.sofa.rpc.codec.Serializer with alias: json.
com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException: create com.alipay.sofa.rpc.codec.jackson.JacksonSerializer instance error
at com.alipay.sofa.rpc.ext.ExtensionClass.getExtInstance(ExtensionClass.java:115)
at com.alipay.sofa.rpc.ext.ExtensionClass.getExtInstance(ExtensionClass.java:89)
at com.alipay.sofa.rpc.codec.SerializerFactory$1.onLoad(SerializerFactory.java:58)
at com.alipay.sofa.rpc.ext.ExtensionLoader.loadSuccess(ExtensionLoader.java:359)
at com.alipay.sofa.rpc.ext.ExtensionLoader.readLine(ExtensionLoader.java:342)
at com.alipay.sofa.rpc.ext.ExtensionLoader.loadFromClassLoader(ExtensionLoader.java:184)
at com.alipay.sofa.rpc.ext.ExtensionLoader.loadFromFile(ExtensionLoader.java:158)
at com.alipay.sofa.rpc.ext.ExtensionLoader.(ExtensionLoader.java:141)
at com.alipay.sofa.rpc.ext.ExtensionLoader.(ExtensionLoader.java:91)
at com.alipay.sofa.rpc.ext.ExtensionLoaderFactory.getExtensionLoader(ExtensionLoaderFactory.java:50)
at com.alipay.sofa.rpc.codec.SerializerFactory.buildLoader(SerializerFactory.java:53)
at com.alipay.sofa.rpc.codec.SerializerFactory.(SerializerFactory.java:50)
at com.alipay.sofa.rpc.codec.bolt.SofaRpcSerialization.serializeContent(SofaRpcSerialization.java:200)
at com.alipay.remoting.rpc.protocol.RpcRequestCommand.serializeContent(RpcRequestCommand.java:124)
at com.alipay.remoting.rpc.RpcCommand.serialize(RpcCommand.java:105)
at com.alipay.remoting.rpc.RpcRemoting.toRemotingCommand(RpcRemoting.java:354)
at com.alipay.remoting.rpc.RpcRemoting.invokeSync(RpcRemoting.java:179)
at com.alipay.remoting.rpc.RpcClientRemoting.invokeSync(RpcClientRemoting.java:67)
at com.alipay.remoting.rpc.RpcClient.invokeSync(RpcClient.java:350)
at com.alipay.sofa.rpc.transport.bolt.BoltClientTransport.doInvokeSync(BoltClientTransport.java:293)
at com.alipay.sofa.rpc.transport.bolt.BoltClientTransport.syncSend(BoltClientTransport.java:266)
at com.alipay.sofa.rpc.client.AbstractCluster.doSendMsg(AbstractCluster.java:514)
at com.alipay.sofa.rpc.client.AbstractCluster.sendMsg(AbstractCluster.java:485)
at com.alipay.sofa.rpc.filter.ConsumerInvoker.invoke(ConsumerInvoker.java:60)
at com.alipay.sofa.rpc.filter.sofatracer.ConsumerTracerFilter.invoke(ConsumerTracerFilter.java:66)
at com.alipay.sofa.rpc.filter.FilterInvoker.invoke(FilterInvoker.java:96)
at com.alipay.sofa.rpc.filter.RpcReferenceContextFilter.invoke(RpcReferenceContextFilter.java:80)
at com.alipay.sofa.rpc.filter.FilterInvoker.invoke(FilterInvoker.java:96)
at com.alipay.sofa.rpc.filter.ConsumerExceptionFilter.invoke(ConsumerExceptionFilter.java:37)
at com.alipay.sofa.rpc.filter.FilterInvoker.invoke(FilterInvoker.java:96)
at com.alipay.sofa.rpc.filter.FilterChain.invoke(FilterChain.java:262)
at com.alipay.sofa.rpc.client.AbstractCluster.filterChain(AbstractCluster.java:478)
at com.alipay.sofa.rpc.client.FailoverCluster.doInvoke(FailoverCluster.java:66)
at com.alipay.sofa.rpc.client.AbstractCluster.invoke(AbstractCluster.java:285)
at com.alipay.sofa.rpc.client.ClientProxyInvoker.invoke(ClientProxyInvoker.java:83)
at net.hello.rpc.HelloService_proxy_0.sayHello(HelloService_proxy_0.java)
at net.hello.main.ZookeeperBoltClientMain.main(ZookeeperBoltClientMain.java:51)
Caused by: com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException: com/fasterxml/jackson/core/JsonProcessingException
at com.alipay.sofa.rpc.common.utils.ClassUtils.newInstance(ClassUtils.java:181)
at com.alipay.sofa.rpc.common.utils.ClassUtils.newInstanceWithArgs(ClassUtils.java:198)
at com.alipay.sofa.rpc.ext.ExtensionClass.getExtInstance(ExtensionClass.java:106)
... 36 more
Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
at java.lang.Class.getConstructor0(Class.java:3075)
at java.lang.Class.getDeclaredConstructor(Class.java:2178)
at com.alipay.sofa.rpc.common.utils.ClassUtils.newInstance(ClassUtils.java:147)
... 38 more
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonProcessingException
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 43 more
2019-01-28 14:02:26,101 WARN main - hello xxx
找不到类:
Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException
也是醉了。。。
it同仁也遇到过:
好了,查了maven最新版本为2.9.8,下面在build.gradle上面添加依赖:
就是说,最后的build.gradle如下:
plugins {
id 'java'
}
group 'net.w2p'
version '1.0-SNAPSHOT'
/***定义项目环境变量***/
String _ENV_="test" //默认是测试环境
if(System.getProperty("env")!=null&&!System.getProperty("env").isEmpty()){
String tmpEnv=System.getProperty("env").toLowerCase().trim();
if(!tmpEnv.equals("test")
&&!tmpEnv.equals("ppe")
&&!tmpEnv.equals("product")){
println "!!!您自定义的环境无效!环境变量只能在test、ppe、product中三选一!!";
}
else{
_ENV_=tmpEnv;
}
}
println "当前指定环境为:$_ENV_"
/***定义项目环境变量 end***/
/***rootProject配置 开始***/
/***这个配置中心是需要手工配置的。***/
ext{
AppSetting = [
//--环境变量由系统自动判断及接收,在编译时候添加 -Denv=test或者ppe或者product即可。
"env" : _ENV_,
//配置中心服务器地址
"xxl_conf_admin_address":"http://localhost:7788/",
//配置中心的密钥--注意,要与xxl-conf-admin设置的密钥一致,没有的话就留空
"xxl_conf_access_token":"",
//--配置中心会在本地建立一个镜像文件
"xxl_conf_mirrorfile":
"/home/too-white/data/applogs/xxl-conf/xxl-conf-mirror-sample.properties"
]
}
/***rootProject配置 结束***/
/***所有项目共通***/
allprojects {
sourceCompatibility = 1.8
targetCompatibility = 1.8
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'groovy'
ext{
/***常见或主要第三方依赖版本号定义 begin***/
globalSpringVersion = "5.1.4.RELEASE"
globalSpringDataJpaVersion ="2.1.2.RELEASE"
globalSpringBootVersion = '2.1.1.RELEASE'
globalFastJsonVersion="1.2.54"
globalMyBatisVersion="3.4.6"
globalMyBatisSpringVersion="1.3.2" //mybatis-spring
globalGoogleGuavaVersion="27.0.1-jre"
globalDom4jVersion="1.6.1"
globalJavaMailVersion="1.4.7"
globalJsoupVersion="1.11.3" //--一个过滤html危险字符串api,用于web安全
globalQuartzVersion="2.3.0"
globalFlexmarkVersion="0.34.32" //--java对markdown语法的解释以及翻译api
globalPostgresqlJdbcDriverVersion="42.2.5"
globalQiniuSdkVersion="7.2.18"//--七牛上传下载客户端sdk
globalApacheAntVersion="1.10.5"
globalGoogleZXingVersion="3.3.3"
globalLog4jVersion="1.2.17"
globalSlf4jVersion="1.7.25"
globalRedisClientVersion="2.10.1"
globalFreemarkerVersion="2.3.28"
globalSpringBootStaterVersionOfMyBatis="1.3.2"
globalMysqlJdbcDriverVersion="5.1.40"
globalApacheCommonLang3Version="3.8.1"
globalDruidVertion="1.1.12"
globalFastDfsClientVersion = "1.27"
globalSofaRpcVersion="5.5.0"
globalCuratorVersion="2.13.0" //java zookeeper客户端---curator用版本2的就好了,否则sofarpc不认的。
globalJacksonVersion="2.9.8"
/***常见或主要第三方依赖版本号定义 end***/
/****常见或者程序主要引用依赖定义 begin****/
//--这个是spring boot要直接compile进去的框架。
ref4SpringBoot=[
/***spring boot 相关依赖***/
"org.springframework.boot:spring-boot:$globalSpringBootVersion",
"org.springframework.boot:spring-boot-starter:$globalSpringBootVersion",
"org.springframework.boot:spring-boot-starter-web:$globalSpringBootVersion",
"org.springframework.boot:spring-boot-starter-freemarker:$globalSpringBootVersion",
"org.springframework.boot:spring-boot-devtools:$globalSpringBootVersion"
]
//--这个是spring boot要compileOnly的类库
ref4SpringBootProvided=[
"org.springframework.boot:spring-boot-dependencies:$globalSpringBootVersion",
]
//--这个是spring boot的测试框架,用testCompile导入
ref4SpringBootTest=[
"org.springframework.boot:spring-boot-starter-test:$globalSpringBootVersion"
]
//--spring框架api
ref4SpringFramework=[
"org.springframework:spring-web:$globalSpringVersion",
"org.springframework:spring-webmvc:$globalSpringVersion",
"org.springframework:spring-jdbc:$globalSpringVersion",
"org.springframework:spring-context-support:$globalSpringVersion",
"org.springframework.data:spring-data-jpa:$globalSpringDataJpaVersion",
"org.springframework:spring-test:$globalSpringVersion"
]
//--jsp&servlet等javaweb容器api,通常都用 compileOnly引用的。
ref4JspAndServletApi=[
"javax.servlet:javax.servlet-api:3.1.0",
"javax.servlet.jsp:jsp-api:2.2",
"javax.servlet.jsp.jstl:javax.servlet.jsp.jstl-api:1.2.1"
]
//--jstl等java web的tag标准api,引入的话要用compile
ref4Jstl=[
'taglibs:standard:1.1.2',
'jstl:jstl:1.2'
]
//--mybatis
ref4MyBatis=[
"org.mybatis:mybatis:$globalMyBatisVersion"
]
ref4MybatisSpring=[
"org.mybatis:mybatis-spring:$globalMyBatisSpringVersion"
]
//--这是apache common 类库引用的地址
ref4ApacheCommons = [
'commons-lang:commons-lang:2.6',
'commons-logging:commons-logging:1.2',
'commons-io:commons-io:2.5',
'commons-fileupload:commons-fileupload:1.3.2',
'commons-codec:commons-codec:1.10',
'commons-beanutils:commons-beanutils:1.9.3',
'commons-httpclient:commons-httpclient:3.1',
'org.apache.httpcomponents:fluent-hc:4.3.6',
'org.apache.httpcomponents:httpclient:4.5.3',
'org.apache.httpcomponents:httpclient-cache:4.5.3',
'org.apache.httpcomponents:httpcore:4.4.8',
'org.apache.httpcomponents:httpmime:4.5.3',
'org.jfree:jfreechart:1.0.19',
'org.apache.velocity:velocity:1.7',
'org.apache.poi:poi:3.16'
]
//--redis client
ref4RedisClient=["redis.clients:jedis:$globalRedisClientVersion"]
ref4Freemarker=["org.freemarker:freemarker:$globalFreemarkerVersion"]
//--这是阿里云短信引用的第三方类库
ref4AliYunSms=[
'com.aliyun:aliyun-java-sdk-core:3.2.8',
'com.aliyun:aliyun-java-sdk-dysmsapi:1.1.0'
]
//--阿里云图片裁剪
ref4AliSimpleImage=[
'com.alibaba:simpleimage:1.2.3'
]
//--阿里fast json引用地址
ref4FastJson=["com.alibaba:fastjson:$globalFastJsonVersion"]
//--json-lib引用地址
ref4JsonLib=["net.sf.json-lib:json-lib:2.4:jdk15"]
//--jdom1&jdom2以及相关api
ref4Jdom=[
'org.jdom:jdom2:2.0.6',
'org.jdom:jdom:1.1.3',
'joda-time:joda-time:2.9.7'
]
//--google guava
ref4GoogleGuava=["com.google.guava:guava:$globalGoogleGuavaVersion"]
//--dom4j
ref4Dom4j=["dom4j:dom4j:$globalDom4jVersion"]
ref4JavaMail=["javax.mail:mail:$globalJavaMailVersion"]
ref4Jsoup=["org.jsoup:jsoup:$globalJsoupVersion"]
ref4Quartz=[
"org.quartz-scheduler:quartz:$globalQuartzVersion",
"org.quartz-scheduler:quartz-jobs:$globalQuartzVersion"
]
ref4Flexmark=[
"com.vladsch.flexmark:flexmark-all:$globalFlexmarkVersion"
]
ref4PostgresqlJdbcDriver=[
"org.postgresql:postgresql:$globalPostgresqlJdbcDriverVersion"
]
ref4QiuniuSdkVersion=[
"com.qiniu:qiniu-java-sdk:$globalQiniuSdkVersion"
]
ref4ApacheAnt=["org.apache.ant:ant:$globalApacheAntVersion"]
//--二维码
ref4ZXing=[
"com.google.zxing:core:$globalGoogleZXingVersion",
"com.google.zxing:javase:$globalGoogleZXingVersion"
]
ref4Log4j=["log4j:log4j:$globalLog4jVersion"]
ref4Slf4jToLog4j=["org.slf4j:slf4j-log4j12:$globalSlf4jVersion"]
ref4Druid=["com.alibaba:druid:$globalDruidVertion"]
ref4FastdfsClient=["cn.bestwu:fastdfs-client-java:$globalFastDfsClientVersion"]
ref4SofaRpc=["com.alipay.sofa:sofa-rpc-all:$globalSofaRpcVersion"]
//curator--zk客户端导入
ref4Curator=["org.apache.curator:curator-framework:$globalCuratorVersion",
"org.apache.curator:curator-recipes:$globalCuratorVersion"]
ref4Jackson=["com.fasterxml.jackson.core:jackson-core:$globalJacksonVersion"
,"com.fasterxml.jackson.core:jackson-databind:$globalJacksonVersion"
,"com.fasterxml.jackson.core:jackson-annotations:$globalJacksonVersion"]
/****常见或者程序主要引用依赖定义 end****/
}
idea {
module {
inheritOutputDirs = true
}
}
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
tasks.withType(GroovyCompile) {
groovyOptions.encoding = "MacRoman"
}
repositories {
maven{
//更换为阿里的仓库
url 'http://maven.aliyun.com/nexus/content/groups/public'
}
//有些jar包在中央仓库是没有的,需要手动添加上去
// flatDir { dirs 'local_jars' }
// mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
/****自定义全局任务 begin****/
task compileConfig{
}
compileConfig << {
println "正在编译替换配置中心的配置文件,请稍后......."
println "根项目目录为:$rootProject.rootDir"
println "当前模块目录为:$project.projectDir"
/***将配置文件复制到子模块的对应位置--当然,会替换掉相关变量****/
copy {
from "${rootProject.rootDir}/conf/"
into "${project.projectDir}/src/main/resources/conf"
filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: rootProject.AppSetting)
}
}
/****自定义全局任务 end****/
}
dependencies{
//--redis
compile ref4RedisClient
//【http相关api】
compileOnly ref4JspAndServletApi
compile ref4Jstl
//【spring 框架】
compile ref4SpringFramework
//【mybatis】
compile ref4MyBatis
compile ref4MybatisSpring
//【apache commons】
compile ref4ApacheCommons
//--sofarpc 相关
compile ref4SofaRpc
compile ref4Curator
compile ref4Slf4jToLog4j
compile ref4Jackson
}
好了,终于完全调通了,没有问题。
想不到一些小小的整合都要话费这么多心思。。
网上那么多资料这么多例子都检查过没出过问题吗?