Skip to content

Commit

Permalink
Cleanup (#304)
Browse files Browse the repository at this point in the history
* replace local constants with global constants

* remove unnecessary declarations

* create adapter for util method

* remove unused imports

* remove unnecessary code. issue #307
  • Loading branch information
kezhenxu94 authored and nzomkxia committed Feb 18, 2019
1 parent 45c0d41 commit 9f7aeea
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@

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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,32 +32,6 @@
*
*/
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);
Expand Down Expand Up @@ -96,34 +65,6 @@ public static DynamicConfigDTO createFromOverride(OverrideDTO overrideDTO) {
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();
Expand Down Expand Up @@ -157,82 +98,4 @@ public static BalancingDTO configtoBalancingDTO(OverrideConfig config, String sc
}
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -82,7 +80,7 @@ GovernanceConfiguration getDynamicConfiguration() {
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 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
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;
Expand All @@ -59,7 +58,7 @@ public AccessesController(RouteService routeService, ProviderService providerSer
@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");
}
Expand All @@ -78,7 +77,7 @@ public List<AccessDTO> searchAccess(@RequestParam(required = false) String servi
}

@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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,6 @@ public BalancingDTO detailLoadBalance(@PathVariable String id, @PathVariable Str
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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,4 @@ public Object invoke(String service, String method, String[] parameterTypes, Obj
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"});
}
}
Loading

0 comments on commit 9f7aeea

Please sign in to comment.