Skip to content
This repository has been archived by the owner on May 30, 2022. It is now read-only.

Commit

Permalink
Cherry pick 601. (#162)
Browse files Browse the repository at this point in the history
* Consul registry support (#126)

* consul registry support

* fix cr

* Support nacos converter (#130)

* Upgrade version

* Support nacos address converter

* Hystrix switcher (#137)

* support enableHystrix

* support enableHystrix

* hystrix enable

* Add nacos client dependency support. (#148)

* Zk auth support. (#156)

* Server config for rest cors. (#155)

* server config for rest cors

* add test case for cors
  • Loading branch information
leizhiyuan authored Mar 8, 2019
1 parent 64635b8 commit 682e0ca
Show file tree
Hide file tree
Showing 19 changed files with 515 additions and 128 deletions.
13 changes: 13 additions & 0 deletions sofa-boot-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<cxf.version>3.0.14</cxf.version>
<skipTests>false</skipTests>
<rpc.all.version>5.5.1</rpc.all.version>
<nacos.version>0.6.0</nacos.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -175,6 +176,18 @@
<scope>provided</scope>
</dependency>

<!--nacos-->
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-api</artifactId>
<version>${nacos.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>${nacos.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> 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<String, String> map = new HashMap<String, String>();
if (paramString.contains("&")) {
String[] paramSplit = paramString.split("&");
for (String param : paramSplit) {
Map<String, String> tempMap = parseKeyValue(param);
map.putAll(tempMap);
}
} else {
Map<String, String> tempMap = parseKeyValue(paramString);
map.putAll(tempMap);
}

return map;
}

/**
* Parse key value map.
*
* @param kv the kv
* @return the map
*/
public static Map<String, String> parseKeyValue(String kv) {
Map<String, String> map = new HashMap<String, String>();
if (StringUtils.isNotEmpty(kv)) {
String[] kvSplit = kv.split("=");
String key = kvSplit[0];
String value = kvSplit[1];
map.put(key, value);
}
return map;
}
}
Original file line number Diff line number Diff line change
@@ -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 配置
* <p>
* 配置格式: com.alipay.sofa.rpc.registry.address=consul://xxx:8500
*
* @author <a href="mailto:[email protected]">zhiyuan.lzy</a>
*/
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<String, String> 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<String, String> map = new HashMap<String, String>();
if (paramString.contains("&")) {
String[] paramSplit = paramString.split("&");
for (String param : paramSplit) {
Map<String, String> tempMap = parseKeyValue(param);
map.putAll(tempMap);
}
} else {
Map<String, String> tempMap = parseKeyValue(paramString);
map.putAll(tempMap);
}

return map;
}

private Map<String, String> parseKeyValue(String kv) {
Map<String, String> map = new HashMap<String, String>();
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<String, String> map = parseParam(address);
return new RegistryConfig()
.setAddress(consulAddress)
.setProtocol(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_CONSUL)
.setParameters(map);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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 配置
* <p>
* 配置格式: com.alipay.sofa.rpc.registry.address=nacos://xxx:8848?k1=v1
* </p>
*
* @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<String, String> map = RegistryParseUtil.parseParam(address,
SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_NACOS);

return new RegistryConfig().setAddress(nacosAddress).setParameters(map)
.setProtocol(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_NACOS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 ********************************************************/

Expand All @@ -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";
Expand Down
Loading

0 comments on commit 682e0ca

Please sign in to comment.