提交 9f7aeea6 编写于 作者: K kezhenxu94 提交者: min

Cleanup (#304)

* replace local constants with global constants

* remove unnecessary declarations

* create adapter for util method

* remove unused imports

* remove unnecessary code. issue #307
上级 45c0d418
......@@ -19,14 +19,11 @@ package org.apache.dubbo.admin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class DubboAdminApplication {
public static void main(String[] args) {
ApplicationContext act = SpringApplication.run(DubboAdminApplication.class, args);
SpringApplication.run(DubboAdminApplication.class, args);
}
}
......@@ -16,18 +16,13 @@
*/
package org.apache.dubbo.admin.common.util;
import org.apache.dubbo.admin.model.domain.LoadBalance;
import org.apache.dubbo.admin.model.domain.Override;
import org.apache.dubbo.admin.model.domain.Weight;
import org.apache.dubbo.admin.model.dto.BalancingDTO;
import org.apache.dubbo.admin.model.dto.DynamicConfigDTO;
import org.apache.dubbo.admin.model.dto.WeightDTO;
import org.apache.dubbo.admin.model.store.OverrideConfig;
import org.apache.dubbo.admin.model.store.OverrideDTO;
import org.apache.dubbo.common.utils.StringUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -37,32 +32,6 @@ import java.util.Map;
*
*/
public class OverrideUtils {
public static List<Weight> overridesToWeights(List<Override> overrides) {
List<Weight> weights = new ArrayList<Weight>();
if (overrides == null) {
return weights;
}
for (Override o : overrides) {
if (StringUtils.isEmpty(o.getParams())) {
continue;
} else {
Map<String, String> params = StringUtils.parseQueryString(o.getParams());
for (Map.Entry<String, String> entry : params.entrySet()) {
if (entry.getKey().equals("weight")) {
Weight weight = new Weight();
weight.setAddress(o.getAddress());
weight.setId(o.getId());
weight.setHash(o.getHash());
weight.setService(o.getService());
weight.setWeight(Integer.valueOf(entry.getValue()));
weights.add(weight);
}
}
}
}
return weights;
}
public static OverrideConfig weightDTOtoConfig(WeightDTO weightDTO) {
OverrideConfig overrideConfig = new OverrideConfig();
overrideConfig.setType(Constants.WEIGHT);
......@@ -96,34 +65,6 @@ public class OverrideUtils {
dynamicConfigDTO.setEnabled(overrideDTO.isEnabled());
return dynamicConfigDTO;
}
public static OverrideDTO createFromDynamicConfig(DynamicConfigDTO dynamicConfigDTO) {
OverrideDTO overrideDTO = new OverrideDTO();
if (StringUtils.isNotEmpty(dynamicConfigDTO.getApplication())) {
overrideDTO.setScope(Constants.APPLICATION);
overrideDTO.setKey(dynamicConfigDTO.getApplication());
} else {
overrideDTO.setScope(Constants.SERVICE);
overrideDTO.setKey(dynamicConfigDTO.getService());
}
overrideDTO.setConfigVersion(dynamicConfigDTO.getConfigVersion());
overrideDTO.setConfigs(dynamicConfigDTO.getConfigs());
return overrideDTO;
}
public static OverrideConfig balanceDTOtoConfig(BalancingDTO balancingDTO) {
OverrideConfig overrideConfig = new OverrideConfig();
overrideConfig.setType(Constants.BALANCING);
overrideConfig.setEnabled(true);
overrideConfig.setSide(Constants.CONSUMER_SIDE);
Map<String, Object> parameters = new HashMap<>();
if (balancingDTO.getMethodName().equals("*")) {
parameters.put("loadbalance", balancingDTO.getStrategy());
} else {
parameters.put(balancingDTO.getMethodName() + ".loadbalance", balancingDTO.getStrategy());
}
overrideConfig.setParameters(parameters);
return overrideConfig;
}
public static WeightDTO configtoWeightDTO(OverrideConfig config, String scope, String key) {
WeightDTO weightDTO = new WeightDTO();
......@@ -157,82 +98,4 @@ public class OverrideUtils {
}
return balancingDTO;
}
public static Weight overrideToWeight(Override override) {
List<Weight> weights = OverrideUtils.overridesToWeights(Arrays.asList(override));
if (weights != null && weights.size() > 0) {
return overridesToWeights(Arrays.asList(override)).get(0);
}
return null;
}
public static Override weightToOverride(Weight weight) {
Override override = new Override();
override.setId(weight.getId());
override.setHash(weight.getHash());
override.setAddress(weight.getAddress());
override.setEnabled(true);
override.setParams("weight=" + weight.getWeight());
override.setService(weight.getService());
return override;
}
public static List<LoadBalance> overridesToLoadBalances(List<Override> overrides) {
List<LoadBalance> loadBalances = new ArrayList<>();
if (overrides == null) {
return loadBalances;
}
for (Override o : overrides) {
if (StringUtils.isEmpty(o.getParams())) {
continue;
} else {
Map<String, String> params = StringUtils.parseQueryString(o.getParams());
for (Map.Entry<String, String> entry : params.entrySet()) {
if (entry.getKey().endsWith("loadbalance")) {
LoadBalance loadBalance = new LoadBalance();
String method = null;
if (entry.getKey().endsWith(".loadbalance")) {
method = entry.getKey().split(".loadbalance")[0];
} else {
method = "*";
}
loadBalance.setMethod(method);
loadBalance.setId(o.getId());
loadBalance.setHash(o.getHash());
loadBalance.setService(o.getService());
loadBalance.setStrategy(entry.getValue());
loadBalances.add(loadBalance);
}
}
}
}
return loadBalances;
}
public static LoadBalance overrideToLoadBalance(Override override) {
List<LoadBalance> loadBalances = OverrideUtils.overridesToLoadBalances(Arrays.asList(override));
if (loadBalances != null && loadBalances.size() > 0) {
return loadBalances.get(0);
}
return null;
}
public static Override loadBalanceToOverride(LoadBalance loadBalance) {
Override override = new Override();
override.setId(loadBalance.getId());
override.setHash(loadBalance.getHash());
override.setService(loadBalance.getService());
override.setEnabled(true);
String method = loadBalance.getMethod();
String strategy = loadBalance.getStrategy();
if (StringUtils.isEmpty(method) || method.equals("*")) {
override.setParams("loadbalance=" + strategy);
} else {
override.setParams(method + ".loadbalance=" + strategy);
}
return override;
}
}
......@@ -60,8 +60,6 @@ public class ConfigCenter {
@Value("${admin.config-center.password:}")
private String password;
private static String globalConfigPath = "config/dubbo/dubbo.properties";
private static final Logger logger = LoggerFactory.getLogger(ConfigCenter.class);
private URL configCenterUrl;
......@@ -82,7 +80,7 @@ public class ConfigCenter {
dynamicConfiguration = ExtensionLoader.getExtensionLoader(GovernanceConfiguration.class).getExtension(configCenterUrl.getProtocol());
dynamicConfiguration.setUrl(configCenterUrl);
dynamicConfiguration.init();
String config = dynamicConfiguration.getConfig(globalConfigPath);
String config = dynamicConfiguration.getConfig(Constants.GLOBAL_CONFIG_PATH);
if (StringUtils.isNotEmpty(config)) {
Arrays.stream(config.split("\n")).forEach( s -> {
......
......@@ -37,7 +37,6 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
......@@ -59,7 +58,7 @@ public class AccessesController {
@RequestMapping(method = RequestMethod.GET)
public List<AccessDTO> searchAccess(@RequestParam(required = false) String service,
@RequestParam(required = false) String application,
@PathVariable String env) throws ParseException {
@PathVariable String env) {
if (StringUtils.isBlank(service) && StringUtils.isBlank(application)) {
throw new ParamValidationException("Either service or application is required");
}
......@@ -78,7 +77,7 @@ public class AccessesController {
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public AccessDTO detailAccess(@PathVariable String id, @PathVariable String env) throws ParseException {
public AccessDTO detailAccess(@PathVariable String id, @PathVariable String env) {
id = id.replace(Constants.ANY_VALUE, Constants.PATH_SEPARATOR);
AccessDTO accessDTO = routeService.findAccess(id);
return accessDTO;
......
......@@ -110,12 +110,6 @@ public class LoadBalanceController {
throw new ResourceNotFoundException("Unknown ID!");
}
return balancingDTO;
// LoadBalance loadBalance = OverrideUtils.overrideToLoadBalance(override);
// BalancingDTO balancingDTO = new BalancingDTO();
// balancingDTO.setService(loadBalance.getService());
// balancingDTO.setMethodName(loadBalance.getMethod());
// balancingDTO.setStrategy(loadBalance.getStrategy());
}
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
......
/*
* 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 org.apache.dubbo.admin.model.adapter;
import java.util.HashMap;
import java.util.Map;
import org.apache.dubbo.admin.common.util.Constants;
import org.apache.dubbo.admin.model.dto.BalancingDTO;
import org.apache.dubbo.admin.model.store.OverrideConfig;
public class BalancingDTO2OverrideConfigAdapter extends OverrideConfig {
public BalancingDTO2OverrideConfigAdapter(BalancingDTO balancingDTO) {
setType(Constants.BALANCING);
setEnabled(true);
setSide(Constants.CONSUMER_SIDE);
Map<String, Object> parameters = new HashMap<>();
if (balancingDTO.getMethodName().equals("*")) {
parameters.put("loadbalance", balancingDTO.getStrategy());
} else {
parameters.put(balancingDTO.getMethodName() + ".loadbalance", balancingDTO.getStrategy());
}
setParameters(parameters);
}
}
/*
* 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 org.apache.dubbo.admin.model.adapter;
import org.apache.dubbo.admin.common.util.Constants;
import org.apache.dubbo.admin.model.dto.DynamicConfigDTO;
import org.apache.dubbo.admin.model.store.OverrideDTO;
import org.apache.dubbo.common.utils.StringUtils;
public class DynamicConfigDTO2OverrideDTOAdapter extends OverrideDTO {
public DynamicConfigDTO2OverrideDTOAdapter(DynamicConfigDTO dynamicConfigDTO) {
if (StringUtils.isNotEmpty(dynamicConfigDTO.getApplication())) {
setScope(Constants.APPLICATION);
setKey(dynamicConfigDTO.getApplication());
} else {
setScope(Constants.SERVICE);
setKey(dynamicConfigDTO.getService());
}
setConfigVersion(dynamicConfigDTO.getConfigVersion());
setConfigs(dynamicConfigDTO.getConfigs());
}
}
/*
* 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 org.apache.dubbo.admin.model.adapter;
import org.apache.dubbo.admin.model.domain.LoadBalance;
import org.apache.dubbo.admin.model.domain.Override;
import org.apache.dubbo.common.utils.StringUtils;
public class LoadBalance2OverrideAdapter extends Override {
public LoadBalance2OverrideAdapter(final LoadBalance loadBalance) {
setId(loadBalance.getId());
setHash(loadBalance.getHash());
setService(loadBalance.getService());
setEnabled(true);
String method = loadBalance.getMethod();
String strategy = loadBalance.getStrategy();
if (StringUtils.isEmpty(method) || method.equals("*")) {
setParams("loadbalance=" + strategy);
} else {
setParams(method + ".loadbalance=" + strategy);
}
}
}
/*
* 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 org.apache.dubbo.admin.model.adapter;
import org.apache.dubbo.admin.model.domain.Override;
import org.apache.dubbo.admin.model.domain.Weight;
public class WeightToOverrideAdapter extends Override {
public WeightToOverrideAdapter(Weight weight) {
setId(weight.getId());
setHash(weight.getHash());
setAddress(weight.getAddress());
setEnabled(true);
setParams("weight=" + weight.getWeight());
setService(weight.getService());
}
}
......@@ -56,10 +56,4 @@ public class GenericServiceImpl {
GenericService genericService = reference.get();
return genericService.$invoke(method, parameterTypes, params);
}
public static void main(String[] args) {
GenericServiceImpl genericService = new GenericServiceImpl();
genericService.init();
genericService.invoke("org.apache.dubbo.demo.api.DemoService", "sayHello", new String[]{"java.lang.String"}, new Object[]{"hello"});
}
}
......@@ -21,6 +21,10 @@ import org.apache.dubbo.admin.common.util.Constants;
import org.apache.dubbo.admin.common.util.ConvertUtil;
import org.apache.dubbo.admin.common.util.OverrideUtils;
import org.apache.dubbo.admin.common.util.YamlParser;
import org.apache.dubbo.admin.model.adapter.BalancingDTO2OverrideConfigAdapter;
import org.apache.dubbo.admin.model.adapter.DynamicConfigDTO2OverrideDTOAdapter;
import org.apache.dubbo.admin.model.adapter.LoadBalance2OverrideAdapter;
import org.apache.dubbo.admin.model.adapter.WeightToOverrideAdapter;
import org.apache.dubbo.admin.model.domain.LoadBalance;
import org.apache.dubbo.admin.model.domain.Override;
import org.apache.dubbo.admin.model.domain.Weight;
......@@ -51,7 +55,7 @@ public class OverrideServiceImpl extends AbstractService implements OverrideServ
String path = getPath(id);
String exitConfig = dynamicConfiguration.getConfig(path);
List<OverrideConfig> configs = new ArrayList<>();
OverrideDTO existOverride = OverrideUtils.createFromDynamicConfig(override);
OverrideDTO existOverride = new DynamicConfigDTO2OverrideDTOAdapter(override);
if (exitConfig != null) {
existOverride = YamlParser.loadObject(exitConfig, OverrideDTO.class);
if (existOverride.getConfigs() != null) {
......@@ -335,7 +339,7 @@ public class OverrideServiceImpl extends AbstractService implements OverrideServ
String scope = ConvertUtil.getScopeFromDTO(balancingDTO);
String path = getPath(id);
String config = dynamicConfiguration.getConfig(path);
OverrideConfig overrideConfig = OverrideUtils.balanceDTOtoConfig(balancingDTO);
OverrideConfig overrideConfig = new BalancingDTO2OverrideConfigAdapter(balancingDTO);
OverrideDTO overrideDTO = insertConfig(config, overrideConfig, id, scope, Constants.BALANCING);
dynamicConfiguration.setConfig(path, YamlParser.dumpObject(overrideDTO));
......@@ -363,7 +367,7 @@ public class OverrideServiceImpl extends AbstractService implements OverrideServ
oldBalancing = OverrideUtils.configtoBalancingDTO(overrideConfig, Constants.SERVICE, overrideDTO.getKey());
}
int index = configs.indexOf(overrideConfig);
OverrideConfig newConfig = OverrideUtils.balanceDTOtoConfig(balancingDTO);
OverrideConfig newConfig = new BalancingDTO2OverrideConfigAdapter(balancingDTO);
configs.set(index, newConfig);
break;
}
......@@ -521,12 +525,12 @@ public class OverrideServiceImpl extends AbstractService implements OverrideServ
private void unregisterWeight(WeightDTO weightDTO) {
List<String> addresses = weightDTO.getAddresses();
if (addresses != null) {
Weight weight = new Weight();
weight.setService(weightDTO.getService());
weight.setWeight(weightDTO.getWeight());
for (String address : addresses) {
Weight weight = new Weight();
weight.setService(weightDTO.getService());
weight.setAddress(address);
weight.setWeight(weightDTO.getWeight());
Override override = OverrideUtils.weightToOverride(weight);
Override override = new WeightToOverrideAdapter(weight);
registry.unregister(override.toUrl());
}
}
......@@ -535,15 +539,14 @@ public class OverrideServiceImpl extends AbstractService implements OverrideServ
private void registerWeight(WeightDTO weightDTO) {
List<String> addresses = weightDTO.getAddresses();
if (addresses != null) {
Weight weight = new Weight();
weight.setService(weightDTO.getService());
weight.setWeight(weightDTO.getWeight());
for (String address : addresses) {
Weight weight = new Weight();
weight.setService(weightDTO.getService());
weight.setAddress(address);
weight.setWeight(weightDTO.getWeight());
Override override = OverrideUtils.weightToOverride(weight);
Override override = new WeightToOverrideAdapter(weight);
registry.register(override.toUrl());
}
}
}
......@@ -552,7 +555,7 @@ public class OverrideServiceImpl extends AbstractService implements OverrideServ
loadBalance.setService(balancingDTO.getService());
loadBalance.setMethod(balancingDTO.getMethodName());
loadBalance.setStrategy(balancingDTO.getStrategy());
registry.unregister(OverrideUtils.loadBalanceToOverride(loadBalance).toUrl());
registry.unregister(new LoadBalance2OverrideAdapter(loadBalance).toUrl());
}
private void registerBalancing(BalancingDTO balancingDTO) {
......@@ -560,7 +563,7 @@ public class OverrideServiceImpl extends AbstractService implements OverrideServ
loadBalance.setService(balancingDTO.getService());
loadBalance.setMethod(balancingDTO.getMethodName());
loadBalance.setStrategy(balancingDTO.getStrategy());
registry.register(OverrideUtils.loadBalanceToOverride(loadBalance).toUrl());
registry.register(new LoadBalance2OverrideAdapter(loadBalance).toUrl());
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册