diff --git a/sofa-boot-core/pom.xml b/sofa-boot-core/pom.xml index 309b9d0..255b425 100644 --- a/sofa-boot-core/pom.xml +++ b/sofa-boot-core/pom.xml @@ -24,6 +24,7 @@ 3.0.14 false 5.5.1 + 0.6.0 @@ -175,6 +176,18 @@ provided + + + com.alibaba.nacos + nacos-api + ${nacos.version} + + + com.alibaba.nacos + nacos-client + ${nacos.version} + + org.springframework spring-beans diff --git a/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/common/RegistryParseUtil.java b/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/common/RegistryParseUtil.java new file mode 100644 index 0000000..732c7af --- /dev/null +++ b/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/common/RegistryParseUtil.java @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.rpc.boot.common; + +import java.util.HashMap; +import java.util.Map; + +import com.alipay.sofa.rpc.common.utils.StringUtils; + +/** + * + * @author JervyShi + * @version $Id: RegistryParseUtil.java, v 0.1 2018-12-03 17:18 JervyShi Exp $$ + */ +public class RegistryParseUtil { + + /** + * Parse address string. + * + * @param config the config + * @param protocol the protocol + * @return the string + */ + public static String parseAddress(String config, String protocol) { + String address = null; + + if (StringUtils.isNotEmpty(config) && config.startsWith(protocol)) { + final String nacosProtocol = protocol + "://"; + String value = config.substring(nacosProtocol.length()); + if (!value.contains("?")) { + address = value; + } else { + int index = value.lastIndexOf('?'); + address = value.substring(0, index); + } + } + + return address; + } + + /** + * Parse param map. + * + * @param address the address + * @param protocol the protocol + * @return the map + */ + public static Map parseParam(String address, String protocol) { + + String host = parseAddress(address, protocol); + + //for config ? + String paramString = address.substring(address.indexOf(host) + host.length()); + + if (StringUtils.isNotEmpty(paramString) && paramString.startsWith("?")) { + paramString = paramString.substring(1); + } + + Map map = new HashMap(); + if (paramString.contains("&")) { + String[] paramSplit = paramString.split("&"); + for (String param : paramSplit) { + Map tempMap = parseKeyValue(param); + map.putAll(tempMap); + } + } else { + Map tempMap = parseKeyValue(paramString); + map.putAll(tempMap); + } + + return map; + } + + /** + * Parse key value map. + * + * @param kv the kv + * @return the map + */ + public static Map parseKeyValue(String kv) { + Map map = new HashMap(); + if (StringUtils.isNotEmpty(kv)) { + String[] kvSplit = kv.split("="); + String key = kvSplit[0]; + String value = kvSplit[1]; + map.put(key, value); + } + return map; + } +} diff --git a/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/ConsulConfigurator.java b/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/ConsulConfigurator.java new file mode 100644 index 0000000..25b37d7 --- /dev/null +++ b/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/ConsulConfigurator.java @@ -0,0 +1,112 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.rpc.boot.config; + +import com.alipay.sofa.rpc.common.utils.StringUtils; +import com.alipay.sofa.rpc.config.RegistryConfig; + +import java.util.HashMap; +import java.util.Map; + +/** + * Consul 配置 + *

+ * 配置格式: com.alipay.sofa.rpc.registry.address=consul://xxx:8500 + * + * @author zhiyuan.lzy + */ +public class ConsulConfigurator implements RegistryConfigureProcessor { + + public ConsulConfigurator() { + } + + /** + * 解析配置 value + * + * @param config 配置 value + */ + String parseAddress(String config) { + String address = null; + + if (StringUtils.isNotEmpty(config) && config.startsWith(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_CONSUL)) { + final String consulProtocol = SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_CONSUL + "://"; + String value = config.substring(consulProtocol.length()); + if (!value.contains("?")) { + address = value; + } else { + int index = value.lastIndexOf('?'); + address = value.substring(0, index); + } + } + + return address; + } + + /** + * 传递原始 url + * + * @param address + * @return + */ + public Map parseParam(String address) { + + String host = parseAddress(address); + + //for config ? + String paramString = address.substring(address.indexOf(host) + host.length()); + + if (StringUtils.isNotEmpty(paramString) && paramString.startsWith("?")) { + paramString = paramString.substring(1); + } + + Map map = new HashMap(); + if (paramString.contains("&")) { + String[] paramSplit = paramString.split("&"); + for (String param : paramSplit) { + Map tempMap = parseKeyValue(param); + map.putAll(tempMap); + } + } else { + Map tempMap = parseKeyValue(paramString); + map.putAll(tempMap); + } + + return map; + } + + private Map parseKeyValue(String kv) { + Map map = new HashMap(); + if (StringUtils.isNotEmpty(kv)) { + String[] kvSplit = kv.split("="); + String key = kvSplit[0]; + String value = kvSplit[1]; + map.put(key, value); + } + return map; + } + + @Override + public RegistryConfig buildFromAddress(String address) { + String consulAddress = parseAddress(address); + Map map = parseParam(address); + return new RegistryConfig() + .setAddress(consulAddress) + .setProtocol(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_CONSUL) + .setParameters(map); + + } +} \ No newline at end of file diff --git a/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/MeshConfigurator.java b/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/MeshConfigurator.java index b095eaf..060a896 100644 --- a/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/MeshConfigurator.java +++ b/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/MeshConfigurator.java @@ -16,7 +16,7 @@ */ package com.alipay.sofa.rpc.boot.config; -import com.alipay.sofa.rpc.common.utils.StringUtils; +import com.alipay.sofa.rpc.boot.common.RegistryParseUtil; import com.alipay.sofa.rpc.config.RegistryConfig; /** @@ -33,33 +33,13 @@ public class MeshConfigurator implements RegistryConfigureProcessor { public MeshConfigurator() { } - /** - * 读取配置 key ,获取其 value 进行解析。 - */ - public String parseConfig(String config) { - String address = null; - - if (StringUtils.isNotEmpty(config) && config.startsWith(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_MESH)) { - final String meshProtocol = SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_MESH + "://"; - String value = config.substring(meshProtocol.length()); - if (!value.contains("?")) { - address = value; - } else { - int index = value.lastIndexOf('?'); - address = value.substring(0, index); - } - } - - return address; - } - @Override public RegistryConfig buildFromAddress(String address) { - String meshAddress = parseConfig(address); + String meshAddress = RegistryParseUtil.parseAddress(address, + SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_MESH); meshAddress = HTTP + meshAddress; - return new RegistryConfig() - .setAddress(meshAddress) + return new RegistryConfig().setAddress(meshAddress) .setProtocol(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_MESH); } diff --git a/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/NacosConfigurator.java b/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/NacosConfigurator.java new file mode 100644 index 0000000..d901ccb --- /dev/null +++ b/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/NacosConfigurator.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.rpc.boot.config; + +import java.util.Map; + +import com.alipay.sofa.rpc.boot.common.RegistryParseUtil; +import com.alipay.sofa.rpc.config.RegistryConfig; + +/** + * Nacos 配置 + *

+ * 配置格式: com.alipay.sofa.rpc.registry.address=nacos://xxx:8848?k1=v1 + *

+ * + * @author jervyshi + * @version $Id: NacosConfigurator.java, v 0.1 2018-12-03 15:43 jervyshi Exp $$ + */ +public class NacosConfigurator implements RegistryConfigureProcessor { + + public NacosConfigurator() { + } + + @Override + public RegistryConfig buildFromAddress(String address) { + String nacosAddress = RegistryParseUtil.parseAddress(address, + SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_NACOS); + Map map = RegistryParseUtil.parseParam(address, + SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_NACOS); + + return new RegistryConfig().setAddress(nacosAddress).setParameters(map) + .setProtocol(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_NACOS); + } +} \ No newline at end of file diff --git a/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/SofaBootRpcConfigConstants.java b/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/SofaBootRpcConfigConstants.java index 5c25796..52eec38 100644 --- a/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/SofaBootRpcConfigConstants.java +++ b/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/SofaBootRpcConfigConstants.java @@ -44,14 +44,12 @@ public class SofaBootRpcConfigConstants { /* registry default configuration */ public static final String REGISTRY_FILE_PATH_DEFAULT = System.getProperty("user.home") - + - System - .getProperty("file.separator") + - "localFileRegistry" - + - System - .getProperty("file.separator") + - "localRegistry.reg"; + + System.getProperty( + "file.separator") + + "localFileRegistry" + + System.getProperty( + "file.separator") + + "localRegistry.reg"; /* possible config value start ********************************************************/ @@ -60,6 +58,11 @@ public class SofaBootRpcConfigConstants { public static final String REGISTRY_PROTOCOL_ZOOKEEPER = "zookeeper"; public static final String REGISTRY_PROTOCOL_MESH = "mesh"; + //@since 5.5.0 + public static final String REGISTRY_PROTOCOL_CONSUL = "consul"; + + public static final String REGISTRY_PROTOCOL_NACOS = "nacos"; + /* server */ public static final String RPC_PROTOCOL_BOLT = "bolt"; public static final String RPC_PROTOCOL_REST = "rest"; diff --git a/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/SofaBootRpcProperties.java b/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/SofaBootRpcProperties.java index 6e953ca..dae0dad 100644 --- a/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/SofaBootRpcProperties.java +++ b/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/SofaBootRpcProperties.java @@ -149,6 +149,11 @@ public class SofaBootRpcProperties { * rest context path (rest context path) */ private String restContextPath; + + /** + * cors settings + */ + private String restAllowedOrigins; // has no use /** * the core thread pool size of rest (rest 核心线程数) @@ -258,6 +263,8 @@ public class SofaBootRpcProperties { */ private String consumerRepeatedReferenceLimit; + private String hystrixEnable; + private String defaultTracer; public String getAftRegulationEffective() { @@ -694,6 +701,24 @@ public void setDefaultTracer(String defaultTracer) { this.defaultTracer = defaultTracer; } + public String getHystrixEnable() { + return StringUtils.isEmpty(hystrixEnable) ? getDotString(new Object() { + }.getClass().getEnclosingMethod().getName()) : hystrixEnable; + } + + public void setHystrixEnable(String hystrixEnable) { + this.hystrixEnable = hystrixEnable; + } + + public String getRestAllowedOrigins() { + return StringUtils.isEmpty(restAllowedOrigins) ? getDotString(new Object() { + }.getClass().getEnclosingMethod().getName()) : restAllowedOrigins; + } + + public void setRestAllowedOrigins(String restAllowedOrigins) { + this.restAllowedOrigins = restAllowedOrigins; + } + private String getDotString(String enclosingMethodName) { if (environment == null) { return null; diff --git a/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/ZookeeperConfigurator.java b/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/ZookeeperConfigurator.java index c43a636..7c4b83c 100644 --- a/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/ZookeeperConfigurator.java +++ b/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/config/ZookeeperConfigurator.java @@ -16,10 +16,10 @@ */ package com.alipay.sofa.rpc.boot.config; +import com.alipay.sofa.rpc.boot.common.RegistryParseUtil; import com.alipay.sofa.rpc.common.utils.StringUtils; import com.alipay.sofa.rpc.config.RegistryConfig; -import java.util.HashMap; import java.util.Map; /** @@ -34,75 +34,12 @@ public class ZookeeperConfigurator implements RegistryConfigureProcessor { public ZookeeperConfigurator() { } - /** - * 解析配置 value - * - * @param config 配置 value - */ - String parseAddress(String config) { - String address = null; - - if (StringUtils.isNotEmpty(config) && config.startsWith(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_ZOOKEEPER)) { - final String zkProtol = SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_ZOOKEEPER + "://"; - String value = config.substring(zkProtol.length()); - if (!value.contains("?")) { - address = value; - } else { - int index = value.lastIndexOf('?'); - address = value.substring(0, index); - } - } - - return address; - } - - /** - * 传递原始 url - * - * @param address - * @return - */ - public Map parseParam(String address) { - - String host = parseAddress(address); - - //for config ? - String paramString = address.substring(address.indexOf(host) + host.length()); - - if (StringUtils.isNotEmpty(paramString) && paramString.startsWith("?")) { - paramString = paramString.substring(1); - } - - Map map = new HashMap(); - if (paramString.contains("&")) { - String[] paramSplit = paramString.split("&"); - for (String param : paramSplit) { - Map tempMap = parseKeyValue(param); - map.putAll(tempMap); - } - } else { - Map tempMap = parseKeyValue(paramString); - map.putAll(tempMap); - } - - return map; - } - - private Map parseKeyValue(String kv) { - Map map = new HashMap(); - if (StringUtils.isNotEmpty(kv)) { - String[] kvSplit = kv.split("="); - String key = kvSplit[0]; - String value = kvSplit[1]; - map.put(key, value); - } - return map; - } - @Override public RegistryConfig buildFromAddress(String address) { - String zkAddress = parseAddress(address); - Map map = parseParam(address); + String zkAddress = RegistryParseUtil.parseAddress(address, + SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_ZOOKEEPER); + Map map = RegistryParseUtil.parseParam(address, + SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_ZOOKEEPER); String file = map.get("file"); @@ -110,10 +47,8 @@ public RegistryConfig buildFromAddress(String address) { file = SofaBootRpcConfigConstants.REGISTRY_FILE_PATH_DEFAULT; } - return new RegistryConfig() - .setAddress(zkAddress) - .setFile(file) - .setProtocol(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_ZOOKEEPER); - + return new RegistryConfig().setAddress(zkAddress).setFile(file) + .setProtocol(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_ZOOKEEPER) + .setParameters(map); } } \ No newline at end of file diff --git a/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/container/ServerConfigContainer.java b/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/container/ServerConfigContainer.java index 29e0a7e..a7a3cdb 100644 --- a/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/container/ServerConfigContainer.java +++ b/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/container/ServerConfigContainer.java @@ -22,11 +22,13 @@ import com.alipay.sofa.rpc.boot.config.SofaBootRpcConfigConstants; import com.alipay.sofa.rpc.boot.config.SofaBootRpcProperties; import com.alipay.sofa.rpc.boot.log.SofaBootRpcLoggerFactory; +import com.alipay.sofa.rpc.common.RpcConstants; import com.alipay.sofa.rpc.config.ServerConfig; import com.alipay.sofa.rpc.server.bolt.BoltServer; import org.slf4j.Logger; import org.springframework.util.StringUtils; +import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ThreadPoolExecutor; @@ -298,6 +300,7 @@ ServerConfig createRestServerConfig() { String telnetStr = sofaBootRpcProperties.getRestTelnet(); String daemonStr = sofaBootRpcProperties.getRestDaemon(); + String allowedOrigins = sofaBootRpcProperties.getRestAllowedOrigins(); int port; int ioThreadCount; int restThreadPoolMaxSize; @@ -345,13 +348,20 @@ ServerConfig createRestServerConfig() { daemon = Boolean.parseBoolean(daemonStr); } + Map parameters = new HashMap(); + + if (StringUtils.hasText(allowedOrigins)) { + parameters.put(RpcConstants.ALLOWED_ORIGINS, allowedOrigins); + } + ServerConfig serverConfig = new ServerConfig() .setPort(port) .setIoThreads(ioThreadCount) .setMaxThreads(restThreadPoolMaxSize) .setPayload(maxRequestSize) .setTelnet(telnet) - .setDaemon(daemon); + .setDaemon(daemon) + .setParameters(parameters); if (!StringUtils.isEmpty(contextPath)) { serverConfig.setContextPath(contextPath); diff --git a/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/runtime/adapter/helper/ConsumerConfigHelper.java b/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/runtime/adapter/helper/ConsumerConfigHelper.java index 84b43ad..3aaa0fe 100644 --- a/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/runtime/adapter/helper/ConsumerConfigHelper.java +++ b/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/runtime/adapter/helper/ConsumerConfigHelper.java @@ -29,6 +29,7 @@ import com.alipay.sofa.rpc.config.RegistryConfig; import com.alipay.sofa.rpc.core.invoke.SofaResponseCallback; import com.alipay.sofa.rpc.filter.Filter; +import com.alipay.sofa.rpc.hystrix.HystrixConstants; import com.alipay.sofa.runtime.spi.binding.Contract; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; @@ -174,8 +175,12 @@ public ConsumerConfig getConsumerConfig(Contract contract, RpcBinding binding) { consumerConfig.setSerialization(serialization); } - if (param.getParamters() != null) { - consumerConfig.setParameters(param.getParamters()); + if (param.getParameters() != null) { + consumerConfig.setParameters(param.getParameters()); + } + + if (Boolean.TRUE.toString().equals(sofaBootRpcProperties.getHystrixEnable())) { + consumerConfig.setParameter(HystrixConstants.SOFA_HYSTRIX_ENABLED, Boolean.TRUE.toString()); } return consumerConfig.setProtocol(protocol); diff --git a/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/runtime/adapter/helper/ProviderConfigHelper.java b/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/runtime/adapter/helper/ProviderConfigHelper.java index fd18fc2..9cd748e 100644 --- a/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/runtime/adapter/helper/ProviderConfigHelper.java +++ b/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/runtime/adapter/helper/ProviderConfigHelper.java @@ -131,8 +131,8 @@ public ProviderConfig getProviderConfig(Contract contract, RpcBinding binding, O providerConfig.setSerialization(serialization); } - if (param.getParamters() != null) { - providerConfig.setParameters(param.getParamters()); + if (param.getParameters() != null) { + providerConfig.setParameters(param.getParameters()); } if (param.getRegistrys() != null && param.getRegistrys().size() > 0) { diff --git a/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/runtime/param/RpcBindingParam.java b/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/runtime/param/RpcBindingParam.java index e506bbb..f555338 100644 --- a/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/runtime/param/RpcBindingParam.java +++ b/sofa-boot-core/src/main/java/com/alipay/sofa/rpc/boot/runtime/param/RpcBindingParam.java @@ -75,9 +75,9 @@ public abstract class RpcBindingParam implements BindingParam { protected String serialization; - protected Map paramters = new ConcurrentHashMap(); + protected Map parameters = new ConcurrentHashMap(); - protected List registrys = new ArrayList(); + protected List registrys = new ArrayList(); /** * Getter method for property timeout. @@ -429,12 +429,12 @@ public void setSerialization(String serialization) { this.serialization = serialization; } - public Map getParamters() { - return paramters; + public Map getParameters() { + return parameters; } - public void setParamters(Map paramters) { - this.paramters = paramters; + public void setParameters(Map parameters) { + this.parameters = parameters; } public List getRegistrys() { @@ -494,7 +494,7 @@ public boolean equals(Object o) { return false; if (serialization != null ? !serialization.equals(that.serialization) : that.serialization != null) return false; - if (paramters != null ? !paramters.equals(that.paramters) : that.paramters != null) + if (parameters != null ? !parameters.equals(that.parameters) : that.parameters != null) return false; return registrys != null ? registrys.equals(that.registrys) : that.registrys == null; } @@ -521,7 +521,7 @@ public int hashCode() { result = 31 * result + (methodInfos != null ? methodInfos.hashCode() : 0); result = 31 * result + (targetUrl != null ? targetUrl.hashCode() : 0); result = 31 * result + (serialization != null ? serialization.hashCode() : 0); - result = 31 * result + (paramters != null ? paramters.hashCode() : 0); + result = 31 * result + (parameters != null ? parameters.hashCode() : 0); result = 31 * result + (registrys != null ? registrys.hashCode() : 0); return result; } diff --git a/sofa-boot-core/src/test/java/com/alipay/sofa/rpc/boot/config/MeshConfiguratorTest.java b/sofa-boot-core/src/test/java/com/alipay/sofa/rpc/boot/config/MeshConfiguratorTest.java new file mode 100644 index 0000000..363c345 --- /dev/null +++ b/sofa-boot-core/src/test/java/com/alipay/sofa/rpc/boot/config/MeshConfiguratorTest.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.rpc.boot.config; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.alipay.sofa.rpc.config.RegistryConfig; + +/** + * + * @author zhuoyu.sjw + * @version $Id: MeshConfiguratorTest.java, v 0.1 2018-12-03 17:39 zhuoyu.sjw Exp $$ + */ +public class MeshConfiguratorTest { + + @Test + public void buildFromAddress() { + String address = "mesh://127.0.0.1:12220"; + + MeshConfigurator meshConfigurator = new MeshConfigurator(); + RegistryConfig registryConfig = meshConfigurator.buildFromAddress(address); + assertEquals("mesh", registryConfig.getProtocol()); + assertEquals("http://127.0.0.1:12220", registryConfig.getAddress()); + } +} \ No newline at end of file diff --git a/sofa-boot-core/src/test/java/com/alipay/sofa/rpc/boot/config/NacosConfiguratorTest.java b/sofa-boot-core/src/test/java/com/alipay/sofa/rpc/boot/config/NacosConfiguratorTest.java new file mode 100644 index 0000000..680fde3 --- /dev/null +++ b/sofa-boot-core/src/test/java/com/alipay/sofa/rpc/boot/config/NacosConfiguratorTest.java @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.rpc.boot.config; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; + +import com.alipay.sofa.rpc.config.RegistryConfig; + +/** + * + * @author zhuoyu.sjw + * @version $Id: NacosConfiguratorTest.java, v 0.1 2018-12-03 17:36 zhuoyu.sjw Exp $$ + */ +public class NacosConfiguratorTest { + + @Test + public void buildFromAddress() { + String address = "nacos://127.0.0.1:8848?cluster=test"; + + NacosConfigurator nacosConfigurator = new NacosConfigurator(); + RegistryConfig registryConfig = nacosConfigurator.buildFromAddress(address); + + assertNotNull(registryConfig); + assertEquals("nacos", registryConfig.getProtocol()); + assertEquals("127.0.0.1:8848", registryConfig.getAddress()); + assertNotNull(registryConfig.getParameters()); + assertEquals("test", registryConfig.getParameter("cluster")); + } +} \ No newline at end of file diff --git a/sofa-boot-core/src/test/java/com/alipay/sofa/rpc/boot/config/ZookeeperConfiguratorTest.java b/sofa-boot-core/src/test/java/com/alipay/sofa/rpc/boot/config/ZookeeperConfiguratorTest.java new file mode 100644 index 0000000..931d812 --- /dev/null +++ b/sofa-boot-core/src/test/java/com/alipay/sofa/rpc/boot/config/ZookeeperConfiguratorTest.java @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.rpc.boot.config; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; + +import com.alipay.sofa.rpc.config.RegistryConfig; + +/** + * @author LiWei + */ +public class ZookeeperConfiguratorTest { + + @Test + public void test() { + String address = "zookeeper://127.0.0.1:2181?aaa=111&rrr=666&file=/host/zk"; + + ZookeeperConfigurator zookeeperConfigurator = new ZookeeperConfigurator(); + RegistryConfig registryConfig = zookeeperConfigurator.buildFromAddress(address); + assertNotNull(registryConfig); + assertEquals("zookeeper", registryConfig.getProtocol()); + assertEquals("127.0.0.1:2181", registryConfig.getAddress()); + assertEquals("/host/zk", registryConfig.getFile()); + } +} \ No newline at end of file diff --git a/sofa-boot-starter/src/main/java/com/alipay/sofa/rpc/boot/SofaBootRpcAutoConfiguration.java b/sofa-boot-starter/src/main/java/com/alipay/sofa/rpc/boot/SofaBootRpcAutoConfiguration.java index 54b849a..eec3af1 100644 --- a/sofa-boot-starter/src/main/java/com/alipay/sofa/rpc/boot/SofaBootRpcAutoConfiguration.java +++ b/sofa-boot-starter/src/main/java/com/alipay/sofa/rpc/boot/SofaBootRpcAutoConfiguration.java @@ -17,9 +17,11 @@ package com.alipay.sofa.rpc.boot; import com.alipay.sofa.healthcheck.startup.ReadinessCheckCallback; +import com.alipay.sofa.rpc.boot.config.ConsulConfigurator; import com.alipay.sofa.rpc.boot.config.FaultToleranceConfigurator; import com.alipay.sofa.rpc.boot.config.LocalFileConfigurator; import com.alipay.sofa.rpc.boot.config.MeshConfigurator; +import com.alipay.sofa.rpc.boot.config.NacosConfigurator; import com.alipay.sofa.rpc.boot.config.RegistryConfigureProcessor; import com.alipay.sofa.rpc.boot.config.SofaBootRpcConfigConstants; import com.alipay.sofa.rpc.boot.config.SofaBootRpcProperties; @@ -92,6 +94,11 @@ public ZookeeperConfigurator zookeeperConfigurator() { return new ZookeeperConfigurator(); } + @Bean + public ConsulConfigurator consulConfigurator() { + return new ConsulConfigurator(); + } + @Bean public LocalFileConfigurator localFileConfigurator() { return new LocalFileConfigurator(); @@ -102,12 +109,19 @@ public MeshConfigurator meshConfigurator() { return new MeshConfigurator(); } + @Bean + public RegistryConfigureProcessor nacosConfigurator() { + return new NacosConfigurator(); + } + @Bean(name = "registryConfigMap") public Map configureProcessorMap() { Map map = new HashMap(); map.put(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_LOCAL, localFileConfigurator()); map.put(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_ZOOKEEPER, zookeeperConfigurator()); map.put(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_MESH, meshConfigurator()); + map.put(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_CONSUL, consulConfigurator()); + map.put(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_NACOS, nacosConfigurator()); return map; } diff --git a/sofa-boot-starter/src/test/java/com/alipay/sofa/rpc/boot/config/ZookeeperConfiguratorTest.java b/sofa-boot-starter/src/test/java/com/alipay/sofa/rpc/boot/config/ConsulConfiguratorTest.java similarity index 67% rename from sofa-boot-starter/src/test/java/com/alipay/sofa/rpc/boot/config/ZookeeperConfiguratorTest.java rename to sofa-boot-starter/src/test/java/com/alipay/sofa/rpc/boot/config/ConsulConfiguratorTest.java index 40ed157..aa90291 100644 --- a/sofa-boot-starter/src/test/java/com/alipay/sofa/rpc/boot/config/ZookeeperConfiguratorTest.java +++ b/sofa-boot-starter/src/test/java/com/alipay/sofa/rpc/boot/config/ConsulConfiguratorTest.java @@ -22,20 +22,19 @@ import java.util.Map; /** - * @author LiWei + * @author zhiyuan.lzy */ -public class ZookeeperConfiguratorTest { +public class ConsulConfiguratorTest { @Test public void test() { - ZookeeperConfigurator zookeeperConfigurator = new ZookeeperConfigurator(); - String config = "zookeeper://127.0.0.1:2181?aaa=111&rrr=666&file=/host/zk"; - String address = zookeeperConfigurator.parseAddress(config); + ConsulConfigurator consulConfigurator = new ConsulConfigurator(); + String config = "consul://127.0.0.1:8500?aaa=111&rrr=666"; + String address = consulConfigurator.parseAddress(config); - Map map = zookeeperConfigurator.parseParam(config); + Map map = consulConfigurator.parseParam(config); Assert.assertEquals("111", map.get("aaa")); Assert.assertEquals("666", map.get("rrr")); - Assert.assertEquals("127.0.0.1:2181", address); - Assert.assertEquals("/host/zk", map.get("file")); + Assert.assertEquals("127.0.0.1:8500", address); } } \ No newline at end of file diff --git a/sofa-boot-starter/src/test/java/com/alipay/sofa/rpc/boot/config/SofaBootRpcPropertiesTest.java b/sofa-boot-starter/src/test/java/com/alipay/sofa/rpc/boot/config/SofaBootRpcPropertiesTest.java index 2e2ecc4..4be5528 100644 --- a/sofa-boot-starter/src/test/java/com/alipay/sofa/rpc/boot/config/SofaBootRpcPropertiesTest.java +++ b/sofa-boot-starter/src/test/java/com/alipay/sofa/rpc/boot/config/SofaBootRpcPropertiesTest.java @@ -40,7 +40,8 @@ SofaBootRpcProperties.PREFIX + ".bolt.port=5000", "com_alipay_sofa_rpc_bolt_thread_pool_max_size=600", SofaBootRpcProperties.PREFIX + ".registries.zk1=zookeeper://xxxx", - SofaBootRpcProperties.PREFIX + ".consumer.repeated.reference.limit=10" + SofaBootRpcProperties.PREFIX + ".consumer.repeated.reference.limit=10", + SofaBootRpcProperties.PREFIX + ".rest.allowed.origins=a.com" }) public class SofaBootRpcPropertiesTest { @Autowired @@ -97,4 +98,10 @@ public void testCustoMapConfig() { Assert.assertEquals("zookeeper://xxxx", map.get("zk1")); } + @Test + public void testAllowedOriginis() { + String result = sofaBootRpcProperties.getRestAllowedOrigins(); + Assert.assertEquals("a.com", result); + } + } diff --git a/sofa-boot-starter/src/test/java/com/alipay/sofa/rpc/boot/container/ServerConfigContainerTest.java b/sofa-boot-starter/src/test/java/com/alipay/sofa/rpc/boot/container/ServerConfigContainerTest.java index b88fc3b..981ce67 100644 --- a/sofa-boot-starter/src/test/java/com/alipay/sofa/rpc/boot/container/ServerConfigContainerTest.java +++ b/sofa-boot-starter/src/test/java/com/alipay/sofa/rpc/boot/container/ServerConfigContainerTest.java @@ -19,6 +19,7 @@ import com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException; import com.alipay.sofa.rpc.boot.config.SofaBootRpcConfigConstants; import com.alipay.sofa.rpc.boot.config.SofaBootRpcProperties; +import com.alipay.sofa.rpc.common.RpcConstants; import com.alipay.sofa.rpc.config.ServerConfig; import com.alipay.sofa.rpc.test.base.ActivelyDestroyTest; import org.junit.Assert; @@ -102,7 +103,7 @@ public void testRestServerConfiguration() { sofaBootRpcProperties.setRestMaxRequestSize("1000"); sofaBootRpcProperties.setRestTelnet("true"); sofaBootRpcProperties.setRestDaemon("true"); - + sofaBootRpcProperties.setRestAllowedOrigins("a.com"); ServerConfig serverConfig = serverConfigContainer .createRestServerConfig(); @@ -114,6 +115,8 @@ public void testRestServerConfiguration() { Assert.assertEquals(1000, serverConfig.getPayload()); Assert.assertTrue(serverConfig.isTelnet()); Assert.assertTrue(serverConfig.isDaemon()); + Assert.assertEquals("a.com", serverConfig.getParameters().get(RpcConstants.ALLOWED_ORIGINS)); + } @Test