1、待解析xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:nettyrpc="http://www.newlandframework.com/nettyrpc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.newlandframework.com/nettyrpc http://www.newlandframework.com/nettyrpc/nettyrpc.xsd"> <context:property-placeholder location="classpath:rpc-server.properties"/> <nettyrpc:service id="demoAddService" interfaceName="com.newlandframework.rpc.services.AddCalculate" ref="calcAddService"></nettyrpc:service> <nettyrpc:service id="demoMultiService" interfaceName="com.newlandframework.rpc.services.MultiCalculate" ref="calcMultiService"></nettyrpc:service> <nettyrpc:service id="demoPersonManage" interfaceName="com.newlandframework.rpc.services.PersonManage" ref="personManageService"></nettyrpc:service> <nettyrpc:service id="demoCostTime" interfaceName="com.newlandframework.rpc.services.CostTimeCalculate" ref="costTimeCalculateService"></nettyrpc:service> <nettyrpc:registry id="rpcRegistry" ipAddr="${rpc.server.addr}" echoApiPort="${rpc.server.echo.api.port}" protocol="PROTOSTUFFSERIALIZE"></nettyrpc:registry> <bean id="calcAddService" class="com.newlandframework.rpc.services.impl.AddCalculateImpl"></bean> <bean id="calcMultiService" class="com.newlandframework.rpc.services.impl.MultiCalculateImpl"></bean> <bean id="personManageService" class="com.newlandframework.rpc.services.impl.PersonManageImpl"></bean> <bean id="costTimeCalculateService" class="com.newlandframework.rpc.services.impl.CostTimeCalculateImpl"></bean> </beans>
2、META-INF下的 nettyrpc.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.newlandframework.com/nettyrpc" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans" targetNamespace="http://www.newlandframework.com/nettyrpc" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:import namespace="http://www.springframework.org/schema/beans"/>
<xsd:element name="service">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:attribute name="interfaceName" type="xsd:string" use="required"/>
<xsd:attribute name="ref" type="xsd:string" use="required"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="registry">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:attribute name="ipAddr" type="xsd:string" use="required"/>
<xsd:attribute name="echoApiPort" type="xsd:string" use="required"/>
<xsd:attribute name="protocol" type="xsd:string" use="required"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="reference">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:attribute name="interfaceName" type="xsd:string" use="required"/>
<xsd:attribute name="ipAddr" type="xsd:string" use="required"/>
<xsd:attribute name="protocol" type="xsd:string" use="required"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>
3、META-INF下的spring.schemas
http\://www.newlandframework.com/nettyrpc/nettyrpc.xsd=META-INF/nettyrpc.xsd
4、META-INF下的spring.handlers
http\://www.newlandframework.com/nettyrpc=com.newlandframework.rpc.spring.NettyRpcNamespaceHandler
5、handler解析
/** * Copyright (C) 2016 Newland Group Holding Limited * <p> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.newlandframework.rpc.spring; import org.springframework.beans.factory.xml.NamespaceHandlerSupport; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import com.google.common.io.CharStreams; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; /** * @author tangjie<https://github.com/tang-jie> * @filename:NettyRpcNamespaceHandler.java * @description:NettyRpcNamespaceHandler功能模块 * @blogs http://www.cnblogs.com/jietang/ * @since 2016/10/7 */ public class NettyRpcNamespaceHandler extends NamespaceHandlerSupport { static { Resource resource = new ClassPathResource("NettyRPC-logo.txt"); if (resource.exists()) { try { Reader reader = new InputStreamReader(resource.getInputStream(), "UTF-8"); String text = CharStreams.toString(reader); System.out.println(text); reader.close(); } catch (IOException e) { e.printStackTrace(); } } else { System.out.println(""); System.out.println(" _ _____ _____ _____ ___ _ ____ ____ ____ "); System.out.println("/ \\ /|/ __//__ __Y__ __\\\\ \\/// __\\/ __\\/ _\\"); System.out.println("| |\\ ||| \\ / \\ / \\ \\ / | \\/|| \\/|| / "); System.out.println("| | \\||| /_ | | | | / / | /| __/| \\_ "); System.out.println("\\_/ \\|\\____\\ \\_/ \\_/ /_/ \\_/\\_\\\\_/ \\____/"); System.out.println("[NettyRPC 2.0,Build 2016/10/7,Author:tangjie http://www.cnblogs.com/jietang/]"); System.out.println(""); } } public void init() { // registerBeanDefinitionParser("service", new NettyRpcServiceParser()); registerBeanDefinitionParser("registry", new NettyRpcRegisteryParser()); registerBeanDefinitionParser("reference", new NettyRpcReferenceParser()); } }
6、解析表签<nettyrpc:service>数据成NettyRpcService对象
/** * Copyright (C) 2016 Newland Group Holding Limited * <p> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.newlandframework.rpc.spring; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.xml.BeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; import org.springframework.beans.factory.support.RootBeanDefinition; import org.w3c.dom.Element; /** * @author tangjie<https://github.com/tang-jie> * @filename:NettyRpcServiceParser.java * @description:NettyRpcServiceParser功能模块 * @blogs http://www.cnblogs.com/jietang/ * @since 2016/10/7 */ public class NettyRpcServiceParser implements BeanDefinitionParser { public BeanDefinition parse(Element element, ParserContext parserContext) { String interfaceName = element.getAttribute("interfaceName"); String ref = element.getAttribute("ref"); RootBeanDefinition beanDefinition = new RootBeanDefinition(); beanDefinition.setBeanClass(NettyRpcService.class); beanDefinition.setLazyInit(false); beanDefinition.getPropertyValues().addPropertyValue("interfaceName", interfaceName); beanDefinition.getPropertyValues().addPropertyValue("ref", ref); parserContext.getRegistry().registerBeanDefinition(interfaceName, beanDefinition); return beanDefinition; } }
7、NettyRpcServiceParser 对象
/** * Copyright (C) 2016 Newland Group Holding Limited * <p> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.newlandframework.rpc.spring; import com.newlandframework.rpc.event.ServerStartEvent; import com.newlandframework.rpc.netty.MessageRecvExecutor; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; /** * @author tangjie<https://github.com/tang-jie> * @filename:NettyRpcService.java * @description:NettyRpcService功能模块 * @blogs http://www.cnblogs.com/jietang/ * @since 2016/10/7 */ public class NettyRpcService implements ApplicationContextAware, ApplicationListener { private String interfaceName; private String ref; private ApplicationContext applicationContext; public String getRef() { return ref; } public void setRef(String ref) { this.ref = ref; } public void onApplicationEvent(ApplicationEvent event) { MessageRecvExecutor.getInstance().getHandlerMap().put(interfaceName, applicationContext.getBean(ref)); } public String getInterfaceName() { return interfaceName; } public void setInterfaceName(String interfaceName) { this.interfaceName = interfaceName; } public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; applicationContext.publishEvent(new ServerStartEvent(new Object())); } public ApplicationContext getApplicationContext() { return applicationContext; } }
8、解析标签<nettyrpc:registry>NettyRpcRegistery对象
/** * Copyright (C) 2016 Newland Group Holding Limited * <p> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.newlandframework.rpc.spring; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.xml.BeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; import org.w3c.dom.Element; /** * @author tangjie<https://github.com/tang-jie> * @filename:NettyRpcRegisteryParser.java * @description:NettyRpcRegisteryParser功能模块 * @blogs http://www.cnblogs.com/jietang/ * @since 2016/10/7 */ public class NettyRpcRegisteryParser implements BeanDefinitionParser { public BeanDefinition parse(Element element, ParserContext parserContext) { String id = element.getAttribute("id"); String ipAddr = element.getAttribute("ipAddr"); String echoApiPort = element.getAttribute("echoApiPort"); String protocolType = element.getAttribute("protocol"); RootBeanDefinition beanDefinition = new RootBeanDefinition(); beanDefinition.setBeanClass(NettyRpcRegistery.class); beanDefinition.getPropertyValues().addPropertyValue("ipAddr", ipAddr); beanDefinition.getPropertyValues().addPropertyValue("echoApiPort", echoApiPort); beanDefinition.getPropertyValues().addPropertyValue("protocol", protocolType); parserContext.getRegistry().registerBeanDefinition(id, beanDefinition); return beanDefinition; } }
9、NettyRpcRegistery对象
/** * Copyright (C) 2016 Newland Group Holding Limited * <p> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.newlandframework.rpc.spring; import com.newlandframework.rpc.core.RpcSystemConfig; import com.newlandframework.rpc.jmx.ThreadPoolMonitorProvider; import com.newlandframework.rpc.serialize.RpcSerializeProtocol; import com.newlandframework.rpc.netty.MessageRecvExecutor; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** * @author tangjie<https://github.com/tang-jie> * @filename:NettyRpcRegistery.java * @description:NettyRpcRegistery功能模块 * @blogs http://www.cnblogs.com/jietang/ * @since 2016/10/7 */ public class NettyRpcRegistery implements InitializingBean, DisposableBean { private String ipAddr; private String protocol; private String echoApiPort; private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); public void destroy() throws Exception { MessageRecvExecutor.getInstance().stop(); } public void afterPropertiesSet() throws Exception { MessageRecvExecutor ref = MessageRecvExecutor.getInstance(); ref.setServerAddress(ipAddr); ref.setEchoApiPort(Integer.parseInt(echoApiPort)); ref.setSerializeProtocol(Enum.valueOf(RpcSerializeProtocol.class, protocol)); if (RpcSystemConfig.isMonitorServerSupport()) { context.register(ThreadPoolMonitorProvider.class); context.refresh(); } ref.start(); } public String getIpAddr() { return ipAddr; } public void setIpAddr(String ipAddr) { this.ipAddr = ipAddr; } public String getProtocol() { return protocol; } public void setProtocol(String protocol) { this.protocol = protocol; } public String getEchoApiPort() { return echoApiPort; } public void setEchoApiPort(String echoApiPort) { this.echoApiPort = echoApiPort; } }