我没有用Spring,裸API Config写的
redis已经搭好可用 jedis 2.0包已经引了
provider端:
package soa.service;
public interface PlayerService {
String queryById(long id);
}
package soa.service;
public class PlayerServiceImpl implements PlayerService {
public String queryById(long id) {
return "FUCK";
}
}
public class ServiceInitiator {
private static ServiceConfig<PlayerService> service = new ServiceConfig<PlayerService>(); // 该类很重,封装了与注册中心的连接,请自行缓存,否则可能造成内存和连接泄漏
public static void listen() {
PlayerService playerService = new PlayerServiceImpl();
// 当前应用配置
ApplicationConfig application = new ApplicationConfig();
application.setName("center");
// 连接注册中心配置
RegistryConfig registry = new RegistryConfig();
registry.setAddress("redis://192.168.1.11:6379");
// 服务提供者协议配置
ProtocolConfig protocol = new ProtocolConfig();
protocol.setName("dubbo");
protocol.setPort(20880);
// protocol.setSerialization("json");
protocol.setThreads(200);
// 注意:ServiceConfig为重对象,内部封装了与注册中心的连接,以及开启服务端口
// 服务提供者暴露服务配置
service.setApplication(application);
service.setRegistry(registry); // 多个注册中心可以用setRegistries()
service.setProtocol(protocol); // 多个协议可以用setProtocols()
service.setInterface(PlayerService.class);
service.setRef(playerService);
service.setVersion("1.0.0");
// 暴露及注册服务
service.export();
}
}
ServiceInitiator的listen启动正常,日志也表示连接上了redis,并成功注册。
[2015-12-03 11:32:37,132] INFO [play-thread-1] com.alibaba.dubbo.remoting.transport.AbstractServer.<init>(AbstractServer.java:69) - [DUBBO] Start NettyServer bind /0.0.0.0:20880, export /192.168.1.99:20880, dubbo version: 2.4.9, current host: 127.0.0.1
[2015-12-03 11:32:37,141] INFO [play-thread-1] com.alibaba.dubbo.registry.support.AbstractRegistry.loadProperties(AbstractRegistry.java:232) - [DUBBO] Load registry store file /Users/tr0j4n/.dubbo/dubbo-registry-192.168.1.11.cache, data: {soa.service.PlayerService:1.0.0=empty://0.0.0.0/soa.service.PlayerService?application=yyy&category=providers&connected=true&dubbo=2.4.9&interface=soa.service.PlayerService&methods=queryById&pid=813&revision=1.0.0&side=consumer×tamp=1449113509365&version=1.0.0}, dubbo version: 2.4.9, current host: 127.0.0.1
[2015-12-03 11:32:37,151] INFO [play-thread-1] com.alibaba.dubbo.registry.support.AbstractRegistry.register(AbstractRegistry.java:302) - [DUBBO] Register: dubbo://192.168.1.99:20880/soa.service.PlayerService?anyhost=true&application=center&dubbo=2.4.9&interface=soa.service.PlayerService&methods=queryById&pid=810&revision=1.0.0&side=provider&threads=200×tamp=1449113556952&version=1.0.0, dubbo version: 2.4.9, current host: 127.0.0.1
[2015-12-03 11:32:37,165] INFO [play-thread-1] com.alibaba.dubbo.registry.support.AbstractRegistry.subscribe(AbstractRegistry.java:325) - [DUBBO] Subscribe: provider://192.168.1.99:20880/soa.service.PlayerService?anyhost=true&application=center&category=configurators&check=false&dubbo=2.4.9&interface=soa.service.PlayerService&methods=queryById&pid=810&revision=1.0.0&side=provider&threads=200×tamp=1449113556952&version=1.0.0, dubbo version: 2.4.9, current host: 127.0.0.1
接下来我在本机的另外一个Consumer应用里创建了PlayerService的接口存根,调用方式如文档所述
consumer端:
package soa.service;
public interface PlayerService {
String queryById(long id);
}
public class ServiceConsumer {
public static void test(){
// 当前应用配置
ApplicationConfig application = new ApplicationConfig();
application.setName("yyy");
// 连接注册中心配置
RegistryConfig registry = new RegistryConfig();
registry.setAddress("redis://192.168.1.11:6379");
// registry.setUsername("admin");
// registry.setPassword("123456");
// 注意:ReferenceConfig为重对象,内部封装了与注册中心的连接,以及与服务提供方的连接
// 引用远程服务
ReferenceConfig<PlayerService> reference = new ReferenceConfig<PlayerService>(); // 该类很重,封装了与注册中心的连接以及与提供者的连接,请自行缓存,否则可能造成内存和连接泄漏
reference.setApplication(application);
reference.setRegistry(registry); // 多个注册中心可以用setRegistries()
reference.setInterface(PlayerService.class);
reference.setVersion("1.0.0");
// 和本地bean一样使用xxxService
PlayerService xxxService = reference.get(); // 注意:此代理对象内部封装了所有通讯细节,对象较重,请缓存复用
String p = xxxService.queryById(25L);
System.out.print(p.toString());
}
}
调用时,provider端爆出了这样的错误:
[2015-12-03 11:32:49,276] INFO [DubboRedisSubscribe] com.alibaba.dubbo.registry.redis.RedisRegistry$NotifySub.onMessage(RedisRegistry.java:483) - [DUBBO] redis event: /dubbo/soa.service.PlayerService/consumers = register, dubbo version: 2.4.9, current host: 127.0.0.1
[2015-12-03 11:32:55,103] DEBUG [DubboServerHandler-192.168.1.99:20880-thread-2] com.alibaba.dubbo.remoting.transport.DecodeHandler.decode(DecodeHandler.java:60) - [DUBBO] Decode decodeable message com.alibaba.dubbo.rpc.protocol.dubbo.DecodeableRpcInvocation, dubbo version: 2.4.9, current host: 127.0.0.1
[2015-12-03 11:32:55,106] ERROR [DubboServerHandler-192.168.1.99:20880-thread-2] com.alibaba.dubbo.rpc.filter.ExceptionFilter.invoke(ExceptionFilter.java:118) - [DUBBO] Got unchecked and undeclared exception which called by 192.168.1.99. service: soa.service.PlayerService, method: queryById, exception: com.alibaba.dubbo.rpc.RpcException: Failed to invoke remote proxy method queryById to registry://192.168.1.11:6379/com.alibaba.dubbo.registry.RegistryService?application=center&dubbo=2.4.9&export=dubbo%3A%2F%2F192.168.1.99%3A20880%2Fsoa.service.PlayerService%3Fanyhost%3Dtrue%26application%3Dcenter%26dubbo%3D2.4.9%26interface%3Dsoa.service.PlayerService%26methods%3DqueryById%26pid%3D810%26revision%3D1.0.0%26side%3Dprovider%26threads%3D200%26timestamp%3D1449113556952%26version%3D1.0.0&pid=810®istry=redis×tamp=1449113556942, cause: java.lang.ClassCastException: soa.service.PlayerServiceImpl cannot be cast to soa.service.PlayerServiceImpl, dubbo version: 2.4.9, current host: 127.0.0.1
com.alibaba.dubbo.rpc.RpcException: Failed to invoke remote proxy method queryById to registry://192.168.1.11:6379/com.alibaba.dubbo.registry.RegistryService?application=center&dubbo=2.4.9&export=dubbo%3A%2F%2F192.168.1.99%3A20880%2Fsoa.service.PlayerService%3Fanyhost%3Dtrue%26application%3Dcenter%26dubbo%3D2.4.9%26interface%3Dsoa.service.PlayerService%26methods%3DqueryById%26pid%3D810%26revision%3D1.0.0%26side%3Dprovider%26threads%3D200%26timestamp%3D1449113556952%26version%3D1.0.0&pid=810®istry=redis×tamp=1449113556942, cause: java.lang.ClassCastException: soa.service.PlayerServiceImpl cannot be cast to soa.service.PlayerServiceImpl
at com.alibaba.dubbo.rpc.proxy.AbstractProxyInvoker.invoke(AbstractProxyInvoker.java:76)
at com.alibaba.dubbo.rpc.protocol.InvokerWrapper.invoke(InvokerWrapper.java:53)
at com.alibaba.dubbo.rpc.protocol.dubbo.filter.TraceFilter.invoke(TraceFilter.java:78)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
at com.alibaba.dubbo.monitor.support.MonitorFilter.invoke(MonitorFilter.java:75)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
at com.alibaba.dubbo.rpc.filter.TimeoutFilter.invoke(TimeoutFilter.java:42)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
at com.alibaba.dubbo.rpc.filter.ExceptionFilter.invoke(ExceptionFilter.java:64)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
at com.alibaba.dubbo.rpc.filter.ContextFilter.invoke(ContextFilter.java:60)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
at com.alibaba.dubbo.rpc.filter.GenericFilter.invoke(GenericFilter.java:112)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
at com.alibaba.dubbo.rpc.filter.ClassLoaderFilter.invoke(ClassLoaderFilter.java:38)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
at com.alibaba.dubbo.rpc.filter.EchoFilter.invoke(EchoFilter.java:38)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol$1.reply(DubboProtocol.java:108)
at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.handleRequest(HeaderExchangeHandler.java:84)
at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.received(HeaderExchangeHandler.java:170)
at com.alibaba.dubbo.remoting.transport.DecodeHandler.received(DecodeHandler.java:52)
at com.alibaba.dubbo.remoting.transport.dispather.ChannelEventRunnable.run(ChannelEventRunnable.java:82)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:680)
Caused by: java.lang.IllegalArgumentException: java.lang.ClassCastException: soa.service.PlayerServiceImpl cannot be cast to soa.service.PlayerServiceImpl
at com.alibaba.dubbo.common.bytecode.Wrapper1.invokeMethod(Wrapper1.java)
at com.alibaba.dubbo.rpc.proxy.javassist.JavassistProxyFactory$1.doInvoke(JavassistProxyFactory.java:46)
at com.alibaba.dubbo.rpc.proxy.AbstractProxyInvoker.invoke(AbstractProxyInvoker.java:72)
... 25 more
Caused by: java.lang.ClassCastException: soa.service.PlayerServiceImpl cannot be cast to soa.service.PlayerServiceImpl
... 28 more
[2015-12-03 11:32:55,123] DEBUG [DubboServerHandler-192.168.1.99:20880-thread-3] com.alibaba.dubbo.remoting.transport.DecodeHandler.decode(DecodeHandler.java:60) - [DUBBO] Decode decodeable message com.alibaba.dubbo.rpc.protocol.dubbo.DecodeableRpcInvocation, dubbo version: 2.4.9, current host: 127.0.0.1
[2015-12-03 11:32:55,123] ERROR [DubboServerHandler-192.168.1.99:20880-thread-3] com.alibaba.dubbo.rpc.filter.ExceptionFilter.invoke(ExceptionFilter.java:118) - [DUBBO] Got unchecked and undeclared exception which called by 192.168.1.99. service: soa.service.PlayerService, method: queryById, exception: com.alibaba.dubbo.rpc.RpcException: Failed to invoke remote proxy method queryById to registry://192.168.1.11:6379/com.alibaba.dubbo.registry.RegistryService?application=center&dubbo=2.4.9&export=dubbo%3A%2F%2F192.168.1.99%3A20880%2Fsoa.service.PlayerService%3Fanyhost%3Dtrue%26application%3Dcenter%26dubbo%3D2.4.9%26interface%3Dsoa.service.PlayerService%26methods%3DqueryById%26pid%3D810%26revision%3D1.0.0%26side%3Dprovider%26threads%3D200%26timestamp%3D1449113556952%26version%3D1.0.0&pid=810®istry=redis×tamp=1449113556942, cause: java.lang.ClassCastException: soa.service.PlayerServiceImpl cannot be cast to soa.service.PlayerServiceImpl, dubbo version: 2.4.9, current host: 127.0.0.1
com.alibaba.dubbo.rpc.RpcException: Failed to invoke remote proxy method queryById to registry://192.168.1.11:6379/com.alibaba.dubbo.registry.RegistryService?application=center&dubbo=2.4.9&export=dubbo%3A%2F%2F192.168.1.99%3A20880%2Fsoa.service.PlayerService%3Fanyhost%3Dtrue%26application%3Dcenter%26dubbo%3D2.4.9%26interface%3Dsoa.service.PlayerService%26methods%3DqueryById%26pid%3D810%26revision%3D1.0.0%26side%3Dprovider%26threads%3D200%26timestamp%3D1449113556952%26version%3D1.0.0&pid=810®istry=redis×tamp=1449113556942, cause: java.lang.ClassCastException: soa.service.PlayerServiceImpl cannot be cast to soa.service.PlayerServiceImpl
这个转换错误很诡异啊??
Caused by: java.lang.ClassCastException: soa.service.PlayerServiceImpl cannot be cast to soa.service.PlayerServiceImpl