From 9176b8665a6b0e422760b37968181929ffc3629f Mon Sep 17 00:00:00 2001 From: songxinjianqwe Date: Fri, 27 Jul 2018 00:59:47 +0800 Subject: [PATCH] =?UTF-8?q?injvm=E5=8D=8F=E8=AE=AE=E4=B8=8D=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E6=B3=A8=E5=86=8C=E4=B8=AD=E5=BF=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sinjinsong/toy/config/RegistryConfig.java | 8 ++++++-- .../api/support/AbstractProtocol.java | 7 ++++--- .../toy/protocol/injvm/InJvmInvoker.java | 4 ++-- .../toy/protocol/injvm/InJvmProtocol.java | 7 ------- .../toy/autoconfig/RPCAutoConfiguration.java | 19 ++++++------------- 5 files changed, 18 insertions(+), 27 deletions(-) diff --git a/toy-rpc-core/src/main/java/com/sinjinsong/toy/config/RegistryConfig.java b/toy-rpc-core/src/main/java/com/sinjinsong/toy/config/RegistryConfig.java index b026d3e..9498203 100644 --- a/toy-rpc-core/src/main/java/com/sinjinsong/toy/config/RegistryConfig.java +++ b/toy-rpc-core/src/main/java/com/sinjinsong/toy/config/RegistryConfig.java @@ -20,11 +20,15 @@ public class RegistryConfig { private ServiceRegistry registryInstance; public void init() { - registryInstance.init(); + if(registryInstance != null) { + registryInstance.init(); + } } public void close() { - registryInstance.close(); + if(registryInstance != null) { + registryInstance.close(); + } } } diff --git a/toy-rpc-core/src/main/java/com/sinjinsong/toy/protocol/api/support/AbstractProtocol.java b/toy-rpc-core/src/main/java/com/sinjinsong/toy/protocol/api/support/AbstractProtocol.java index e1221b1..87319ee 100644 --- a/toy-rpc-core/src/main/java/com/sinjinsong/toy/protocol/api/support/AbstractProtocol.java +++ b/toy-rpc-core/src/main/java/com/sinjinsong/toy/protocol/api/support/AbstractProtocol.java @@ -10,6 +10,7 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; + /** * @author sinjinsong * @date 2018/7/7 @@ -18,8 +19,8 @@ public abstract class AbstractProtocol implements Protocol { private Map> exporters = new ConcurrentHashMap<>(); private GlobalConfig globalConfig; - - + + public void init(GlobalConfig globalConfig) { this.globalConfig = globalConfig; } @@ -42,6 +43,6 @@ public ServiceConfig referLocalService(String interfaceMame) throws RPCEx @Override public void close() { - + } } diff --git a/toy-rpc-core/src/main/java/com/sinjinsong/toy/protocol/injvm/InJvmInvoker.java b/toy-rpc-core/src/main/java/com/sinjinsong/toy/protocol/injvm/InJvmInvoker.java index 9ad9f0a..b4811de 100644 --- a/toy-rpc-core/src/main/java/com/sinjinsong/toy/protocol/injvm/InJvmInvoker.java +++ b/toy-rpc-core/src/main/java/com/sinjinsong/toy/protocol/injvm/InJvmInvoker.java @@ -12,7 +12,7 @@ * @date 2018/7/18 */ public class InJvmInvoker extends AbstractInvoker { - + @Override public RPCResponse invoke(InvokeParam invokeParam) throws RPCException { Object serviceBean = getGlobalConfig().getProtocol().referLocalService(invokeParam.getInterfaceName()).getRef(); @@ -33,5 +33,5 @@ public RPCResponse invoke(InvokeParam invokeParam) throws RPCException { } return response; } - + } diff --git a/toy-rpc-core/src/main/java/com/sinjinsong/toy/protocol/injvm/InJvmProtocol.java b/toy-rpc-core/src/main/java/com/sinjinsong/toy/protocol/injvm/InJvmProtocol.java index 097f6ee..a88314c 100644 --- a/toy-rpc-core/src/main/java/com/sinjinsong/toy/protocol/injvm/InJvmProtocol.java +++ b/toy-rpc-core/src/main/java/com/sinjinsong/toy/protocol/injvm/InJvmProtocol.java @@ -23,13 +23,6 @@ public Exporter export(Invoker invoker, ServiceConfig serviceConfig exporter.setServiceConfig(serviceConfig); putExporter(invoker.getInterface(), exporter); // export -// // injvm 不需要注册到注册中心 -// try { -// //TODO refactor this -// serviceConfig.getRegistryConfig().getRegistryInstance().register(InetAddress.getLocalHost().getHostAddress() + ":" + getGlobalConfig().getPort(), serviceConfig.getInterfaceName()); -// } catch (UnknownHostException e) { -// throw new RPCException(e,ErrorEnum.READ_LOCALHOST_ERROR,"读取本地Host失败"); -// } return exporter; } diff --git a/toy-rpc-spring-boot-starter/src/main/java/com/sinjinsong/toy/autoconfig/RPCAutoConfiguration.java b/toy-rpc-spring-boot-starter/src/main/java/com/sinjinsong/toy/autoconfig/RPCAutoConfiguration.java index 4ae64c6..c9a2094 100644 --- a/toy-rpc-spring-boot-starter/src/main/java/com/sinjinsong/toy/autoconfig/RPCAutoConfiguration.java +++ b/toy-rpc-spring-boot-starter/src/main/java/com/sinjinsong/toy/autoconfig/RPCAutoConfiguration.java @@ -18,12 +18,9 @@ import com.sinjinsong.toy.registry.zookeeper.ZkServiceRegistry; import com.sinjinsong.toy.serialize.api.Serializer; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.BeansException; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -34,13 +31,11 @@ @EnableConfigurationProperties(RPCProperties.class) @Configuration @Slf4j -public class RPCAutoConfiguration implements InitializingBean, ApplicationContextAware { +public class RPCAutoConfiguration implements InitializingBean { @Autowired private RPCProperties properties; - private ApplicationContext ctx; private ExtensionLoader extensionLoader; - @Bean(initMethod = "init", destroyMethod = "close") public RegistryConfig registryConfig() { RegistryConfig registryConfig = properties.getRegistry(); @@ -49,12 +44,14 @@ public RegistryConfig registryConfig() { } //TODO 根据type创建ServiceRegistry //TODO injvm协议不需要连接ZK - ZkServiceRegistry serviceRegistry = new ZkServiceRegistry(registryConfig); - registryConfig.setRegistryInstance(serviceRegistry); + if (ProtocolType.INJVM != ProtocolType.valueOf(properties.getProtocol().getType().toUpperCase())) { + ZkServiceRegistry serviceRegistry = new ZkServiceRegistry(registryConfig); + registryConfig.setRegistryInstance(serviceRegistry); + } log.info("{}", registryConfig); return registryConfig; } - + @Bean public ApplicationConfig applicationConfig() { ApplicationConfig application = properties.getApplication(); @@ -151,10 +148,6 @@ public RPCProviderBeanPostProcessor rpcProviderBeanPostProcessor(ApplicationConf return processor; } - @Override - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { - this.ctx = applicationContext; - } @Override public void afterPropertiesSet() throws Exception {