├── .gitignore ├── Irelia-common-util ├── pom.xml └── src │ └── main │ └── java │ └── cn │ └── fanhub │ └── irelia │ └── common │ └── utils │ ├── RSA.java │ ├── ResponseUtil.java │ └── SignUtil.java ├── Irelia-core ├── pom.xml └── src │ └── main │ └── java │ └── cn │ └── fanhub │ └── irelia │ └── core │ ├── Handler.java │ ├── exception │ └── IreliaRuntimeException.java │ ├── handler │ ├── AbstractErrorHandler.java │ ├── AbstractPostHandler.java │ ├── AbstractPreHandler.java │ └── AbstractRouterHandler.java │ ├── model │ ├── CacheConfig.java │ ├── Headers.java │ ├── IreliaRequest.java │ ├── IreliaResponse.java │ ├── IreliaResponseCode.java │ ├── LimitConfig.java │ ├── RpcConfig.java │ └── SystemConfig.java │ └── upstream │ └── UpstreamConfig.java ├── Irelia-server ├── pom.xml └── src │ └── main │ ├── java │ └── cn │ │ └── fanhub │ │ └── irelia │ │ └── server │ │ ├── Bootstrap.java │ │ ├── HttpSupportInitializer.java │ │ ├── future │ │ └── ErrorChannelFutureListener.java │ │ ├── handler │ │ ├── AbstractConfigHandler.java │ │ ├── HttpInboundHandler.java │ │ ├── ResponseHandler.java │ │ ├── RouteHandler.java │ │ ├── SecurityHandler.java │ │ ├── cache │ │ │ ├── AbstractCacheHandler.java │ │ │ ├── CacheHelper.java │ │ │ ├── CacheModel.java │ │ │ ├── GuavaCacheHandler.java │ │ │ └── SimpleCacheHandler.java │ │ ├── error │ │ │ └── ErrorHandler.java │ │ └── limit │ │ │ ├── AbstractLimitHandler.java │ │ │ └── SimpleLimitHandler.java │ │ └── http │ │ └── HeaderKey.java │ └── resources │ └── logback.xml ├── Irelia-spi-core ├── pom.xml └── src │ └── main │ └── java │ └── cn │ └── fanhub │ └── irelia │ └── spi │ └── core │ ├── IreliaService.java │ ├── IreliaServiceHolder.java │ ├── IreliaServiceManager.java │ ├── annotation │ └── Rpc.java │ ├── impl │ ├── IreliaServiceHolderImpl.java │ └── IreliaServiceImpl.java │ └── model │ ├── IreliaBean.java │ └── MethodInfo.java ├── Irelia-spi-dubbo ├── pom.xml └── src │ └── main │ └── java │ └── cn │ └── fanhub │ └── irelia │ └── spi │ └── dubbo │ ├── DubboClient.java │ └── DubboStarter.java ├── Irelia-test ├── pom.xml └── src │ ├── main │ └── java │ │ └── cn │ │ └── fanhub │ │ └── irelia │ │ └── demo │ │ ├── Starter.java │ │ └── config │ │ └── SpringConfig.java │ └── test │ └── java │ └── TestBootstrap.java ├── Irelia-upstream-dubbo ├── pom.xml └── src │ └── main │ ├── java │ └── cn │ │ └── fanhub │ │ └── irelia │ │ └── upstream │ │ └── dubbo │ │ ├── DubboReqest.java │ │ ├── DubboServiceManager.java │ │ ├── DubboUpstream.java │ │ └── DubboUpstreamConfig.java │ └── resources │ └── irelia.upstream.de ├── Irelia-upstream ├── pom.xml └── src │ └── main │ └── java │ └── cn │ └── fanhub │ └── irelia │ └── upstream │ ├── IreliaUpstream.java │ ├── UpstreamConfig.java │ └── UpstreamManager.java ├── LICENSE ├── README.md └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | .log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.ear 17 | *.zip 18 | *.tar.gz 19 | *.rar 20 | 21 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 22 | hs_err_pid* 23 | 24 | .idea/ 25 | .idea/*.xml 26 | *.iml 27 | .DS_Store/ 28 | .vscode/ 29 | 30 | target/ 31 | -------------------------------------------------------------------------------- /Irelia-common-util/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | Irelia-parent 7 | cn.fanhub 8 | 0.0.1 9 | 10 | 4.0.0 11 | 12 | Irelia-common-util 13 | 14 | 15 | 16 | cn.fanhub 17 | Irelia-core 18 | ${project.version} 19 | 20 | 21 | -------------------------------------------------------------------------------- /Irelia-common-util/src/main/java/cn/fanhub/irelia/common/utils/RSA.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.common.utils; 17 | 18 | import java.security.KeyFactory; 19 | import java.security.KeyPair; 20 | import java.security.KeyPairGenerator; 21 | import java.security.NoSuchAlgorithmException; 22 | import java.security.PrivateKey; 23 | import java.security.PublicKey; 24 | import java.security.interfaces.RSAPrivateKey; 25 | import java.security.interfaces.RSAPublicKey; 26 | import java.security.spec.InvalidKeySpecException; 27 | import java.security.spec.PKCS8EncodedKeySpec; 28 | import java.security.spec.X509EncodedKeySpec; 29 | import java.util.HashMap; 30 | import java.util.Map; 31 | 32 | /** 33 | * 34 | * @author chengfan 35 | * @version $Id: RSA.java, v 0.1 2018年06月13日 下午11:35 chengfan Exp $ 36 | */ 37 | public class RSA { 38 | public static final String KEY_ALGORITHM = "RSA"; 39 | public static final String CIPHER_ALGORITHM = "RSA/ECB/PKCS1Padding"; 40 | public static final String PUBLIC_KEY = "publicKey"; 41 | public static final String PRIVATE_KEY = "privateKey"; 42 | public static final int KEY_SIZE = 2048; 43 | /** 44 | * 生成密钥对 45 | * 46 | * @return 47 | */ 48 | public static Map generateKeyBytes() { 49 | 50 | try { 51 | KeyPairGenerator keyPairGenerator = KeyPairGenerator 52 | .getInstance(KEY_ALGORITHM); 53 | keyPairGenerator.initialize(KEY_SIZE); 54 | KeyPair keyPair = keyPairGenerator.generateKeyPair(); 55 | RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); 56 | RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); 57 | Map keyMap = new HashMap(); 58 | keyMap.put(PUBLIC_KEY, publicKey.getEncoded()); 59 | keyMap.put(PRIVATE_KEY, privateKey.getEncoded()); 60 | return keyMap; 61 | } catch (NoSuchAlgorithmException e) { 62 | e.printStackTrace(); 63 | } 64 | return null; 65 | } 66 | 67 | /** 68 | * 还原公钥 69 | * 70 | * @param keyBytes 71 | * @return 72 | */ 73 | public static PublicKey restorePublicKey(byte[] keyBytes) { 74 | X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes); 75 | try { 76 | KeyFactory factory = KeyFactory.getInstance(KEY_ALGORITHM); 77 | PublicKey publicKey = factory.generatePublic(x509EncodedKeySpec); 78 | return publicKey; 79 | } catch (NoSuchAlgorithmException e) { 80 | e.printStackTrace(); 81 | } catch (InvalidKeySpecException e) { 82 | e.printStackTrace(); 83 | } 84 | return null; 85 | } 86 | 87 | /** 88 | * 还原私钥 89 | * 90 | * @param keyBytes 91 | * @return 92 | */ 93 | public static PrivateKey restorePrivateKey(byte[] keyBytes) { 94 | PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec( 95 | keyBytes); 96 | try { 97 | KeyFactory factory = KeyFactory.getInstance(KEY_ALGORITHM); 98 | PrivateKey privateKey = factory.generatePrivate(pkcs8EncodedKeySpec); 99 | return privateKey; 100 | } catch (NoSuchAlgorithmException e) { 101 | e.printStackTrace(); 102 | } catch (InvalidKeySpecException e) { 103 | e.printStackTrace(); 104 | } 105 | return null; 106 | } 107 | } -------------------------------------------------------------------------------- /Irelia-common-util/src/main/java/cn/fanhub/irelia/common/utils/ResponseUtil.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.common.utils; 17 | 18 | import cn.fanhub.irelia.core.exception.IreliaRuntimeException; 19 | import cn.fanhub.irelia.core.model.IreliaResponse; 20 | import cn.fanhub.irelia.core.model.IreliaResponseCode; 21 | import com.alibaba.fastjson.JSON; 22 | import io.netty.buffer.Unpooled; 23 | import io.netty.channel.ChannelFutureListener; 24 | import io.netty.channel.ChannelHandlerContext; 25 | import io.netty.handler.codec.http.DefaultFullHttpResponse; 26 | import io.netty.handler.codec.http.FullHttpResponse; 27 | import io.netty.handler.codec.http.HttpHeaderNames; 28 | import io.netty.handler.codec.http.HttpResponseStatus; 29 | import io.netty.handler.codec.http.HttpVersion; 30 | import io.netty.util.CharsetUtil; 31 | 32 | /** 33 | * 34 | * @author chengfan 35 | * @version $Id: ResponseUtil.java, v 0.1 2018年05月10日 下午8:46 chengfan Exp $ 36 | */ 37 | public class ResponseUtil { 38 | 39 | /** 40 | * 发送的返回值 41 | * @param ctx 返回 42 | * @param ireliaResponse 消息 43 | */ 44 | public static void send(ChannelHandlerContext ctx, IreliaResponse ireliaResponse, HttpResponseStatus status) { 45 | FullHttpResponse response = new DefaultFullHttpResponse( 46 | HttpVersion.HTTP_1_1, 47 | status, 48 | Unpooled.copiedBuffer(JSON.toJSONString(ireliaResponse), CharsetUtil.UTF_8) 49 | ); 50 | response.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json; charset=UTF-8"); 51 | ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); 52 | } 53 | 54 | public static void buildExceptionResponse(Throwable cause, IreliaResponse response) { 55 | if (cause instanceof IreliaRuntimeException) { 56 | response.setCode(((IreliaRuntimeException) cause).getResponseCode().getCode()); 57 | response.setMessage(((IreliaRuntimeException) cause).getResponseCode().getMessage()); 58 | } else { 59 | response.setCode(IreliaResponseCode.SERVER_ERR.getCode()); 60 | response.setMessage(IreliaResponseCode.SERVER_ERR.getMessage()); 61 | } 62 | } 63 | } -------------------------------------------------------------------------------- /Irelia-common-util/src/main/java/cn/fanhub/irelia/common/utils/SignUtil.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.common.utils; 17 | 18 | import java.security.InvalidKeyException; 19 | import java.security.MessageDigest; 20 | import java.security.NoSuchAlgorithmException; 21 | import java.security.PrivateKey; 22 | import java.security.PublicKey; 23 | import java.security.Signature; 24 | import java.security.SignatureException; 25 | 26 | /** 27 | * 28 | * @author chengfan 29 | * @version $Id: SignUtil.java, v 0.1 2018年06月13日 下午11:28 chengfan Exp $ 30 | */ 31 | public class SignUtil { 32 | 33 | public static final String SIGNATURE_ALGORITHM = "SHA256withRSA"; 34 | public static final String ENCODE_ALGORITHM = "SHA-256"; 35 | 36 | public static byte[] sign(PrivateKey privateKey, String plain_text) { 37 | 38 | 39 | MessageDigest messageDigest; 40 | byte[] signed = null; 41 | try { 42 | messageDigest = MessageDigest.getInstance(ENCODE_ALGORITHM); 43 | messageDigest.update(plain_text.getBytes()); 44 | byte[] outputDigest_sign = messageDigest.digest(); 45 | 46 | Signature Sign = Signature.getInstance(SIGNATURE_ALGORITHM); 47 | Sign.initSign(privateKey); 48 | Sign.update(outputDigest_sign); 49 | signed = Sign.sign(); 50 | } catch (NoSuchAlgorithmException e) { 51 | e.printStackTrace(); 52 | } catch (SignatureException e) { 53 | e.printStackTrace(); 54 | } catch (InvalidKeyException e) { 55 | e.printStackTrace(); 56 | } 57 | return signed; 58 | } 59 | 60 | /** 61 | * 验签 62 | * 63 | * @param publicKey 64 | * 公钥 65 | * @param plain_text 66 | * 明文 67 | * @param signed 68 | * 签名 69 | */ 70 | public static boolean verifySign(PublicKey publicKey, String plain_text, byte[] signed) { 71 | 72 | MessageDigest messageDigest; 73 | boolean SignedSuccess=false; 74 | try { 75 | messageDigest = MessageDigest.getInstance(ENCODE_ALGORITHM); 76 | messageDigest.update(plain_text.getBytes()); 77 | byte[] outputDigest_verify = messageDigest.digest(); 78 | Signature verifySign = Signature.getInstance(SIGNATURE_ALGORITHM); 79 | verifySign.initVerify(publicKey); 80 | verifySign.update(outputDigest_verify); 81 | SignedSuccess = verifySign.verify(signed); 82 | 83 | } catch (NoSuchAlgorithmException e) { 84 | e.printStackTrace(); 85 | } catch (SignatureException e) { 86 | e.printStackTrace(); 87 | } catch (InvalidKeyException e) { 88 | e.printStackTrace(); 89 | } 90 | return SignedSuccess; 91 | } 92 | 93 | /** 94 | * bytes[]换成16进制字符串 95 | * 96 | * @param src 97 | * @return 98 | */ 99 | public static String bytesToHexString(byte[] src) { 100 | StringBuilder stringBuilder = new StringBuilder(); 101 | if (src == null || src.length <= 0) { 102 | return null; 103 | } 104 | for (int i = 0; i < src.length; i++) { 105 | int v = src[i] & 0xFF; 106 | String hv = Integer.toHexString(v); 107 | if (hv.length() < 2) { 108 | stringBuilder.append(0); 109 | } 110 | stringBuilder.append(hv); 111 | } 112 | return stringBuilder.toString(); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /Irelia-core/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | Irelia-parent 7 | cn.fanhub 8 | 0.0.1 9 | 10 | 4.0.0 11 | 12 | Irelia-core 13 | 14 | 15 | 16 | org.apache.maven.plugins 17 | maven-compiler-plugin 18 | 19 | 8 20 | 8 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | io.netty 29 | netty-all 30 | 31 | 32 | org.projectlombok 33 | lombok 34 | 35 | 36 | ch.qos.logback 37 | logback-classic 38 | 39 | 40 | 41 | org.springframework 42 | spring-core 43 | 44 | 45 | org.springframework 46 | spring-aop 47 | 48 | 49 | org.springframework 50 | spring-aspects 51 | 52 | 53 | org.aspectj 54 | aspectjweaver 55 | 56 | 57 | aopalliance 58 | aopalliance 59 | 60 | 61 | org.springframework 62 | spring-beans 63 | 64 | 65 | org.springframework 66 | spring-context 67 | 68 | 69 | com.google.guava 70 | guava 71 | 72 | 73 | org.apache.commons 74 | commons-lang3 75 | 76 | 77 | commons-beanutils 78 | commons-beanutils 79 | 80 | 81 | 82 | commons-lang 83 | commons-lang 84 | 85 | 86 | 87 | 88 | joda-time 89 | joda-time 90 | 91 | 92 | 93 | com.alibaba 94 | fastjson 95 | 96 | 97 | -------------------------------------------------------------------------------- /Irelia-core/src/main/java/cn/fanhub/irelia/core/Handler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.core; 17 | 18 | /** 19 | * 20 | * @author chengfan 21 | * @version $Id: Handler.java, v 0.1 2018年04月16日 下午10:33 chengfan Exp $ 22 | */ 23 | public interface Handler { 24 | int order(); 25 | } -------------------------------------------------------------------------------- /Irelia-core/src/main/java/cn/fanhub/irelia/core/exception/IreliaRuntimeException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.core.exception; 17 | 18 | import cn.fanhub.irelia.core.model.IreliaResponseCode; 19 | import lombok.Data; 20 | 21 | /** 22 | * 23 | * @author chengfan 24 | * @version $Id: IreliaRuntimeException.java, v 0.1 2018年05月06日 下午5:29 chengfan Exp $ 25 | */ 26 | @Data 27 | public class IreliaRuntimeException extends Exception { 28 | 29 | private IreliaResponseCode responseCode; 30 | 31 | private String message; 32 | 33 | private Throwable cause; 34 | 35 | public IreliaRuntimeException(IreliaResponseCode responseCode, String message) { 36 | super(message); 37 | this.responseCode = responseCode; 38 | this.message = message; 39 | } 40 | 41 | public IreliaRuntimeException(IreliaResponseCode responseCode, String message, Throwable cause) { 42 | super(message, cause); 43 | this.responseCode = responseCode; 44 | this.message = message; 45 | this.cause = cause; 46 | } 47 | } -------------------------------------------------------------------------------- /Irelia-core/src/main/java/cn/fanhub/irelia/core/handler/AbstractErrorHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.core.handler; 17 | 18 | import cn.fanhub.irelia.core.Handler; 19 | import io.netty.channel.ChannelDuplexHandler; 20 | 21 | /** 22 | * 23 | * @author chengfan 24 | * @version $Id: AbstractErrorHandler.java, v 0.1 2018年04月16日 下午10:38 chengfan Exp $ 25 | */ 26 | public abstract class AbstractErrorHandler extends ChannelDuplexHandler implements Handler { 27 | 28 | } -------------------------------------------------------------------------------- /Irelia-core/src/main/java/cn/fanhub/irelia/core/handler/AbstractPostHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.core.handler; 17 | 18 | import cn.fanhub.irelia.core.Handler; 19 | import io.netty.channel.ChannelOutboundHandlerAdapter; 20 | 21 | /** 22 | * 23 | * @author chengfan 24 | * @version $Id: AbstractPostHandler.java, v 0.1 2018年04月16日 下午10:37 chengfan Exp $ 25 | */ 26 | public abstract class AbstractPostHandler extends ChannelOutboundHandlerAdapter implements Handler { 27 | 28 | } -------------------------------------------------------------------------------- /Irelia-core/src/main/java/cn/fanhub/irelia/core/handler/AbstractPreHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.core.handler; 17 | 18 | import cn.fanhub.irelia.core.Handler; 19 | import io.netty.channel.ChannelInboundHandlerAdapter; 20 | 21 | /** 22 | * 23 | * @author chengfan 24 | * @version $Id: AbstractPreHandler.java, v 0.1 2018年04月16日 下午10:34 chengfan Exp $ 25 | */ 26 | public abstract class AbstractPreHandler extends ChannelInboundHandlerAdapter implements Handler { 27 | 28 | } -------------------------------------------------------------------------------- /Irelia-core/src/main/java/cn/fanhub/irelia/core/handler/AbstractRouterHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.core.handler; 17 | 18 | import cn.fanhub.irelia.core.Handler; 19 | import io.netty.channel.ChannelInboundHandlerAdapter; 20 | 21 | /** 22 | * 23 | * @author chengfan 24 | * @version $Id: AbstractRouterHandler.java, v 0.1 2018年04月16日 下午10:37 chengfan Exp $ 25 | */ 26 | public abstract class AbstractRouterHandler extends ChannelInboundHandlerAdapter implements Handler { 27 | } -------------------------------------------------------------------------------- /Irelia-core/src/main/java/cn/fanhub/irelia/core/model/CacheConfig.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.core.model; 17 | 18 | import lombok.Data; 19 | 20 | import java.io.Serializable; 21 | 22 | /** 23 | * 24 | * @author chengfan 25 | * @version $Id: CacheConfig.java, v 0.1 2018年06月11日 下午3:28 chengfan Exp $ 26 | */ 27 | @Data 28 | public class CacheConfig implements Serializable { 29 | private boolean cache = false; 30 | private long cacheTime; 31 | 32 | public long getCacheTime() { 33 | return cacheTime; 34 | } 35 | 36 | public void setCacheTime(String cacheTime) { 37 | this.cacheTime = Long.parseLong(cacheTime); 38 | } 39 | } -------------------------------------------------------------------------------- /Irelia-core/src/main/java/cn/fanhub/irelia/core/model/Headers.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.core.model; 17 | 18 | import com.google.common.collect.Maps; 19 | import io.netty.handler.codec.http.HttpHeaders; 20 | 21 | import java.io.Serializable; 22 | import java.util.Map; 23 | import java.util.Map.Entry; 24 | import java.util.Set; 25 | 26 | /** 27 | * 28 | * @author chengfan 29 | * @version $Id: Headers.java, v 0.1 2018年06月14日 下午6:54 chengfan Exp $ 30 | */ 31 | public class Headers implements Serializable { 32 | 33 | private Map headerMap = Maps.newConcurrentMap(); 34 | 35 | public void put(String key, String value) { 36 | headerMap.put(key, value); 37 | } 38 | 39 | public String get(String key) { 40 | return headerMap.get(key); 41 | } 42 | 43 | public Set> entries() { 44 | return headerMap.entrySet(); 45 | } 46 | 47 | public void putAll(HttpHeaders httpHeaders) { 48 | for (Entry entry : httpHeaders.entries()) { 49 | headerMap.put(entry.getKey(), entry.getValue()); 50 | } 51 | } 52 | } -------------------------------------------------------------------------------- /Irelia-core/src/main/java/cn/fanhub/irelia/core/model/IreliaRequest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.core.model; 17 | 18 | import cn.fanhub.irelia.core.upstream.UpstreamConfig; 19 | import com.alibaba.fastjson.JSONArray; 20 | import lombok.Data; 21 | 22 | import java.io.Serializable; 23 | 24 | /** 25 | * 26 | * @author chengfan 27 | * @version $Id: IreliaRequest.java, v 0.1 2018年04月21日 下午4:25 chengfan Exp $ 28 | */ 29 | @Data 30 | public class IreliaRequest implements Serializable { 31 | 32 | private String appName; 33 | 34 | private UpstreamConfig upstreamConfig; 35 | 36 | private RpcConfig rpcConfig; 37 | 38 | private SystemConfig systemConfig; 39 | 40 | private String rpcValue; 41 | 42 | private JSONArray requestArgs; 43 | 44 | private Headers headers; 45 | } -------------------------------------------------------------------------------- /Irelia-core/src/main/java/cn/fanhub/irelia/core/model/IreliaResponse.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.core.model; 17 | 18 | import lombok.Data; 19 | 20 | import java.io.Serializable; 21 | 22 | /** 23 | * 24 | * @author chengfan 25 | * @version $Id: IreliaResponse.java, v 0.1 2018年04月21日 下午4:25 chengfan Exp $ 26 | */ 27 | @Data 28 | public class IreliaResponse implements Serializable { 29 | private Object content; 30 | private int code; 31 | private String message; 32 | 33 | } -------------------------------------------------------------------------------- /Irelia-core/src/main/java/cn/fanhub/irelia/core/model/IreliaResponseCode.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.core.model; 17 | 18 | /** 19 | * 20 | * @author chengfan 21 | * @version $Id: IreliaResponseCode.java, v 0.1 2018年05月10日 下午8:51 chengfan Exp $ 22 | */ 23 | public enum IreliaResponseCode { 24 | 25 | NO_SIGN_KEY_OR_VALUE(3006, "没有上传公钥或者没有传递签名信息"), 26 | SIGN_ERROR(3005, "签名验证失败"), 27 | RPC_BEEN_LIMITED(3004, "RPC 被限流"), 28 | NOT_OPEN_RPC(3003, "没有开启的 RPC"), 29 | INVALID_QUERY_URL(3002, "非法的请求路径"), 30 | NOT_SUPPORT_REQUEST(3001, "不支持的请求类型"), 31 | SERVER_ERR(2001, "服务端错误"), 32 | BAD_REQUEST(2002, "请求失败"), 33 | CUSTOMER_ERR(2000, "业务方错误"), 34 | SUCCESS(1000, "响应成功"); 35 | 36 | private int code; 37 | private String message; 38 | 39 | IreliaResponseCode(int code, String message) { 40 | this.code = code; 41 | this.message = message; 42 | } 43 | 44 | public int getCode() { 45 | return code; 46 | } 47 | 48 | public String getMessage() { 49 | return message; 50 | } 51 | 52 | public IreliaResponseCode of(int code) { 53 | switch (code) { 54 | case 1000: 55 | return SUCCESS; 56 | case 2000: 57 | return CUSTOMER_ERR; 58 | case 2001: 59 | return SERVER_ERR; 60 | default: 61 | return SUCCESS; 62 | } 63 | } 64 | 65 | 66 | } -------------------------------------------------------------------------------- /Irelia-core/src/main/java/cn/fanhub/irelia/core/model/LimitConfig.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.core.model; 17 | 18 | import lombok.Data; 19 | 20 | import java.io.Serializable; 21 | 22 | /** 23 | * 24 | * @author chengfan 25 | * @version $Id: LimitConfig.java, v 0.1 2018年05月13日 下午1:36 chengfan Exp $ 26 | */ 27 | @Data 28 | public class LimitConfig implements Serializable { 29 | private boolean limit = false; 30 | private int frequency; 31 | private String limitTemplate; 32 | } -------------------------------------------------------------------------------- /Irelia-core/src/main/java/cn/fanhub/irelia/core/model/RpcConfig.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.core.model; 17 | 18 | import lombok.Data; 19 | import lombok.NoArgsConstructor; 20 | 21 | import java.io.Serializable; 22 | 23 | /** 24 | * 25 | * @author chengfan 26 | * @version $Id: RpcConfig.java, v 0.1 2018年04月30日 下午10:34 chengfan Exp $ 27 | */ 28 | @Data 29 | @NoArgsConstructor 30 | public class RpcConfig implements Serializable { 31 | private String itfName; 32 | private String methodName; 33 | private String appName; 34 | private String rpcValue; 35 | private String rpcName; 36 | private String des; 37 | private boolean open = false; 38 | private boolean needSign = false; 39 | private LimitConfig limitConfig; 40 | private CacheConfig cacheConfig; 41 | 42 | /** 43 | * 扩展字段,可以存为一个大的 json 字段,用来为自定义的 handler 提供配置功能 44 | */ 45 | private String extension; 46 | } -------------------------------------------------------------------------------- /Irelia-core/src/main/java/cn/fanhub/irelia/core/model/SystemConfig.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.core.model; 17 | 18 | import lombok.Data; 19 | 20 | import java.io.Serializable; 21 | 22 | /** 23 | * 24 | * @author chengfan 25 | * @version $Id: SystemConfig.java, v 0.1 2018年06月13日 下午11:19 chengfan Exp $ 26 | */ 27 | @Data 28 | public class SystemConfig implements Serializable { 29 | private String publicKey; 30 | } -------------------------------------------------------------------------------- /Irelia-core/src/main/java/cn/fanhub/irelia/core/upstream/UpstreamConfig.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.core.upstream; 17 | 18 | import java.io.Serializable; 19 | 20 | /** 21 | * 22 | * @author chengfan 23 | * @version $Id: UpstreamConfig.java, v 0.1 2018年04月24日 下午10:05 chengfan Exp $ 24 | */ 25 | public interface UpstreamConfig extends Serializable { 26 | /** 27 | * Gets app id. 28 | * 29 | * @return the app id 30 | */ 31 | String getAppName(); 32 | 33 | /** 34 | * Gets name. 35 | * 36 | * @return the name 37 | */ 38 | String getName(); 39 | } -------------------------------------------------------------------------------- /Irelia-server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | Irelia-parent 7 | cn.fanhub 8 | 0.0.1 9 | 10 | 4.0.0 11 | 12 | Irelia-server 13 | 14 | 15 | 16 | org.apache.maven.plugins 17 | maven-compiler-plugin 18 | 19 | 7 20 | 7 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | cn.fanhub 29 | Irelia-common-util 30 | ${project.version} 31 | 32 | 33 | cn.fanhub 34 | Irelia-upstream 35 | ${project.version} 36 | 37 | 38 | -------------------------------------------------------------------------------- /Irelia-server/src/main/java/cn/fanhub/irelia/server/Bootstrap.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.server; 17 | 18 | import cn.fanhub.irelia.upstream.UpstreamManager; 19 | import io.netty.bootstrap.ServerBootstrap; 20 | import io.netty.channel.ChannelFuture; 21 | import io.netty.channel.EventLoopGroup; 22 | import io.netty.channel.nio.NioEventLoopGroup; 23 | import io.netty.channel.socket.nio.NioServerSocketChannel; 24 | import lombok.Setter; 25 | import lombok.extern.slf4j.Slf4j; 26 | import org.springframework.beans.BeansException; 27 | import org.springframework.context.ApplicationContext; 28 | import org.springframework.context.ApplicationContextAware; 29 | 30 | import java.net.InetSocketAddress; 31 | 32 | /** 33 | * 34 | * @author chengfan 35 | * @version $Id: Bootstrap.java, v 0.1 2018年04月07日 下午4:05 chengfan Exp $ 36 | */ 37 | @Slf4j 38 | public class Bootstrap implements ApplicationContextAware { 39 | 40 | @Setter 41 | private int port; 42 | 43 | private ApplicationContext applicationContext; 44 | 45 | /** 46 | * 网关会单独起一个线程运行,否则会阻塞主线程。 47 | * todo 线程池 48 | * @throws InterruptedException 49 | */ 50 | public void start() throws InterruptedException { 51 | log.info("start netty server"); 52 | final Thread thread = new Thread(new Runnable() { 53 | @Override 54 | public void run() { 55 | EventLoopGroup boosGroup = new NioEventLoopGroup(); 56 | EventLoopGroup workerGroup = new NioEventLoopGroup(); 57 | try { 58 | Class.forName(UpstreamManager.class.getName(), true, Thread.currentThread().getContextClassLoader()); 59 | ServerBootstrap bootstrap = new ServerBootstrap(); 60 | bootstrap.group(boosGroup, workerGroup) 61 | .channel(NioServerSocketChannel.class) 62 | .localAddress(new InetSocketAddress(port)) 63 | .childHandler(new HttpSupportInitializer(applicationContext)); 64 | ChannelFuture future = bootstrap.bind().sync(); 65 | future.channel().closeFuture().sync(); 66 | 67 | } catch (InterruptedException e) { 68 | log.error("start netty server error :", e); 69 | } catch (ClassNotFoundException e) { 70 | log.error("start UpstreamManager error :", e); 71 | } finally { 72 | log.info("shutdown netty server"); 73 | try { 74 | boosGroup.shutdownGracefully().sync(); 75 | workerGroup.shutdownGracefully().sync(); 76 | } catch (InterruptedException e) { 77 | log.error("shutdown netty server error :", e); 78 | } 79 | } 80 | } 81 | }); 82 | thread.setDaemon(true); 83 | thread.start(); 84 | 85 | } 86 | 87 | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 88 | this.applicationContext = applicationContext; 89 | } 90 | } -------------------------------------------------------------------------------- /Irelia-server/src/main/java/cn/fanhub/irelia/server/HttpSupportInitializer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.server; 17 | 18 | import cn.fanhub.irelia.core.Handler; 19 | import io.netty.channel.Channel; 20 | import io.netty.channel.ChannelHandler; 21 | import io.netty.channel.ChannelInitializer; 22 | import io.netty.handler.codec.http.HttpContentCompressor; 23 | import io.netty.handler.codec.http.HttpObjectAggregator; 24 | import io.netty.handler.codec.http.HttpServerCodec; 25 | import lombok.extern.slf4j.Slf4j; 26 | import org.springframework.context.ApplicationContext; 27 | 28 | import java.util.ArrayList; 29 | import java.util.Collections; 30 | import java.util.Comparator; 31 | import java.util.List; 32 | import java.util.Map; 33 | import java.util.Map.Entry; 34 | 35 | /** 36 | * 37 | * @author chengfan 38 | * @version $Id: HttpSupportInitializer.java, v 0.1 2018年04月09日 下午10:25 chengfan Exp $ 39 | */ 40 | @Slf4j 41 | public class HttpSupportInitializer extends ChannelInitializer { 42 | 43 | private ApplicationContext applicationContext; 44 | 45 | private List> handlerList; 46 | 47 | public HttpSupportInitializer(ApplicationContext applicationContext) { 48 | this.applicationContext = applicationContext; 49 | init(); 50 | } 51 | 52 | private void init() { 53 | Map handlers = applicationContext.getBeansOfType(Handler.class); 54 | handlerList = new ArrayList<>(handlers.entrySet()); 55 | Collections.sort(handlerList, new Comparator>() { 56 | public int compare(Entry firstEntry, Entry secondEntry) { 57 | return firstEntry.getValue().order() - secondEntry.getValue().order(); 58 | } 59 | }); 60 | } 61 | 62 | /** 63 | * 默认只支持 http 的请求 64 | */ 65 | protected void initChannel(Channel channel) throws Exception { 66 | log.info("init pipeline"); 67 | for (Entry handlerEntry : handlerList) { 68 | if (handlerEntry.getKey().contains("http")) { 69 | channel.pipeline() 70 | .addLast("codec", new HttpServerCodec()) 71 | .addLast("aggregator", new HttpObjectAggregator(512 * 1024)) 72 | .addLast("compressor", new HttpContentCompressor()) 73 | ; 74 | } 75 | channel.pipeline() 76 | .addLast(handlerEntry.getKey(), ((ChannelHandler) handlerEntry.getValue())); 77 | } 78 | 79 | } 80 | } -------------------------------------------------------------------------------- /Irelia-server/src/main/java/cn/fanhub/irelia/server/future/ErrorChannelFutureListener.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.server.future; 17 | 18 | import cn.fanhub.irelia.common.utils.ResponseUtil; 19 | import cn.fanhub.irelia.core.exception.IreliaRuntimeException; 20 | import cn.fanhub.irelia.core.model.IreliaResponse; 21 | import io.netty.channel.ChannelFuture; 22 | import io.netty.channel.ChannelFutureListener; 23 | import io.netty.handler.codec.http.HttpResponseStatus; 24 | import lombok.extern.slf4j.Slf4j; 25 | 26 | /** 27 | * post handler 异常处理 28 | * @author chengfan 29 | * @version $Id: ErrorChannelFutureListener.java, v 0.1 2018年05月13日 下午7:38 chengfan Exp $ 30 | */ 31 | @Slf4j 32 | public class ErrorChannelFutureListener implements ChannelFutureListener { 33 | @Override 34 | public void operationComplete(ChannelFuture future) { 35 | if (!future.isSuccess()) { 36 | Throwable cause = future.cause(); 37 | if (cause instanceof IreliaRuntimeException) { 38 | log.error("post error" , cause); 39 | future.channel().close(); 40 | } 41 | log.error("error: " , cause); 42 | IreliaResponse response = new IreliaResponse(); 43 | ResponseUtil.buildExceptionResponse(cause, response); 44 | response.setContent("错误详情请查看日志"); 45 | // todo 有瑕疵啊 46 | ResponseUtil.send(future.channel().pipeline().lastContext(), response, HttpResponseStatus.BAD_GATEWAY); 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /Irelia-server/src/main/java/cn/fanhub/irelia/server/handler/AbstractConfigHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.server.handler; 17 | 18 | import cn.fanhub.irelia.core.handler.AbstractPreHandler; 19 | import cn.fanhub.irelia.core.model.Headers; 20 | import cn.fanhub.irelia.core.model.IreliaRequest; 21 | import cn.fanhub.irelia.core.model.RpcConfig; 22 | import cn.fanhub.irelia.core.model.SystemConfig; 23 | import cn.fanhub.irelia.core.upstream.UpstreamConfig; 24 | import cn.fanhub.irelia.server.http.HeaderKey; 25 | import com.alibaba.fastjson.JSON; 26 | import io.netty.buffer.ByteBuf; 27 | import io.netty.channel.ChannelHandler.Sharable; 28 | import io.netty.channel.ChannelHandlerContext; 29 | import io.netty.handler.codec.http.FullHttpRequest; 30 | import io.netty.handler.codec.http.HttpHeaders; 31 | import io.netty.util.CharsetUtil; 32 | import lombok.extern.slf4j.Slf4j; 33 | 34 | /** 35 | * 获得 rpc 配置的 handler, 需要使用方自行配置数据。 36 | * @author chengfan 37 | * @version $Id: AbstractConfigHandler.java, v 0.1 2018年04月30日 下午9:59 chengfan Exp $ 38 | */ 39 | @Sharable 40 | @Slf4j 41 | public abstract class AbstractConfigHandler extends AbstractPreHandler { 42 | 43 | @Override 44 | public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { 45 | FullHttpRequest httpRequest = (FullHttpRequest)msg; 46 | HttpHeaders headers = httpRequest.headers(); 47 | String rpcValue = headers.get(HeaderKey.rpcValue.name()); 48 | String body = getBody(httpRequest); 49 | 50 | RpcConfig config = getRpcConfig(rpcValue); 51 | 52 | IreliaRequest ireliaRequest = new IreliaRequest(); 53 | ireliaRequest.setRpcValue(rpcValue); 54 | 55 | Headers header = new Headers(); 56 | header.putAll(headers); 57 | ireliaRequest.setHeaders(header); 58 | 59 | // todo 参数支持的不够完善 60 | ireliaRequest.setRequestArgs(JSON.parseArray(body)); 61 | ireliaRequest.setRpcConfig(config); 62 | 63 | ireliaRequest.setSystemConfig(getSystemConfig(config.getAppName())); 64 | 65 | ireliaRequest.setUpstreamConfig(getUpstreamConfig(config.getAppName())); 66 | 67 | ctx.fireChannelRead(ireliaRequest); 68 | 69 | } 70 | 71 | @Override 72 | public int order() { 73 | return 10; 74 | } 75 | 76 | /** 77 | * 获取body参数 78 | * @param request 79 | * @return 80 | */ 81 | private String getBody(FullHttpRequest request){ 82 | ByteBuf buf = request.content(); 83 | return buf.toString(CharsetUtil.UTF_8); 84 | } 85 | 86 | abstract public RpcConfig getRpcConfig(String rpcValue); 87 | 88 | abstract public UpstreamConfig getUpstreamConfig(String rpcValue); 89 | 90 | abstract public SystemConfig getSystemConfig(String sysName); 91 | } -------------------------------------------------------------------------------- /Irelia-server/src/main/java/cn/fanhub/irelia/server/handler/HttpInboundHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.server.handler; 17 | 18 | import cn.fanhub.irelia.common.utils.ResponseUtil; 19 | import cn.fanhub.irelia.core.handler.AbstractPreHandler; 20 | import cn.fanhub.irelia.core.model.IreliaResponse; 21 | import cn.fanhub.irelia.core.model.IreliaResponseCode; 22 | import io.netty.channel.ChannelHandler.Sharable; 23 | import io.netty.channel.ChannelHandlerContext; 24 | import io.netty.handler.codec.http.FullHttpRequest; 25 | import io.netty.handler.codec.http.HttpMethod; 26 | import io.netty.handler.codec.http.HttpResponseStatus; 27 | import lombok.extern.slf4j.Slf4j; 28 | 29 | /** 30 | * 31 | * @author chengfan 32 | * @version $Id: HttpInboundHandler.java, v 0.1 2018年04月10日 下午9:00 chengfan Exp $ 33 | */ 34 | @Slf4j 35 | @Sharable 36 | public class HttpInboundHandler extends AbstractPreHandler { 37 | 38 | @Override 39 | public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { 40 | if(! (msg instanceof FullHttpRequest)){ 41 | IreliaResponse response = new IreliaResponse(); 42 | response.setCode(IreliaResponseCode.NOT_SUPPORT_REQUEST.getCode()); 43 | response.setMessage(IreliaResponseCode.NOT_SUPPORT_REQUEST.getMessage()); 44 | ResponseUtil.send(ctx, response, HttpResponseStatus.BAD_REQUEST); 45 | return; 46 | } 47 | FullHttpRequest httpRequest = (FullHttpRequest)msg; 48 | try{ 49 | String path=httpRequest.uri(); // 获取路径 50 | HttpMethod method = httpRequest.method();// 获取请求方法 51 | // 如果不是这个路径,就直接返回错误 52 | if(!"/irelia".equalsIgnoreCase(path) || !HttpMethod.POST.equals(method)){ 53 | IreliaResponse response = new IreliaResponse(); 54 | response.setCode(IreliaResponseCode.INVALID_QUERY_URL.getCode()); 55 | response.setMessage(IreliaResponseCode.INVALID_QUERY_URL.getMessage()); 56 | ResponseUtil.send(ctx, response, HttpResponseStatus.BAD_REQUEST); 57 | return; 58 | } 59 | // 如果是POST请求 todo 60 | ctx.fireChannelRead(msg); 61 | 62 | } catch(Exception e) { 63 | log.error("处理请求失败!", e); 64 | } finally { 65 | //释放请求 66 | httpRequest.release(); 67 | } 68 | } 69 | 70 | @Override 71 | public void channelActive(ChannelHandlerContext ctx) throws Exception { 72 | log.info("连接的客户端地址: {}", ctx.channel().remoteAddress()); 73 | super.channelActive(ctx); 74 | } 75 | 76 | public int order() { 77 | return 0; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /Irelia-server/src/main/java/cn/fanhub/irelia/server/handler/ResponseHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.server.handler; 17 | 18 | import cn.fanhub.irelia.core.handler.AbstractPostHandler; 19 | import io.netty.channel.ChannelHandlerContext; 20 | 21 | /** 22 | * 23 | * @author chengfan 24 | * @version $Id: ResponseHandler.java, v 0.1 2018年05月13日 下午12:39 chengfan Exp $ 25 | */ 26 | public class ResponseHandler extends AbstractPostHandler { 27 | 28 | @Override 29 | public void flush(ChannelHandlerContext ctx) throws Exception { 30 | super.flush(ctx); 31 | } 32 | 33 | @Override 34 | public int order() { 35 | return 3000; 36 | } 37 | } -------------------------------------------------------------------------------- /Irelia-server/src/main/java/cn/fanhub/irelia/server/handler/RouteHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.server.handler; 17 | 18 | import cn.fanhub.irelia.common.utils.ResponseUtil; 19 | import cn.fanhub.irelia.core.exception.IreliaRuntimeException; 20 | import cn.fanhub.irelia.core.handler.AbstractRouterHandler; 21 | import cn.fanhub.irelia.core.model.IreliaRequest; 22 | import cn.fanhub.irelia.core.model.IreliaResponse; 23 | import cn.fanhub.irelia.core.model.IreliaResponseCode; 24 | import cn.fanhub.irelia.server.handler.cache.CacheHelper; 25 | import cn.fanhub.irelia.upstream.IreliaUpstream; 26 | import cn.fanhub.irelia.upstream.UpstreamManager; 27 | import io.netty.channel.ChannelHandler.Sharable; 28 | import io.netty.channel.ChannelHandlerContext; 29 | import io.netty.handler.codec.http.HttpResponseStatus; 30 | import lombok.extern.slf4j.Slf4j; 31 | 32 | /** 33 | * 34 | * @author chengfan 35 | * @version $Id: RouteHandler.java, v 0.1 2018年04月09日 下午10:41 chengfan Exp $ 36 | */ 37 | @Slf4j 38 | @Sharable 39 | public class RouteHandler extends AbstractRouterHandler { 40 | 41 | @Override 42 | public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { 43 | IreliaRequest ireliaRequest = (IreliaRequest)msg; 44 | IreliaUpstream upstream = UpstreamManager.getUpstream(ireliaRequest.getUpstreamConfig().getName()); 45 | 46 | IreliaResponse ireliaResponse; 47 | try { 48 | ireliaResponse = upstream.invoke(ireliaRequest); 49 | } catch (Exception e) { 50 | throw new IreliaRuntimeException(IreliaResponseCode.BAD_REQUEST, "请求失败,请检查业务系统是否正常运行"); 51 | } 52 | 53 | if (((IreliaRequest) msg).getRpcConfig().getCacheConfig().isCache()) { 54 | CacheHelper.setValue(ireliaRequest, ireliaResponse); 55 | if (log.isInfoEnabled()) { 56 | log.info("{} response cached", ireliaRequest.getRpcValue()); 57 | } 58 | } 59 | ireliaResponse.setCode(IreliaResponseCode.SUCCESS.getCode()); 60 | ireliaResponse.setMessage(IreliaResponseCode.SUCCESS.getMessage()); 61 | ResponseUtil.send(ctx, ireliaResponse, HttpResponseStatus.OK); 62 | 63 | 64 | } 65 | 66 | public int order() { 67 | return 1000; 68 | } 69 | } -------------------------------------------------------------------------------- /Irelia-server/src/main/java/cn/fanhub/irelia/server/handler/SecurityHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.server.handler; 17 | 18 | import cn.fanhub.irelia.common.utils.ResponseUtil; 19 | import cn.fanhub.irelia.common.utils.SignUtil; 20 | import cn.fanhub.irelia.core.handler.AbstractPreHandler; 21 | import cn.fanhub.irelia.core.model.IreliaRequest; 22 | import cn.fanhub.irelia.core.model.IreliaResponse; 23 | import cn.fanhub.irelia.core.model.IreliaResponseCode; 24 | import cn.fanhub.irelia.core.model.RpcConfig; 25 | import cn.fanhub.irelia.server.http.HeaderKey; 26 | import io.netty.channel.ChannelHandler.Sharable; 27 | import io.netty.channel.ChannelHandlerContext; 28 | import io.netty.handler.codec.http.HttpResponseStatus; 29 | import lombok.extern.slf4j.Slf4j; 30 | import sun.security.rsa.RSAPublicKeyImpl; 31 | 32 | /** 33 | * 34 | * @author chengfan 35 | * @version $Id: SecurityHandler.java, v 0.1 2018年04月09日 下午10:48 chengfan Exp $ 36 | */ 37 | @Slf4j 38 | @Sharable 39 | public class SecurityHandler extends AbstractPreHandler { 40 | 41 | @Override 42 | public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { 43 | IreliaRequest ireliaRequest = (IreliaRequest)msg; 44 | RpcConfig rpcConfig = ireliaRequest.getRpcConfig(); 45 | if (!rpcConfig.isOpen()) { 46 | IreliaResponse response = new IreliaResponse(); 47 | response.setCode(IreliaResponseCode.NOT_OPEN_RPC.getCode()); 48 | response.setMessage(IreliaResponseCode.NOT_OPEN_RPC.getMessage()); 49 | ResponseUtil.send(ctx, response, HttpResponseStatus.BAD_REQUEST); 50 | return; 51 | } 52 | 53 | if (rpcConfig.isNeedSign()) { 54 | // 签名校验 55 | String transSign = ireliaRequest.getHeaders().get(HeaderKey.sign.name()); 56 | String publicKey = ireliaRequest.getSystemConfig().getPublicKey(); 57 | String body = ireliaRequest.getRequestArgs().toJSONString(); 58 | if (transSign == null || publicKey == null ) { 59 | IreliaResponse response = new IreliaResponse(); 60 | response.setCode(IreliaResponseCode.NO_SIGN_KEY_OR_VALUE.getCode()); 61 | response.setMessage(IreliaResponseCode.NO_SIGN_KEY_OR_VALUE.getMessage()); 62 | ResponseUtil.send(ctx, response, HttpResponseStatus.BAD_REQUEST); 63 | return; 64 | } 65 | boolean verifySign = SignUtil.verifySign(new RSAPublicKeyImpl(publicKey.getBytes()), body, transSign.getBytes()); 66 | if (!verifySign) { 67 | IreliaResponse response = new IreliaResponse(); 68 | response.setCode(IreliaResponseCode.SIGN_ERROR.getCode()); 69 | response.setMessage(IreliaResponseCode.SIGN_ERROR.getMessage()); 70 | ResponseUtil.send(ctx, response, HttpResponseStatus.BAD_REQUEST); 71 | return; 72 | } 73 | } 74 | ctx.fireChannelRead(ireliaRequest); 75 | } 76 | 77 | public int order() { 78 | return 20; 79 | } 80 | } -------------------------------------------------------------------------------- /Irelia-server/src/main/java/cn/fanhub/irelia/server/handler/cache/AbstractCacheHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.server.handler.cache; 17 | 18 | import cn.fanhub.irelia.common.utils.ResponseUtil; 19 | import cn.fanhub.irelia.core.handler.AbstractPreHandler; 20 | import cn.fanhub.irelia.core.model.IreliaRequest; 21 | import cn.fanhub.irelia.core.model.IreliaResponse; 22 | import io.netty.channel.ChannelHandlerContext; 23 | import io.netty.handler.codec.http.HttpResponseStatus; 24 | import org.springframework.beans.factory.InitializingBean; 25 | 26 | /** 27 | * 28 | * @author chengfan 29 | * @version $Id: AbstractCacheHandler.java, v 0.1 2018年06月11日 下午3:21 chengfan Exp $ 30 | */ 31 | public abstract class AbstractCacheHandler extends AbstractPreHandler implements InitializingBean { 32 | 33 | @Override 34 | public void afterPropertiesSet() throws Exception { 35 | CacheHelper.register(this); 36 | } 37 | 38 | @Override 39 | public void channelRead(ChannelHandlerContext ctx, Object msg) { 40 | IreliaRequest ireliaRequest = (IreliaRequest) msg; 41 | if (!hasCache(ireliaRequest)) { 42 | ctx.fireChannelRead(msg); 43 | return; 44 | } 45 | IreliaResponse response = cacheValue(ireliaRequest); 46 | if (response == null) { 47 | ctx.fireChannelRead(msg); 48 | } else { 49 | // 直接响应 50 | ResponseUtil.send(ctx, response, HttpResponseStatus.OK); 51 | } 52 | } 53 | 54 | 55 | abstract public boolean hasCache(IreliaRequest request); 56 | 57 | abstract public IreliaResponse cacheValue(IreliaRequest request); 58 | 59 | abstract public void put(IreliaRequest request, IreliaResponse response); 60 | 61 | } -------------------------------------------------------------------------------- /Irelia-server/src/main/java/cn/fanhub/irelia/server/handler/cache/CacheHelper.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.server.handler.cache; 17 | 18 | import cn.fanhub.irelia.core.model.IreliaRequest; 19 | import cn.fanhub.irelia.core.model.IreliaResponse; 20 | import org.springframework.util.Assert; 21 | 22 | /** 23 | * 24 | * @author chengfan 25 | * @version $Id: CacheHelper.java, v 0.1 2018年06月11日 下午5:26 chengfan Exp $ 26 | */ 27 | public class CacheHelper { 28 | 29 | private static AbstractCacheHandler CACHE_HANDLER; 30 | 31 | public static void register(AbstractCacheHandler cacheHandler) { 32 | CACHE_HANDLER = cacheHandler; 33 | } 34 | 35 | public static void setValue(IreliaRequest request, IreliaResponse response) { 36 | Assert.notNull(CACHE_HANDLER, "NO CACHE_HANDLER "); 37 | CACHE_HANDLER.put(request, response); 38 | } 39 | } -------------------------------------------------------------------------------- /Irelia-server/src/main/java/cn/fanhub/irelia/server/handler/cache/CacheModel.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.server.handler.cache; 17 | 18 | import cn.fanhub.irelia.core.model.IreliaResponse; 19 | import lombok.Data; 20 | 21 | /** 22 | * 23 | * @author chengfan 24 | * @version $Id: CacheModel.java, v 0.1 2018年06月14日 下午5:42 chengfan Exp $ 25 | */ 26 | @Data 27 | public class CacheModel { 28 | private long startTimestamp; 29 | private IreliaResponse response; 30 | } -------------------------------------------------------------------------------- /Irelia-server/src/main/java/cn/fanhub/irelia/server/handler/cache/GuavaCacheHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.server.handler.cache; 17 | 18 | import cn.fanhub.irelia.core.model.IreliaRequest; 19 | import cn.fanhub.irelia.core.model.IreliaResponse; 20 | import com.google.common.cache.CacheBuilder; 21 | import com.google.common.cache.CacheLoader; 22 | import com.google.common.cache.LoadingCache; 23 | import com.google.common.collect.Maps; 24 | import io.netty.channel.ChannelHandler.Sharable; 25 | 26 | import java.util.Map; 27 | 28 | /** 29 | * 30 | * @author chengfan 31 | * @version $Id: GuavaCacheHandler.java, v 0.1 2018年06月11日 下午3:34 chengfan Exp $ 32 | */ 33 | @Sharable 34 | public class GuavaCacheHandler extends AbstractCacheHandler { 35 | 36 | private static Map cacheMap = Maps.newConcurrentMap(); 37 | 38 | @Override 39 | public boolean hasCache(IreliaRequest request) { 40 | return request.getRpcConfig().getCacheConfig().isCache(); 41 | } 42 | 43 | @Override 44 | public IreliaResponse cacheValue(IreliaRequest request) { 45 | LoadingCache loadingCache = cacheMap.get(request.getRpcValue()); 46 | if (loadingCache == null) { 47 | return null; 48 | } 49 | return (IreliaResponse) loadingCache.getIfPresent(request.getRpcValue()); 50 | } 51 | 52 | @Override 53 | public void put(IreliaRequest request, final IreliaResponse response) { 54 | // 有性能问题 55 | LoadingCache ireliaResponseLoadingCache = CacheBuilder.newBuilder() 56 | .maximumSize(request.getRpcConfig().getCacheConfig().getCacheTime()) 57 | .build(new CacheLoader() { 58 | @Override 59 | public IreliaResponse load(String rpcValue) { 60 | return response; 61 | } 62 | }); 63 | cacheMap.put(request.getRpcValue(), ireliaResponseLoadingCache); 64 | } 65 | 66 | @Override 67 | public int order() { 68 | return 800; 69 | } 70 | 71 | } -------------------------------------------------------------------------------- /Irelia-server/src/main/java/cn/fanhub/irelia/server/handler/cache/SimpleCacheHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.server.handler.cache; 17 | 18 | import cn.fanhub.irelia.core.model.CacheConfig; 19 | import cn.fanhub.irelia.core.model.IreliaRequest; 20 | import cn.fanhub.irelia.core.model.IreliaResponse; 21 | import com.google.common.collect.Maps; 22 | import io.netty.channel.ChannelHandler.Sharable; 23 | 24 | import java.util.Map; 25 | 26 | /** 27 | * 28 | * @author chengfan 29 | * @version $Id: SimpleCacheHandler.java, v 0.1 2018年06月14日 下午5:41 chengfan Exp $ 30 | */ 31 | @Sharable 32 | public class SimpleCacheHandler extends AbstractCacheHandler { 33 | 34 | private static Map modelMap = Maps.newConcurrentMap(); 35 | 36 | @Override 37 | public boolean hasCache(IreliaRequest request) { 38 | CacheConfig cacheConfig = request.getRpcConfig().getCacheConfig(); 39 | if (!cacheConfig.isCache()) { 40 | return false; 41 | } 42 | CacheModel cacheModel = modelMap.get(getKey(request)); 43 | if (cacheModel == null) { 44 | return false; 45 | } 46 | long time = System.currentTimeMillis() - cacheModel.getStartTimestamp(); 47 | 48 | if (time > cacheConfig.getCacheTime()) { 49 | modelMap.remove(getKey(request)); 50 | return false; 51 | } 52 | 53 | return true; 54 | } 55 | 56 | @Override 57 | public IreliaResponse cacheValue(IreliaRequest request) { 58 | CacheModel cacheModel = modelMap.get(getKey(request)); 59 | if (cacheModel == null) { 60 | return null; 61 | } 62 | return cacheModel.getResponse(); 63 | } 64 | 65 | @Override 66 | public void put(IreliaRequest request, IreliaResponse response) { 67 | CacheModel cacheModel = new CacheModel(); 68 | cacheModel.setResponse(response); 69 | cacheModel.setStartTimestamp(System.currentTimeMillis()); 70 | modelMap.put(getKey(request), cacheModel); 71 | } 72 | 73 | @Override 74 | public int order() { 75 | return 800; 76 | } 77 | 78 | private String getKey(IreliaRequest request) { 79 | return request.getRpcValue() + request.getRequestArgs(); 80 | } 81 | } -------------------------------------------------------------------------------- /Irelia-server/src/main/java/cn/fanhub/irelia/server/handler/error/ErrorHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.server.handler.error; 17 | 18 | import cn.fanhub.irelia.common.utils.ResponseUtil; 19 | import cn.fanhub.irelia.core.handler.AbstractErrorHandler; 20 | import cn.fanhub.irelia.core.model.IreliaResponse; 21 | import cn.fanhub.irelia.server.future.ErrorChannelFutureListener; 22 | import io.netty.channel.ChannelHandler.Sharable; 23 | import io.netty.channel.ChannelHandlerContext; 24 | import io.netty.channel.ChannelPromise; 25 | import io.netty.handler.codec.http.HttpResponseStatus; 26 | import lombok.Setter; 27 | import lombok.extern.slf4j.Slf4j; 28 | 29 | import java.net.SocketAddress; 30 | 31 | /** 32 | * 全局异常处理 33 | * @author chengfan 34 | * @version $Id: ErrorHandler.java, v 0.1 2018年05月10日 下午8:43 chengfan Exp $ 35 | */ 36 | @Sharable 37 | @Slf4j 38 | public class ErrorHandler extends AbstractErrorHandler { 39 | 40 | @Setter 41 | private ErrorChannelFutureListener channelFutureListener; 42 | 43 | @Override 44 | public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) { 45 | ctx.connect(remoteAddress, localAddress, promise.addListener(channelFutureListener)); 46 | } 47 | 48 | @Override 49 | public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) { 50 | ctx.write(msg, promise.addListener(channelFutureListener)); 51 | } 52 | 53 | @Override 54 | public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) { 55 | ctx.bind(localAddress, promise.addListener(channelFutureListener)); 56 | } 57 | 58 | @Override 59 | public void close(ChannelHandlerContext ctx, ChannelPromise promise) { 60 | ctx.close(promise.addListener(channelFutureListener)); 61 | } 62 | 63 | @Override 64 | public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) { 65 | ctx.disconnect(promise.addListener(channelFutureListener)); 66 | } 67 | 68 | @Override 69 | public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) { 70 | ctx.deregister(promise.addListener(channelFutureListener)); 71 | } 72 | 73 | @Override 74 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { 75 | log.error(cause.getMessage(), cause); 76 | IreliaResponse response = new IreliaResponse(); 77 | ResponseUtil.buildExceptionResponse(cause, response); 78 | response.setContent("错误详情请查看日志"); 79 | ResponseUtil.send(ctx, response, HttpResponseStatus.BAD_GATEWAY); 80 | } 81 | 82 | @Override 83 | public int order() { 84 | return 2000; 85 | } 86 | } -------------------------------------------------------------------------------- /Irelia-server/src/main/java/cn/fanhub/irelia/server/handler/limit/AbstractLimitHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.server.handler.limit; 17 | 18 | import cn.fanhub.irelia.common.utils.ResponseUtil; 19 | import cn.fanhub.irelia.core.handler.AbstractPreHandler; 20 | import cn.fanhub.irelia.core.model.IreliaRequest; 21 | import cn.fanhub.irelia.core.model.IreliaResponse; 22 | import cn.fanhub.irelia.core.model.IreliaResponseCode; 23 | import io.netty.channel.ChannelHandlerContext; 24 | import io.netty.handler.codec.http.HttpResponseStatus; 25 | 26 | /** 27 | * 28 | * @author chengfan 29 | * @version $Id: AbstractLimitHandler.java, v 0.1 2018年05月13日 下午1:39 chengfan Exp $ 30 | */ 31 | public abstract class AbstractLimitHandler extends AbstractPreHandler { 32 | 33 | @Override 34 | public final void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { 35 | IreliaRequest ireliaRequest = (IreliaRequest)msg; 36 | if (shouldLimit(ireliaRequest)) { 37 | IreliaResponse response = new IreliaResponse(); 38 | response.setCode(IreliaResponseCode.RPC_BEEN_LIMITED.getCode()); 39 | response.setMessage(IreliaResponseCode.RPC_BEEN_LIMITED.getMessage()); 40 | response.setContent(ireliaRequest.getRpcConfig().getLimitConfig().getLimitTemplate()); 41 | ResponseUtil.send(ctx, response, HttpResponseStatus.OK); 42 | } else { 43 | ctx.fireChannelRead(ireliaRequest); 44 | } 45 | 46 | } 47 | 48 | @Override 49 | public int order() { 50 | return 110; 51 | } 52 | 53 | abstract boolean shouldLimit(IreliaRequest ireliaRequest); 54 | } -------------------------------------------------------------------------------- /Irelia-server/src/main/java/cn/fanhub/irelia/server/handler/limit/SimpleLimitHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.server.handler.limit; 17 | 18 | import cn.fanhub.irelia.core.model.IreliaRequest; 19 | import cn.fanhub.irelia.core.model.LimitConfig; 20 | import com.google.common.collect.Maps; 21 | import com.google.common.util.concurrent.RateLimiter; 22 | import io.netty.channel.ChannelHandler.Sharable; 23 | 24 | import java.util.Map; 25 | 26 | /** 27 | * 28 | * @author chengfan 29 | * @version $Id: SimpleLimitHandler.java, v 0.1 2018年05月13日 下午1:41 chengfan Exp $ 30 | */ 31 | @Sharable 32 | public class SimpleLimitHandler extends AbstractLimitHandler { 33 | 34 | private static Map limiterMap = Maps.newConcurrentMap(); 35 | 36 | @Override 37 | boolean shouldLimit(IreliaRequest ireliaRequest) { 38 | LimitConfig limitConfig = ireliaRequest.getRpcConfig().getLimitConfig(); 39 | if (limitConfig.isLimit()) { 40 | if (limitConfig.getFrequency() <= 0) { 41 | return true; 42 | } 43 | 44 | RateLimiter limiter = limiterMap.get(ireliaRequest.getRpcValue()); 45 | if (limiter == null) { 46 | limiter = RateLimiter.create(limitConfig.getFrequency());; 47 | limiterMap.put(ireliaRequest.getRpcValue(), limiter); 48 | } 49 | return !limiter.tryAcquire(); 50 | } 51 | return false; 52 | } 53 | } -------------------------------------------------------------------------------- /Irelia-server/src/main/java/cn/fanhub/irelia/server/http/HeaderKey.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.server.http; 17 | 18 | /** 19 | * 20 | * @author chengfan 21 | * @version $Id: HeaderKey.java, v 0.1 2018年04月11日 下午10:10 chengfan Exp $ 22 | */ 23 | 24 | public enum HeaderKey { 25 | rpcValue, 26 | version, 27 | sign, 28 | } -------------------------------------------------------------------------------- /Irelia-server/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | %d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger - %msg%n 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | ERROR 34 | 35 | ACCEPT 36 | 37 | DENY 38 | 39 | 40 | 41 | 42 | 43 | ${log_dir}/error/%d{yyyy-MM-dd}/error-log.log 44 | 45 | 46 | ${maxHistory} 47 | 48 | 49 | 50 | 51 | %d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger - %msg%n 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | WARN 63 | 64 | ACCEPT 65 | 66 | DENY 67 | 68 | 69 | 70 | ${log_dir}/warn/%d{yyyy-MM-dd}/warn-log.log 71 | ${maxHistory} 72 | 73 | 74 | %d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger - %msg%n 75 | 76 | 77 | 78 | 79 | 80 | 81 | INFO 82 | ACCEPT 83 | DENY 84 | 85 | 86 | ${log_dir}/info/%d{yyyy-MM-dd}/info-log.log 87 | ${maxHistory} 88 | 89 | 90 | %d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger - %msg%n 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | -------------------------------------------------------------------------------- /Irelia-spi-core/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | Irelia-parent 7 | cn.fanhub 8 | 0.0.1 9 | 10 | 4.0.0 11 | 12 | Irelia-spi-core 13 | 14 | 15 | 16 | cn.fanhub 17 | Irelia-core 18 | ${project.version} 19 | 20 | 21 | -------------------------------------------------------------------------------- /Irelia-spi-core/src/main/java/cn/fanhub/irelia/spi/core/IreliaService.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.spi.core; 17 | 18 | import cn.fanhub.irelia.core.model.IreliaRequest; 19 | import cn.fanhub.irelia.core.model.IreliaResponse; 20 | 21 | import java.io.Serializable; 22 | import java.lang.reflect.InvocationTargetException; 23 | 24 | /** 25 | * 26 | * @author chengfan 27 | * @version $Id: IreliaService.java, v 0.1 2018年04月22日 上午11:51 chengfan Exp $ 28 | */ 29 | public interface IreliaService extends Serializable { 30 | 31 | IreliaResponse invoke(IreliaRequest request) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException; 32 | 33 | void setIreliaServiceHolder(IreliaServiceHolder serviceHolder); 34 | 35 | IreliaServiceHolder getIreliaServiceHolder(); 36 | } -------------------------------------------------------------------------------- /Irelia-spi-core/src/main/java/cn/fanhub/irelia/spi/core/IreliaServiceHolder.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.spi.core; 17 | 18 | import cn.fanhub.irelia.core.model.RpcConfig; 19 | import cn.fanhub.irelia.spi.core.model.IreliaBean; 20 | 21 | import java.io.Serializable; 22 | import java.util.List; 23 | 24 | /** 25 | * 26 | * @author chengfan 27 | * @version $Id: IreliaServiceHolder.java, v 0.1 2018年04月29日 下午11:17 chengfan Exp $ 28 | */ 29 | public interface IreliaServiceHolder extends Serializable { 30 | void loadRpc(String sysName, Object rpcBean); 31 | 32 | IreliaBean getIreliaBean(String rpcValue); 33 | 34 | List getBeansBySysName(String sysName); 35 | 36 | List getRpcConfig(String sysName); 37 | } -------------------------------------------------------------------------------- /Irelia-spi-core/src/main/java/cn/fanhub/irelia/spi/core/IreliaServiceManager.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.spi.core; 17 | 18 | import cn.fanhub.irelia.core.exception.IreliaRuntimeException; 19 | import cn.fanhub.irelia.core.model.IreliaResponseCode; 20 | import lombok.extern.slf4j.Slf4j; 21 | 22 | import java.util.Map; 23 | import java.util.concurrent.ConcurrentHashMap; 24 | 25 | /** 26 | * 27 | * @author chengfan 28 | * @version $Id: IreliaServiceManager.java, v 0.1 2018年04月10日 下午10:52 chengfan Exp $ 29 | */ 30 | @Slf4j 31 | public class IreliaServiceManager { 32 | 33 | private static final Map SERVICE_MAP = new ConcurrentHashMap(); 34 | 35 | public static void register(String appName, IreliaService service) { 36 | if (log.isDebugEnabled()) { 37 | log.debug("register ireliaService "); 38 | } 39 | SERVICE_MAP.put(appName, service); 40 | } 41 | 42 | public static IreliaService getService(String appName) throws IreliaRuntimeException { 43 | IreliaService ireliaService = SERVICE_MAP.get(appName); 44 | if (ireliaService == null) { 45 | log.error("not found this service:" + appName); 46 | throw new IreliaRuntimeException(IreliaResponseCode.SERVER_ERR, "not found this service" + appName); 47 | } 48 | return ireliaService; 49 | } 50 | 51 | } -------------------------------------------------------------------------------- /Irelia-spi-core/src/main/java/cn/fanhub/irelia/spi/core/annotation/Rpc.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.spi.core.annotation; 17 | 18 | import java.lang.annotation.ElementType; 19 | import java.lang.annotation.Retention; 20 | import java.lang.annotation.RetentionPolicy; 21 | import java.lang.annotation.Target; 22 | 23 | /** 24 | * 25 | * @author chengfan 26 | * @version $Id: Rpc.java, v 0.1 2018年04月10日 下午10:53 chengfan Exp $ 27 | */ 28 | @Retention(RetentionPolicy.RUNTIME) 29 | @Target(ElementType.METHOD) 30 | public @interface Rpc { 31 | 32 | String value(); 33 | 34 | String name() default ""; 35 | 36 | String desc() default ""; 37 | } -------------------------------------------------------------------------------- /Irelia-spi-core/src/main/java/cn/fanhub/irelia/spi/core/impl/IreliaServiceHolderImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.spi.core.impl; 17 | 18 | import cn.fanhub.irelia.core.model.CacheConfig; 19 | import cn.fanhub.irelia.core.model.LimitConfig; 20 | import cn.fanhub.irelia.core.model.RpcConfig; 21 | import cn.fanhub.irelia.spi.core.model.IreliaBean; 22 | import cn.fanhub.irelia.spi.core.IreliaServiceHolder; 23 | import cn.fanhub.irelia.spi.core.model.MethodInfo; 24 | import cn.fanhub.irelia.spi.core.annotation.Rpc; 25 | import com.google.common.collect.Lists; 26 | import com.google.common.collect.Maps; 27 | import org.apache.commons.lang3.reflect.MethodUtils; 28 | 29 | import java.lang.reflect.Method; 30 | import java.util.List; 31 | import java.util.Map; 32 | 33 | /** 34 | * 35 | * @author chengfan 36 | * @version $Id: IreliaServiceHolderImpl.java, v 0.1 2018年04月25日 下午10:40 chengfan Exp $ 37 | */ 38 | public class IreliaServiceHolderImpl implements IreliaServiceHolder { 39 | 40 | private final Map IreliaBeansMap = Maps.newConcurrentMap(); 41 | 42 | private final Map> rpcConfigMap = Maps.newConcurrentMap(); 43 | 44 | 45 | private final Map> sysTemBeansMap = Maps.newConcurrentMap(); 46 | 47 | public void loadRpc(String sysName, Object rpcBean) { 48 | 49 | List beanList = Lists.newCopyOnWriteArrayList(); 50 | List configList = Lists.newCopyOnWriteArrayList(); 51 | for (Class intf : rpcBean.getClass().getInterfaces()) { 52 | Method[] methods = MethodUtils.getMethodsWithAnnotation(intf, Rpc.class); 53 | 54 | for (Method method : methods) { 55 | 56 | Rpc annotation = method.getAnnotation(Rpc.class); 57 | 58 | Class[] parameterTypes = method.getParameterTypes(); 59 | String[] paramNames = new String[parameterTypes.length]; 60 | for (int i = 0; i < parameterTypes.length; i++) { 61 | paramNames[i] = parameterTypes[i].getName(); 62 | } 63 | MethodInfo methodInfo = MethodInfo 64 | .builder() 65 | .paramTypes(parameterTypes) 66 | .paramNames(paramNames) 67 | //.intf(intf) 68 | .methodName(method.getName()) 69 | //.method(method) 70 | .returnType(method.getReturnType()) 71 | .build(); 72 | 73 | IreliaBean ireliaBean = IreliaBean 74 | .builder() 75 | .rpcValue(annotation.value()) 76 | .rpcName(annotation.name()) 77 | .des(annotation.desc()) 78 | .impl(rpcBean) 79 | .methodInfo(methodInfo) 80 | .build(); 81 | 82 | RpcConfig rpcConfig = new RpcConfig(); 83 | rpcConfig.setAppName(sysName); 84 | rpcConfig.setRpcName(annotation.name()); 85 | rpcConfig.setRpcValue(annotation.value()); 86 | rpcConfig.setDes(annotation.desc()); 87 | rpcConfig.setMethodName(method.getName()); 88 | rpcConfig.setItfName(intf.getName()); 89 | rpcConfig.setCacheConfig(new CacheConfig()); 90 | rpcConfig.setLimitConfig(new LimitConfig()); 91 | 92 | 93 | IreliaBeansMap.put(annotation.value(), ireliaBean); 94 | configList.add(rpcConfig); 95 | beanList.add(ireliaBean); 96 | } 97 | } 98 | if (beanList.size() > 0) { 99 | sysTemBeansMap.put(sysName, beanList); 100 | rpcConfigMap.put(sysName, configList); 101 | } 102 | 103 | } 104 | 105 | public IreliaBean getIreliaBean(String rpcValue) { 106 | return IreliaBeansMap.get(rpcValue); 107 | } 108 | 109 | public List getBeansBySysName(String sysName) { 110 | return sysTemBeansMap.get(sysName); 111 | } 112 | 113 | 114 | public List getRpcConfig(String sysName) { 115 | return rpcConfigMap.get(sysName); 116 | } 117 | 118 | 119 | } -------------------------------------------------------------------------------- /Irelia-spi-core/src/main/java/cn/fanhub/irelia/spi/core/impl/IreliaServiceImpl.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.spi.core.impl; 17 | 18 | import cn.fanhub.irelia.core.model.IreliaRequest; 19 | import cn.fanhub.irelia.core.model.IreliaResponse; 20 | import cn.fanhub.irelia.spi.core.model.IreliaBean; 21 | import cn.fanhub.irelia.spi.core.IreliaService; 22 | import cn.fanhub.irelia.spi.core.IreliaServiceHolder; 23 | import cn.fanhub.irelia.spi.core.model.MethodInfo; 24 | import org.springframework.util.ReflectionUtils; 25 | 26 | import java.lang.reflect.InvocationTargetException; 27 | import java.lang.reflect.Method; 28 | 29 | /** 30 | * 31 | * @author chengfan 32 | * @version $Id: IreliaServiceImpl.java, v 0.1 2018年04月24日 下午10:37 chengfan Exp $ 33 | */ 34 | public class IreliaServiceImpl implements IreliaService { 35 | 36 | private IreliaServiceHolder ireliaServiceHolder; 37 | 38 | 39 | public IreliaResponse invoke(IreliaRequest request) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { 40 | IreliaResponse response = new IreliaResponse(); 41 | IreliaBean ireliaBean = ireliaServiceHolder.getIreliaBean(request.getRpcValue()); 42 | MethodInfo methodInfo = ireliaBean.getMethodInfo(); 43 | //Object o = MethodUtils.invokeExactMethod(ireliaBean.getImpl(), methodInfo.getMethodName(), request.getRequestArgs(), 44 | // 45 | // methodInfo.getParamTypes()); 46 | 47 | Method method = ireliaBean.getImpl().getClass().getMethod(methodInfo.getMethodName(), methodInfo.getParamTypes()); 48 | //Method method = methodInfo.getMethod(); 49 | ReflectionUtils.makeAccessible(method); 50 | Object[] params = new Object[request.getRequestArgs().size()]; 51 | 52 | for (int i = 0; i < request.getRequestArgs().size(); i++) { 53 | params[i] = request.getRequestArgs().getString(i); 54 | } 55 | Object o = method.invoke(ireliaBean.getImpl(), params); 56 | response.setContent(o); 57 | return response; 58 | } 59 | 60 | public IreliaServiceHolder getIreliaServiceHolder() { 61 | return ireliaServiceHolder; 62 | } 63 | 64 | public void setIreliaServiceHolder(IreliaServiceHolder ireliaServiceHolder) { 65 | this.ireliaServiceHolder = ireliaServiceHolder; 66 | } 67 | } -------------------------------------------------------------------------------- /Irelia-spi-core/src/main/java/cn/fanhub/irelia/spi/core/model/IreliaBean.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.spi.core.model; 17 | 18 | import lombok.Builder; 19 | import lombok.Getter; 20 | 21 | import java.io.Serializable; 22 | 23 | /** 24 | * 25 | * @author chengfan 26 | * @version $Id: IreliaBean.java, v 0.1 2018年04月26日 下午10:22 chengfan Exp $ 27 | */ 28 | @Builder 29 | @Getter 30 | public class IreliaBean implements Serializable { 31 | 32 | private MethodInfo methodInfo; 33 | 34 | private String rpcValue; 35 | 36 | private String rpcName; 37 | 38 | private String des; 39 | 40 | private Object impl; 41 | } -------------------------------------------------------------------------------- /Irelia-spi-core/src/main/java/cn/fanhub/irelia/spi/core/model/MethodInfo.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.spi.core.model; 17 | 18 | import lombok.Builder; 19 | import lombok.Getter; 20 | 21 | import java.io.Serializable; 22 | 23 | /** 24 | * 25 | * @author chengfan 26 | * @version $Id: MethodInfo.java, v 0.1 2018年04月26日 下午10:22 chengfan Exp $ 27 | */ 28 | 29 | @Builder 30 | @Getter 31 | public class MethodInfo implements Serializable { 32 | private String[] paramNames; 33 | private Class[] paramTypes; 34 | private Class returnType; 35 | private String methodName; 36 | //private Class intf; 37 | //private Method method; 38 | } -------------------------------------------------------------------------------- /Irelia-spi-dubbo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | Irelia-parent 7 | cn.fanhub 8 | 0.0.1 9 | 10 | 4.0.0 11 | 12 | Irelia-spi-dubbo 13 | 14 | 15 | 16 | cn.fanhub 17 | Irelia-spi-core 18 | ${project.version} 19 | 20 | 21 | com.github.sgroschupf 22 | zkclient 23 | 24 | 25 | com.alibaba 26 | dubbo 27 | 28 | 29 | -------------------------------------------------------------------------------- /Irelia-spi-dubbo/src/main/java/cn/fanhub/irelia/spi/dubbo/DubboClient.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.spi.dubbo; 17 | 18 | import cn.fanhub.irelia.spi.core.IreliaService; 19 | import com.alibaba.dubbo.config.ApplicationConfig; 20 | import com.alibaba.dubbo.config.ProtocolConfig; 21 | import com.alibaba.dubbo.config.RegistryConfig; 22 | import com.alibaba.dubbo.config.ServiceConfig; 23 | 24 | /** 25 | * 26 | * @author chengfan 27 | * @version $Id: DubboClient.java, v 0.1 2018年04月30日 下午1:29 chengfan Exp $ 28 | */ 29 | public class DubboClient { 30 | 31 | public void appServiceRegister(String appName, IreliaService ireliaService, String url, Integer port, Integer threads) { 32 | /** 当前应用配置, 配置应用名*/ 33 | ApplicationConfig application = new ApplicationConfig(); 34 | application.setName(appName); 35 | 36 | /** 连接注册中心配置*/ 37 | RegistryConfig registry = new RegistryConfig(); 38 | registry.setAddress(url); 39 | 40 | /** 服务提供者协议配置 threads线程池大小*/ 41 | ProtocolConfig protocol = new ProtocolConfig(); 42 | protocol.setName("dubbo"); 43 | protocol.setThreads(threads); 44 | protocol.setPort(port); 45 | 46 | // 服务提供者暴露服务配置 47 | ServiceConfig service = new ServiceConfig(); 48 | service.setApplication(application); 49 | service.setRegistry(registry); // 多个注册中心可以用setRegistries() 50 | service.setProtocol(protocol); // 多个协议可以用setProtocols() 51 | service.setInterface(IreliaService.class); 52 | service.setRef(ireliaService); 53 | service.setVersion("1.0.0"); 54 | service.setGroup(appName); 55 | 56 | // 暴露及注册服务 57 | service.export(); 58 | 59 | } 60 | } -------------------------------------------------------------------------------- /Irelia-spi-dubbo/src/main/java/cn/fanhub/irelia/spi/dubbo/DubboStarter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.spi.dubbo; 17 | 18 | import cn.fanhub.irelia.spi.core.IreliaService; 19 | import cn.fanhub.irelia.spi.core.IreliaServiceHolder; 20 | import cn.fanhub.irelia.spi.core.impl.IreliaServiceHolderImpl; 21 | import cn.fanhub.irelia.spi.core.impl.IreliaServiceImpl; 22 | import lombok.Setter; 23 | import lombok.extern.slf4j.Slf4j; 24 | import org.springframework.context.ApplicationContext; 25 | import org.springframework.context.ApplicationContextAware; 26 | 27 | /** 28 | * 29 | * @author chengfan 30 | * @version $Id: DubboStarter.java, v 0.1 2018年04月30日 上午11:18 chengfan Exp $ 31 | */ 32 | @Setter 33 | @Slf4j 34 | public class DubboStarter implements ApplicationContextAware { 35 | 36 | private ApplicationContext applicationContext; 37 | 38 | private String registryUrl; 39 | 40 | private String appName; 41 | 42 | private Integer protocolPort; 43 | 44 | private Integer serviceThreads; 45 | 46 | public void init() { 47 | if (log.isDebugEnabled()) { 48 | log.debug("init dubbo irelia service"); 49 | } 50 | IreliaServiceHolder holder = new IreliaServiceHolderImpl(); 51 | IreliaService service = new IreliaServiceImpl(); 52 | service.setIreliaServiceHolder(holder); 53 | 54 | for (String springBeanName : this.applicationContext.getBeanDefinitionNames()) { 55 | holder.loadRpc(appName, this.applicationContext.getBean(springBeanName)); 56 | } 57 | 58 | new DubboClient().appServiceRegister(appName, service, registryUrl, protocolPort, serviceThreads); 59 | } 60 | 61 | } -------------------------------------------------------------------------------- /Irelia-test/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | Irelia-parent 7 | cn.fanhub 8 | 0.0.1 9 | 10 | 4.0.0 11 | 12 | Irelia-test 13 | 14 | 15 | 16 | cn.fanhub 17 | Irelia-server 18 | ${project.version} 19 | 20 | 21 | cn.fanhub 22 | Irelia-upstream-dubbo 23 | ${project.version} 24 | 25 | 26 | junit 27 | junit 28 | 29 | 30 | -------------------------------------------------------------------------------- /Irelia-test/src/main/java/cn/fanhub/irelia/demo/Starter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.demo; 17 | 18 | import cn.fanhub.irelia.demo.config.SpringConfig; 19 | import org.springframework.context.ApplicationContext; 20 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 21 | 22 | import java.io.IOException; 23 | 24 | /** 25 | * 26 | * @author chengfan 27 | * @version $Id: Starter.java, v 0.1 2018年04月18日 下午9:53 chengfan Exp $ 28 | */ 29 | public class Starter { 30 | public static void main(String[] args) throws IOException { 31 | ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class); 32 | //for (Entry entry : UpstreamManager.getUpstreamMap().entrySet()) { 33 | // System.out.println(entry.getKey()); // 第一次会加载很久 bio 需要升级 34 | //} 35 | // 36 | //for (Entry entry : UpstreamManager.getUpstreamMap().entrySet()) { 37 | // System.out.println(entry.getKey()); // 第二次会加载很快 38 | //} 39 | //System.in.read(new byte[2]); 40 | 41 | //System.out.println(applicationContext.getBean(TestService.class)); 42 | } 43 | } -------------------------------------------------------------------------------- /Irelia-test/src/main/java/cn/fanhub/irelia/demo/config/SpringConfig.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.demo.config; 17 | 18 | import cn.fanhub.irelia.server.Bootstrap; 19 | import cn.fanhub.irelia.server.handler.HttpInboundHandler; 20 | import cn.fanhub.irelia.server.handler.RouteHandler; 21 | import cn.fanhub.irelia.server.handler.SecurityHandler; 22 | import org.springframework.context.annotation.Bean; 23 | import org.springframework.context.annotation.ComponentScan; 24 | import org.springframework.context.annotation.Configuration; 25 | 26 | /** 27 | * 28 | * @author chengfan 29 | * @version $Id: SpringConfig.java, v 0.1 2018年04月18日 下午9:53 chengfan Exp $ 30 | */ 31 | 32 | @Configuration 33 | @ComponentScan(basePackages = "cn.fanhub.irelia.demo") 34 | public class SpringConfig { 35 | 36 | // 使用 http 服务 37 | @Bean 38 | public HttpInboundHandler httpInboundHandler() { 39 | return new HttpInboundHandler(); 40 | } 41 | 42 | // 添加默认路由组件 43 | @Bean 44 | public RouteHandler routeHandler() { 45 | return new RouteHandler(); 46 | } 47 | 48 | // 添加默认身份验证组件 49 | @Bean 50 | public SecurityHandler securityHandler() { 51 | return new SecurityHandler(); 52 | } 53 | 54 | // 启动网关 55 | @Bean(initMethod = "start") 56 | public Bootstrap bootstrap() { 57 | Bootstrap bootstrap = new Bootstrap(); 58 | bootstrap.setPort(9876); 59 | return bootstrap; 60 | } 61 | } -------------------------------------------------------------------------------- /Irelia-test/src/test/java/TestBootstrap.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import cn.fanhub.irelia.server.Bootstrap; 18 | import org.junit.Test; 19 | 20 | /** 21 | * 22 | * @author chengfan 23 | * @version $Id: TestBootstrap.java, v 0.1 2018年04月09日 下午10:33 chengfan Exp $ 24 | */ 25 | public class TestBootstrap { 26 | 27 | @Test 28 | public void test() throws InterruptedException { 29 | Bootstrap bootstrap = new Bootstrap(); 30 | bootstrap.setPort(8765); 31 | 32 | bootstrap.start(); 33 | 34 | } 35 | } -------------------------------------------------------------------------------- /Irelia-upstream-dubbo/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | Irelia-parent 7 | cn.fanhub 8 | 0.0.1 9 | 10 | 4.0.0 11 | 12 | Irelia-upstream-dubbo 13 | 14 | 15 | 16 | com.github.sgroschupf 17 | zkclient 18 | 19 | 20 | com.alibaba 21 | dubbo 22 | 23 | 24 | cn.fanhub 25 | Irelia-upstream 26 | ${project.version} 27 | 28 | 29 | cn.fanhub 30 | Irelia-spi-dubbo 31 | ${project.version} 32 | 33 | 34 | org.projectlombok 35 | lombok 36 | 37 | 38 | -------------------------------------------------------------------------------- /Irelia-upstream-dubbo/src/main/java/cn/fanhub/irelia/upstream/dubbo/DubboReqest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.upstream.dubbo; 17 | 18 | import lombok.Data; 19 | 20 | /** 21 | * 22 | * @author chengfan 23 | * @version $Id: DubboReqest.java, v 0.1 2018年04月11日 下午9:53 chengfan Exp $ 24 | */ 25 | @Data 26 | public class DubboReqest { 27 | 28 | private String interfaceName; 29 | 30 | private String methodName; 31 | 32 | private String version; 33 | 34 | private String[] paramTypes; 35 | 36 | private Object[] params; 37 | } -------------------------------------------------------------------------------- /Irelia-upstream-dubbo/src/main/java/cn/fanhub/irelia/upstream/dubbo/DubboServiceManager.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.upstream.dubbo; 17 | 18 | import cn.fanhub.irelia.core.exception.IreliaRuntimeException; 19 | import cn.fanhub.irelia.core.model.IreliaResponseCode; 20 | import cn.fanhub.irelia.spi.core.IreliaService; 21 | import cn.fanhub.irelia.spi.core.IreliaServiceManager; 22 | import com.alibaba.dubbo.config.ApplicationConfig; 23 | import com.alibaba.dubbo.config.ReferenceConfig; 24 | import com.alibaba.dubbo.config.RegistryConfig; 25 | import lombok.extern.slf4j.Slf4j; 26 | 27 | /** 28 | * 29 | * @author chengfan 30 | * @version $Id: DubboServiceManager.java, v 0.1 2018年04月11日 下午9:41 chengfan Exp $ 31 | */ 32 | @Slf4j 33 | public class DubboServiceManager { 34 | 35 | public static DubboServiceManager getInstance() { 36 | return DubboServiceManagerHolder.INSTANCE; 37 | } 38 | 39 | private DubboServiceManager() { 40 | 41 | } 42 | 43 | private static class DubboServiceManagerHolder { 44 | private final static DubboServiceManager INSTANCE = new DubboServiceManager(); 45 | } 46 | 47 | private IreliaService createService(DubboUpstreamConfig config) { 48 | // 当前应用配置 49 | ApplicationConfig application = new ApplicationConfig(); 50 | application.setName(config.getAppName()); 51 | 52 | // 连接注册中心配置 53 | RegistryConfig registry = new RegistryConfig(); 54 | registry.setAddress(config.getAddress()); 55 | registry.setUsername(config.getUsername()); 56 | registry.setPassword(config.getPassword()); 57 | registry.setGroup(config.getAppName()); 58 | 59 | ReferenceConfig reference = new ReferenceConfig(); 60 | reference.setApplication(application); 61 | reference.setRegistry(registry); // 多个注册中心可以用setRegistries() 62 | reference.setInterface(IreliaService.class); 63 | reference.setGroup(config.getAppName()); 64 | reference.setVersion("1.0.0"); 65 | 66 | IreliaService service = reference.get(); 67 | // 缓存 68 | IreliaServiceManager.register(config.getAppName(), service); 69 | return service; 70 | } 71 | 72 | public IreliaService getService(DubboUpstreamConfig config) throws IreliaRuntimeException { 73 | IreliaService service = null; 74 | try { 75 | service = IreliaServiceManager.getService(config.getAppName()); 76 | } catch (IreliaRuntimeException e) { 77 | service = createService(config); 78 | if (log.isInfoEnabled()) { 79 | log.info("try load service"); 80 | } 81 | 82 | if (service == null) { 83 | log.error("load error"); 84 | throw new IreliaRuntimeException(IreliaResponseCode.SERVER_ERR, "try load fail!"); 85 | } 86 | } 87 | return service; 88 | } 89 | 90 | public IreliaService register(DubboUpstreamConfig config) { 91 | return createService(config); 92 | } 93 | 94 | } -------------------------------------------------------------------------------- /Irelia-upstream-dubbo/src/main/java/cn/fanhub/irelia/upstream/dubbo/DubboUpstream.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.upstream.dubbo; 17 | 18 | import cn.fanhub.irelia.core.exception.IreliaRuntimeException; 19 | import cn.fanhub.irelia.core.model.IreliaRequest; 20 | import cn.fanhub.irelia.core.model.IreliaResponse; 21 | import cn.fanhub.irelia.spi.core.IreliaService; 22 | import cn.fanhub.irelia.upstream.IreliaUpstream; 23 | import cn.fanhub.irelia.upstream.UpstreamManager; 24 | import lombok.extern.slf4j.Slf4j; 25 | 26 | import java.lang.reflect.InvocationTargetException; 27 | 28 | /** 29 | * 30 | * @author chengfan 31 | * @version $Id: DubboUpstream.java, v 0.1 2018年04月23日 下午9:32 chengfan Exp $ 32 | */ 33 | @Slf4j 34 | public class DubboUpstream implements IreliaUpstream { 35 | 36 | static { 37 | if (log.isInfoEnabled()) { 38 | log.info("DubboUpstream start register"); 39 | } 40 | UpstreamManager.register(DubboUpstreamHolder.INSTANCE); 41 | } 42 | 43 | private static class DubboUpstreamHolder { 44 | private static final DubboUpstream INSTANCE = new DubboUpstream(); 45 | } 46 | 47 | private DubboUpstream() { 48 | 49 | } 50 | 51 | public IreliaResponse invoke(IreliaRequest request) 52 | throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, IreliaRuntimeException { 53 | IreliaService service = DubboServiceManager.getInstance().getService((DubboUpstreamConfig) request.getUpstreamConfig()); 54 | return service.invoke(request); 55 | } 56 | 57 | public String name() { 58 | return "dubbo"; 59 | } 60 | } -------------------------------------------------------------------------------- /Irelia-upstream-dubbo/src/main/java/cn/fanhub/irelia/upstream/dubbo/DubboUpstreamConfig.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.upstream.dubbo; 17 | 18 | import cn.fanhub.irelia.core.upstream.UpstreamConfig; 19 | import lombok.Data; 20 | 21 | /** 22 | * 23 | * @author chengfan 24 | * @version $Id: DubboUpstreamConfig.java, v 0.1 2018年04月11日 下午9:41 chengfan Exp $ 25 | */ 26 | @Data 27 | public class DubboUpstreamConfig implements UpstreamConfig { 28 | private String name; 29 | 30 | private String address; 31 | 32 | private String username; 33 | 34 | private String password; 35 | 36 | private String appName; 37 | 38 | } -------------------------------------------------------------------------------- /Irelia-upstream-dubbo/src/main/resources/irelia.upstream.de: -------------------------------------------------------------------------------- 1 | cn.fanhub.irelia.upstream.dubbo.DubboUpstream -------------------------------------------------------------------------------- /Irelia-upstream/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | Irelia-parent 7 | cn.fanhub 8 | 0.0.1 9 | 10 | 4.0.0 11 | 12 | Irelia-upstream 13 | 14 | 15 | 16 | cn.fanhub 17 | Irelia-core 18 | ${project.version} 19 | 20 | 21 | -------------------------------------------------------------------------------- /Irelia-upstream/src/main/java/cn/fanhub/irelia/upstream/IreliaUpstream.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.upstream; 17 | 18 | import cn.fanhub.irelia.core.exception.IreliaRuntimeException; 19 | import cn.fanhub.irelia.core.model.IreliaRequest; 20 | import cn.fanhub.irelia.core.model.IreliaResponse; 21 | 22 | import java.lang.reflect.InvocationTargetException; 23 | 24 | /** 25 | * 26 | * @author chengfan 27 | * @version $Id: IreliaUpstream.java, v 0.1 2018年04月21日 下午4:22 chengfan Exp $ 28 | */ 29 | public interface IreliaUpstream { 30 | 31 | /** 32 | * Invoke irelia response. 33 | * 34 | * @param request the request 35 | * @return the irelia response 36 | */ 37 | IreliaResponse invoke(IreliaRequest request) 38 | throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, IreliaRuntimeException; 39 | 40 | /** 41 | * Name string. 42 | * 43 | * @return the string 44 | */ 45 | String name(); 46 | } -------------------------------------------------------------------------------- /Irelia-upstream/src/main/java/cn/fanhub/irelia/upstream/UpstreamConfig.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.upstream; 17 | 18 | /** 19 | * 20 | * @author chengfan 21 | * @version $Id: UpstreamConfig.java, v 0.1 2018年04月21日 下午4:34 chengfan Exp $ 22 | */ 23 | public class UpstreamConfig { 24 | public static final String UPSTREAM_FILE = "irelia.upstream"; 25 | } -------------------------------------------------------------------------------- /Irelia-upstream/src/main/java/cn/fanhub/irelia/upstream/UpstreamManager.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2018 chengfan(fanhub.cn) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package cn.fanhub.irelia.upstream; 17 | 18 | import lombok.extern.slf4j.Slf4j; 19 | import org.apache.commons.lang3.StringUtils; 20 | 21 | import java.io.BufferedReader; 22 | import java.io.File; 23 | import java.io.FileReader; 24 | import java.io.IOException; 25 | import java.net.URL; 26 | import java.util.Enumeration; 27 | import java.util.Map; 28 | import java.util.concurrent.ConcurrentHashMap; 29 | 30 | /** 31 | * 32 | * @author chengfan 33 | * @version $Id: UpstreamManager.java, v 0.1 2018年04月21日 下午4:23 chengfan Exp $ 34 | */ 35 | @Slf4j 36 | public class UpstreamManager { 37 | 38 | private static final Map UPSTREAM_MAP = new ConcurrentHashMap(); 39 | 40 | static { 41 | try { 42 | if (log.isInfoEnabled()) { 43 | log.info("start load upstreams"); 44 | } 45 | loadInitialUpstreams(); 46 | 47 | } catch (IOException e) { 48 | log.error("load upstream error : io exception", e); 49 | } catch (ClassNotFoundException e) { 50 | log.error("load upstream error : class not found", e); 51 | } 52 | } 53 | 54 | private static void loadInitialUpstreams() throws IOException, ClassNotFoundException { 55 | Enumeration resources = UpstreamManager.class.getClassLoader().getResources(UpstreamConfig.UPSTREAM_FILE); 56 | 57 | while (resources.hasMoreElements()) { 58 | File file = new File(resources.nextElement().getFile()); 59 | FileReader fileReader = new FileReader(file); 60 | BufferedReader bf = new BufferedReader(fileReader); 61 | String upstreamStr = bf.readLine(); 62 | String[] upstreams = StringUtils.split(upstreamStr, ","); 63 | 64 | for (String upstream : upstreams) { 65 | Class.forName(upstream, true, Thread.currentThread().getContextClassLoader()); 66 | if (log.isInfoEnabled()) { 67 | log.info("load upstreams {}", upstream); 68 | } 69 | } 70 | 71 | bf.close(); 72 | fileReader.close(); 73 | } 74 | 75 | } 76 | 77 | public static void register(IreliaUpstream upstream) { 78 | if (log.isInfoEnabled()) { 79 | log.info("register upstream "); 80 | } 81 | UPSTREAM_MAP.put(upstream.name(), upstream); 82 | } 83 | 84 | public static Map getUpstreamMap() { 85 | return UPSTREAM_MAP; 86 | } 87 | 88 | public static IreliaUpstream getUpstream(String name) { 89 | return UPSTREAM_MAP.get(name); 90 | } 91 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2018 chengfan 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Irelia 2 | 3 | 基于 netty 实现的 api 网关 4 | 5 | ## 1. 各模块介绍 6 | 7 | - irelia-core: 核心模块,存放一些抽象类和接口 8 | 9 | - irelia-server: server 模块,接收请求,组装 pipeline,执行 handler 的模块。 10 | 11 | - irelia-spi-core: spi 核心模块,提供 spi 的基本方法,如提取 IreliaBean、methodInfo 等。 12 | 13 | - irelia-spi-dubbo: 为 dubbo 项目提供的快速接入包。 14 | 15 | - Irelia-upstream: upstream 核心模块,提供了 upstream 的核心接口,以及管理类。 16 | 17 | - Irelia-upstream-dubbo: dubbo 项目的 upstream 实现。 18 | 19 | ## 2. 使用方式 20 | 21 | ### 2.1 server 的配置 22 | 23 | 1. 继承 `AbstractConfigHandler` 抽象类,实现其中的抽象接口。该类的作用是获取每一个请求中 rpc 的配置,配置可以选择保存在数据库中。 24 | 25 | ```java 26 | @Component 27 | public class PlacidiumConfigHandler extends AbstractConfigHandler { 28 | 29 | @Autowired 30 | private RpcInfoService rpcInfoService; 31 | 32 | @Autowired 33 | private SystemInfoService systemInfoService; 34 | 35 | @Override 36 | public RpcConfig getRpcConfig(String rpcValue) { 37 | return rpcInfoService.getByRpcValue(rpcValue).getRpcConfig(); 38 | } 39 | 40 | @Override 41 | public UpstreamConfig getUpstreamConfig(String sysName) { 42 | SystemInfo systemInfo = systemInfoService.getByName(sysName); 43 | DubboUpstreamConfig upstreamConfig = new DubboUpstreamConfig(); 44 | upstreamConfig.setUsername("cf"); 45 | upstreamConfig.setPassword("123"); 46 | upstreamConfig.setAppName(sysName); 47 | upstreamConfig.setName(sysName); 48 | upstreamConfig.setAddress(systemInfo.getRegisterUrl()); 49 | return upstreamConfig; 50 | } 51 | 52 | } 53 | 54 | ``` 55 | 56 | 2. 配置 `Bootstrap` 以及需要的 `Handler`。Bootstrap 需要设置启动的端口。Handler 的具体信息在下一节。 57 | 58 | 示例配置: 59 | 60 | ```java 61 | @Configuration 62 | public class IreliaConfig { 63 | // 使用 http 服务 64 | @Bean 65 | public HttpInboundHandler httpInboundHandler() { 66 | return new HttpInboundHandler(); 67 | } 68 | 69 | // 添加默认路由组件 70 | @Bean 71 | public RouteHandler routeHandler() { 72 | return new RouteHandler(); 73 | } 74 | 75 | // 添加默认身份验证组件 76 | @Bean 77 | public SecurityHandler securityHandler() { 78 | return new SecurityHandler(); 79 | } 80 | 81 | // 启动网关 82 | @Bean(initMethod = "start") 83 | public Bootstrap bootstrap() { 84 | Bootstrap bootstrap = new Bootstrap(); 85 | bootstrap.setPort(9876); 86 | return bootstrap; 87 | } 88 | } 89 | ``` 90 | ### 2.2 handler 的配置 91 | 92 | > 网关端配置 93 | 94 | 95 | 96 | 引入 pom 97 | 98 | ```java 99 | 100 | cn.fanhub 101 | irelia-server 102 | 0.0.1 103 | 104 | ``` 105 | 106 | 107 | 将 handler 配置为 spring bean 即可。 108 | 109 | 必须配置的 handler : 110 | 111 | - HttpInboundHandler: 接收 http 请求的组件(如果想使用 tcp,则需要自己编写 Handler)。 112 | 113 | - AbstractConfigHandler: 构造网关运行所需要的必要数据,需实现该抽象类。 114 | 115 | - RouteHandler: 路由组件 116 | 117 | 118 | 可选的 handler 119 | 120 | - SecurityHandler: 安全认证的组件 121 | 122 | - ErrorHandler: 统一的异常处理组件 123 | 124 | 125 | ### 2.3 upstream 的配置 126 | 127 | > 网关端配置 128 | 129 | 引入 pom 130 | 131 | ```java 132 | 133 | cn.fanhub 134 | Irelia-upstream-dubbo 135 | 0.0.1 136 | 137 | 138 | cn.fanhub 139 | Irelia-spi-dubbo 140 | 0.0.1 141 | 142 | ``` 143 | 144 | 在 `classpath` 下,新建文件: `irelia.upstream` 145 | 146 | 填写支持的 `upstream` 147 | 148 | ```java 149 | cn.fanhub.irelia.upstream.dubbo.DubboUpstream 150 | ``` 151 | 152 | > Irelia 只提供了 dubbo upstream,其他 upstream 需要自行实现,多个 upstream 用 `,` 分隔。 153 | 154 | 这样,网关就配置好了。 155 | 156 | ### 2.4 spi 的使用 157 | 158 | > 业务方配置 159 | 160 | 引入 pom: 161 | 162 | ```java 163 | 164 | com.alibaba 165 | dubbo 166 | ${dubbo.version} 167 | 168 | 169 | junit 170 | junit 171 | 4.12 172 | 173 | 174 | cn.fanhub 175 | Irelia-spi-dubbo 176 | 0.0.1 177 | 178 | 179 | cn.fanhub 180 | Irelia-upstream-dubbo 181 | 0.0.1 182 | 183 | ``` 184 | 185 | 配置 DubboStarter 186 | 187 | ```java 188 | @Component 189 | public class Config { 190 | @Bean 191 | public DubboStarter dubboStarter() { 192 | DubboStarter dubboStarter = new DubboStarter(); 193 | dubboStarter.setAppName("test"); 194 | dubboStarter.setProtocolPort(20880); 195 | dubboStarter.setRegistryUrl("multicast://224.5.6.7:1234"); 196 | dubboStarter.setServiceThreads(10); 197 | return dubboStarter; 198 | } 199 | } 200 | ``` 201 | 202 | 编写接口以及实现, 在方法上添加 `@Rpc` 注解,填写相应信息,将接口的实现发布为 spring bean 203 | 204 | ```java 205 | 206 | public interface DemoService { 207 | 208 | @Rpc(value = "cn.fanhub.dubbo.sayhello", name = "testName", desc = "testDes") 209 | String sayHello(String name); 210 | } 211 | 212 | @Component 213 | public class DemoServiceImpl implements DemoService { 214 | public String sayHello(String name) { 215 | return "Hello " + name; 216 | } 217 | } 218 | ``` 219 | 220 | 完整的网关 demo 参考: https://github.com/distributed-and-microservice/Placidium 221 | 222 | ## 3. 扩展方式 223 | 224 | ### 3.1 handler 的扩展 225 | 226 | irelia handler 使用常见的 PRPE 模式。即 pre、post、route 和 error。 227 | 228 | 每个 handler 有一个 order 函数,返回值为 int,值小的先执行, 不能为负数。 229 | 230 | 目前: 231 | 232 | - HttpInboundHandler: pre | order 为 0 233 | 234 | - AbstractConfigHandler: pre | order 为 10 235 | 236 | - SecurityHandler: pre | order 为 20 237 | 238 | - RouteHandler: route | order 为 1000 239 | 240 | - ErrorHandler: error | order 为 2000 241 | 242 | 243 | 244 | 245 | ### 3.2 upstream 的扩展 246 | 247 | // 待更新,可以参考 dubbo-upstream 248 | 249 | ### 3.3 spi 的扩展 250 | 251 | // 待更新,可以参考 dubbo-spi -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | cn.fanhub 8 | Irelia-parent 9 | pom 10 | 0.0.1 11 | 12 | 0.0.1 13 | 2.5.4 14 | 4.0.6.RELEASE 15 | 2.3 16 | 17 | 18 | 19 | Irelia-server 20 | Irelia-common-util 21 | Irelia-upstream 22 | Irelia-test 23 | Irelia-spi-core 24 | Irelia-spi-dubbo 25 | Irelia-upstream-dubbo 26 | Irelia-core 27 | 28 | 29 | 30 | 31 | 32 | 33 | io.netty 34 | netty-all 35 | 4.1.16.Final 36 | 37 | 38 | org.projectlombok 39 | lombok 40 | 1.16.18 41 | 42 | 43 | junit 44 | junit 45 | 4.12 46 | 47 | 48 | ch.qos.logback 49 | logback-classic 50 | 1.2.3 51 | 52 | 53 | com.github.sgroschupf 54 | zkclient 55 | 0.1 56 | 57 | 58 | com.alibaba 59 | dubbo 60 | ${dubbo.version} 61 | 62 | 63 | org.springframework 64 | spring-web 65 | 66 | 67 | org.springframework 68 | spring-beans 69 | 70 | 71 | 72 | 73 | com.google.guava 74 | guava 75 | 23.0 76 | 77 | 78 | org.apache.commons 79 | commons-lang3 80 | 3.6 81 | 82 | 83 | commons-lang 84 | commons-lang 85 | 2.6 86 | 87 | 88 | commons-beanutils 89 | commons-beanutils 90 | 1.9.3 91 | 92 | 93 | 94 | 95 | org.springframework 96 | spring-core 97 | ${springframework.version} 98 | 99 | 100 | org.springframework 101 | spring-context 102 | ${springframework.version} 103 | 104 | 105 | org.springframework 106 | spring-beans 107 | ${springframework.version} 108 | 109 | 110 | org.springframework 111 | spring-aop 112 | ${springframework.version} 113 | 114 | 115 | org.springframework 116 | spring-aspects 117 | 4.3.12.RELEASE 118 | 119 | 120 | org.aspectj 121 | aspectjweaver 122 | 1.8.13 123 | 124 | 125 | aopalliance 126 | aopalliance 127 | 1.0 128 | 129 | 130 | 131 | 132 | joda-time 133 | joda-time 134 | ${joda-time.version} 135 | 136 | 137 | 138 | com.alibaba 139 | fastjson 140 | 1.2.48 141 | 142 | 143 | 144 | --------------------------------------------------------------------------------