Commit 130068a9 by 房斌

1等待时间,10ms 优化

2配置 ,url  引用公共的
3命名优化
4代码结构优化
parent 6b39172b
...@@ -48,43 +48,6 @@ public class ConfigCenter { ...@@ -48,43 +48,6 @@ public class ConfigCenter {
public String patch; public String patch;
public URL registryUrl; public URL registryUrl;
public URL getRegistryUrl() {
return registryUrl;
}
public void setRegistryUrl(URL registryUrl) {
this.registryUrl = registryUrl;
}
public URL metadataUrl;
public String getRegistryAddress() {
return registryAddress;
}
public void setRegistryAddress(String registryAddress) {
this.registryAddress = registryAddress;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
/*
* generate registry client
*/
@Bean @Bean
Registry getRegistry() { Registry getRegistry() {
Registry registry = null; Registry registry = null;
......
/*
* 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.secoo.mall.dubbo.monitor.dubbo.Constants; package com.secoo.mall.dubbo.monitor.dubbo.Constants;
...@@ -23,59 +7,14 @@ import java.util.Set; ...@@ -23,59 +7,14 @@ import java.util.Set;
public class Constants { public class Constants {
public static final String REGISTRY_ADDRESS = "dubbo.registry.address";
public static final String METADATA_ADDRESS = "dubbo.metadata-report.address";
public static final String DEFAULT_ROOT = "dubbo";
public static final String PATH_SEPARATOR = "/";
public static final String GROUP_KEY = "group"; public static final String GROUP_KEY = "group";
public static final String CONFIG_KEY = "config" + PATH_SEPARATOR + "dubbo";
public static final String DUBBO_PROPERTY = "dubbo.properties";
public static final String PROVIDER_SIDE = "provider";
public static final String CONSUMER_SIDE = "consumer";
public static final String CATEGORY_KEY = "category"; public static final String CATEGORY_KEY = "category";
public static final String ROUTERS_CATEGORY = "routers";
public static final String CONDITION_ROUTE = "condition_route";
public static final String CONDITION_RULE_SUFFIX = ".condition-router";
public static final String CONFIGURATOR = "configurators";
public static final String CONFIGURATOR_RULE_SUFFIX = ".configurators";
public static final String TAG_ROUTE = "tag_route";
public static final String TAG_RULE_SUFFIX = ".tag-router";
public static final String COMPATIBLE_CONFIG = "compatible_config";
public static final String WEIGHT = "weight"; public static final String WEIGHT = "weight";
public static final String BALANCING = "balancing"; public static final String BALANCING = "balancing";
public static final String SERVICE = "service";
public static final String APPLICATION = "application";
public static final String PUNCTUATION_POINT = ".";
public static final String PUNCTUATION_SEPARATOR_POINT = "\\.";
public static final String INTERROGATION_POINT = "?";
public static final String ANY_VALUE = "*"; public static final String ANY_VALUE = "*";
public static final String PLUS_SIGNS = "+";
public static final String IP = "ip";
public static final String INTERFACE_KEY = "interface";
public static final String DYNAMIC_KEY = "dynamic";
public static final String CONSUMER_PROTOCOL = "consumer";
public static final String PROVIDER_PROTOCOL = "provider";
public static final String ROUTE_PROTOCOL = "route";
public static final String APPLICATION_KEY = "application";
public static final String ENABLED_KEY = "enabled";
public static final String RULE_KEY = "rule";
public static final String ANYHOST_VALUE = "0.0.0.0";
public static final String OVERRIDE_PROTOCOL = "override";
public static final String CONFIGURATORS_CATEGORY = "configurators";
public static final String EMPTY_PROTOCOL = "empty"; public static final String EMPTY_PROTOCOL = "empty";
public static final String WEIGHT_KEY = "weight";
public static final int DEFAULT_WEIGHT = 100;
public static final String ADMIN_PROTOCOL = "admin";
public static final String CLASSIFIER_KEY = "classifier";
public static final String CHECK_KEY = "check";
public static final String VERSION_KEY = "version"; public static final String VERSION_KEY = "version";
public static final String PROVIDERS_CATEGORY = "providers"; public static final String PROVIDERS_CATEGORY = "providers";
public static final String CONSUMERS_CATEGORY = "consumers";
public static final String SPECIFICATION_VERSION_KEY = "release";
public static final String GLOBAL_CONFIG = "global";
public static final String GLOBAL_CONFIG_PATH = "config/dubbo/dubbo.properties";
public static final String METRICS_PORT = "metrics.port";
public static final String METRICS_PROTOCOL = "metrics.protocol";
public static final Set<String> CONFIGS = new HashSet<>(); public static final Set<String> CONFIGS = new HashSet<>();
static { static {
......
/*
* 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.secoo.mall.dubbo.monitor.dubbo.domain;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
/**
* Entity
*/
public abstract class Entity implements Serializable {
private static final long serialVersionUID = -3031128781434583143L;
private List<Long> ids;
private Long id;
private String hash;
private Date created;
private Date modified;
private Date now;
private String operator;
private String operatorAddress;
private boolean miss;
public Entity() {
}
public Entity(Long id) {
this.id = id;
}
public List<Long> getIds() {
return ids;
}
public void setIds(List<Long> ids) {
this.ids = ids;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getHash() {
return hash;
}
public void setHash(String hash) {
this.hash = hash;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public Date getModified() {
return modified;
}
public void setModified(Date modified) {
this.modified = modified;
}
public Date getNow() {
return now;
}
public void setNow(Date now) {
this.now = now;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
if (operator != null && operator.length() > 200) {
operator = operator.substring(0, 200);
}
this.operator = operator;
}
public String getOperatorAddress() {
return operatorAddress;
}
public void setOperatorAddress(String operatorAddress) {
this.operatorAddress = operatorAddress;
}
public boolean isMiss() {
return miss;
}
public void setMiss(boolean miss) {
this.miss = miss;
}
}
/*
* 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.secoo.mall.dubbo.monitor.dubbo.domain;
import com.secoo.mall.dubbo.monitor.dubbo.Constants.Constants;
import com.secoo.mall.dubbo.monitor.utils.ConvertUtil;
import org.apache.dubbo.common.URL;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* Provider
*/
public class Provider extends Entity {
private static final long serialVersionUID = 5981342400350878171L;
private String service;/* The name of the service provided by the provider */
private String url; /* Provider's address for service */
private String parameters; /* Provider provides service parameters */
private String address; /* Provider address */
private String registry;/* The provider's registry address */
private boolean dynamic; /* provider was registered dynamically */
private boolean enabled; /* provider enabled or not */
private int weight; /* provider weight */
private String application; /* application name */
private String username; /* operator */
private Date expired; /* time to expire */
private long alived; /* time to live in milliseconds */
private Override override;
private List<Override> overrides;
public Provider() {
}
public Provider(Long id) {
super(id);
}
public String getService() {
return service;
}
public void setService(String service) {
this.service = service;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getParameters() {
return parameters;
}
public void setParameters(String parameters) {
this.parameters = parameters;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getRegistry() {
return registry;
}
public void setRegistry(String registry) {
this.registry = registry;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getApplication() {
return application;
}
public void setApplication(String application) {
this.application = application;
}
public boolean isDynamic() {
return dynamic;
}
public void setDynamic(boolean dynamic) {
this.dynamic = dynamic;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public Date getExpired() {
return expired;
}
public void setExpired(Date expired) {
this.expired = expired;
}
public long getAlived() {
return alived;
}
public void setAlived(long aliveSeconds) {
this.alived = aliveSeconds;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public Override getOverride() {
return override;
}
public void setOverride(Override override) {
this.override = override;
}
public List<Override> getOverrides() {
return overrides;
}
public void setOverrides(List<Override> overrides) {
this.overrides = overrides;
}
public URL toUrl() {
Map<String, String> serviceName2Map = ConvertUtil.serviceName2Map(getService());
/*if(!serviceName2Map.containsKey(Constants.INTERFACE_KEY)) {
throw new IllegalArgumentException("No interface info");
}
if(!serviceName2Map.containsKey(Constants.VERSION_KEY)) {
throw new IllegalArgumentException("No version info");
}*/
String u = getUrl();
URL url = URL.valueOf(u + "?" + getParameters());
url = url.addParameters(serviceName2Map);
boolean dynamic = isDynamic();
if (!dynamic) {
url = url.addParameter(Constants.DYNAMIC_KEY, false);
}
boolean enabled = isEnabled();
if (enabled != url.getParameter("enabled", true)) {
if (enabled) {
url = url.removeParameter("enabled");
} else {
url = url.addParameter("enabled", false);
}
}
return url;
}
}
/*
* 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.secoo.mall.dubbo.monitor.dubbo.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
/**
* Parameter validation failure exception
*/
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public class ParamValidationException extends SystemException {
public ParamValidationException(String message) {
super(message);
}
}
/*
* 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.secoo.mall.dubbo.monitor.dubbo.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
/**
* System Exception
*/
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
public class SystemException extends RuntimeException {
public SystemException() {
super();
}
public SystemException(String message) {
super(message);
}
public SystemException(String message, Throwable cause) {
super(message, cause);
}
public SystemException(Throwable cause) {
super(cause);
}
}
/*
* 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.secoo.mall.dubbo.monitor.dubbo.model.domain;
import com.secoo.mall.dubbo.monitor.dubbo.Constants.Constants;
import com.secoo.mall.dubbo.monitor.utils.Tool;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.StringUtils;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* Consumer
*/
public class Consumer extends Entity {
private static final long serialVersionUID = -1140894843784583237L;
private String service; /* The name of the service referenced by the consumer */
private String parameters;
private String result; /*route result*/
private String address; /* address of consumer */
private String registry; /* Consumer connected registry address */
private String application; /* application name */
private String username; /* user name of consumer */
private String statistics; /* Service call statistics */
private Date collected; /* Date statistics was recorded */
private Override override;
private List<Override> overrides;
private List<Route> conditionRoutes;
private List<Provider> providers;
private Date expired;
private long alived; /*Time to live in milliseconds*/
public Consumer() {
}
public Consumer(Long id) {
super(id);
}
public String getService() {
return service;
}
public void setService(String service) {
this.service = service;
}
public String getParameters() {
return parameters;
}
public void setParameters(String parameters) {
this.parameters = parameters;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getRegistry() {
return registry;
}
public void setRegistry(String registry) {
this.registry = registry;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getStatistics() {
return statistics;
}
public void setStatistics(String statistics) {
this.statistics = statistics;
}
public Date getCollected() {
return collected;
}
public void setCollected(Date collected) {
this.collected = collected;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public String getApplication() {
return application;
}
public void setApplication(String application) {
this.application = application;
}
public Date getExpired() {
return expired;
}
public void setExpired(Date expired) {
this.expired = expired;
}
public long getAlived() {
return alived;
}
public void setAlived(long alived) {
this.alived = alived;
}
public Override getOverride() {
return override;
}
public void setOverride(Override override) {
this.override = override;
}
public List<Override> getOverrides() {
return overrides;
}
public void setOverrides(List<Override> overrides) {
this.overrides = overrides;
}
public List<Route> getConditionRoutes() {
return conditionRoutes;
}
public void setConditionRoutes(List<Route> conditionRoutes) {
this.conditionRoutes = conditionRoutes;
}
public List<Provider> getProviders() {
return providers;
}
public void setProviders(List<Provider> providers) {
this.providers = providers;
}
@Override
public String toString() {
return "Consumer [service=" + service + ", parameters=" + parameters + ", result=" + result
+ ", address=" + address + ", registry=" + registry + ", application="
+ application + ", username=" + username + ", statistics=" + statistics
+ ", collected=" + collected + ", conditionRoutes=" + conditionRoutes + ", overrides=" + overrides
+ ", expired=" + expired + ", alived=" + alived + "]";
}
public URL toUrl() {
String group = Tool.getGroup(service);
String version = Tool.getVersion(service);
String interfaze = Tool.getInterface(service);
Map<String, String> param = StringUtils.parseQueryString(parameters);
param.put(Constants.CATEGORY_KEY, Constants.CONSUMERS_CATEGORY);
if (group != null) {
param.put(Constants.GROUP_KEY, group);
}
if (version != null) {
param.put(Constants.VERSION_KEY, version);
}
return URL.valueOf(Constants.CONSUMER_PROTOCOL + "://" + address + "/" + interfaze
+ "?" + StringUtils.toQueryString(param));
}
}
/*
* 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.secoo.mall.dubbo.monitor.dubbo.model.domain;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
/**
* Entity
*/
public abstract class Entity implements Serializable {
private static final long serialVersionUID = -3031128781434583143L;
private List<Long> ids;
private Long id;
private String hash;
private Date created;
private Date modified;
private Date now;
private String operator;
private String operatorAddress;
private boolean miss;
public Entity() {
}
public Entity(Long id) {
this.id = id;
}
public List<Long> getIds() {
return ids;
}
public void setIds(List<Long> ids) {
this.ids = ids;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getHash() {
return hash;
}
public void setHash(String hash) {
this.hash = hash;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public Date getModified() {
return modified;
}
public void setModified(Date modified) {
this.modified = modified;
}
public Date getNow() {
return now;
}
public void setNow(Date now) {
this.now = now;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
if (operator != null && operator.length() > 200) {
operator = operator.substring(0, 200);
}
this.operator = operator;
}
public String getOperatorAddress() {
return operatorAddress;
}
public void setOperatorAddress(String operatorAddress) {
this.operatorAddress = operatorAddress;
}
public boolean isMiss() {
return miss;
}
public void setMiss(boolean miss) {
this.miss = miss;
}
}
/*
* 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.secoo.mall.dubbo.monitor.dubbo.model.domain;
import com.secoo.mall.dubbo.monitor.dubbo.Constants.Constants;
import com.secoo.mall.dubbo.monitor.utils.ConvertUtil;
import org.apache.dubbo.common.URL;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* Provider
*/
public class Provider extends Entity {
private static final long serialVersionUID = 5981342400350878171L;
private String service;/* The name of the service provided by the provider */
private String url; /* Provider's address for service */
private String parameters; /* Provider provides service parameters */
private String address; /* Provider address */
private String registry;/* The provider's registry address */
private boolean dynamic; /* provider was registered dynamically */
private boolean enabled; /* provider enabled or not */
private int weight; /* provider weight */
private String application; /* application name */
private String username; /* operator */
private Date expired; /* time to expire */
private long alived; /* time to live in milliseconds */
private Override override;
private List<Override> overrides;
public Provider() {
}
public Provider(Long id) {
super(id);
}
public String getService() {
return service;
}
public void setService(String service) {
this.service = service;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getParameters() {
return parameters;
}
public void setParameters(String parameters) {
this.parameters = parameters;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getRegistry() {
return registry;
}
public void setRegistry(String registry) {
this.registry = registry;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getApplication() {
return application;
}
public void setApplication(String application) {
this.application = application;
}
public boolean isDynamic() {
return dynamic;
}
public void setDynamic(boolean dynamic) {
this.dynamic = dynamic;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public Date getExpired() {
return expired;
}
public void setExpired(Date expired) {
this.expired = expired;
}
public long getAlived() {
return alived;
}
public void setAlived(long aliveSeconds) {
this.alived = aliveSeconds;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public Override getOverride() {
return override;
}
public void setOverride(Override override) {
this.override = override;
}
public List<Override> getOverrides() {
return overrides;
}
public void setOverrides(List<Override> overrides) {
this.overrides = overrides;
}
public URL toUrl() {
Map<String, String> serviceName2Map = ConvertUtil.serviceName2Map(getService());
/*if(!serviceName2Map.containsKey(Constants.INTERFACE_KEY)) {
throw new IllegalArgumentException("No interface info");
}
if(!serviceName2Map.containsKey(Constants.VERSION_KEY)) {
throw new IllegalArgumentException("No version info");
}*/
String u = getUrl();
URL url = URL.valueOf(u + "?" + getParameters());
url = url.addParameters(serviceName2Map);
boolean dynamic = isDynamic();
if (!dynamic) {
url = url.addParameter(Constants.DYNAMIC_KEY, false);
}
boolean enabled = isEnabled();
if (enabled != url.getParameter("enabled", true)) {
if (enabled) {
url = url.removeParameter("enabled");
} else {
url = url.addParameter("enabled", false);
}
}
return url;
}
}
/*
* 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.secoo.mall.dubbo.monitor.dubbo.model.domain;
import com.secoo.mall.dubbo.monitor.dubbo.Constants.Constants;
import com.secoo.mall.dubbo.monitor.utils.Tool;
import org.apache.dubbo.common.URL;
import java.util.List;
public class Route extends Entity {
public static final String ALL_METHOD = "*";
public static final String KEY_METHOD = "method";
// WHEN KEY
public static final String KEY_CONSUMER_APPLICATION = "consumer.application";
public static final String KEY_CONSUMER_GROUP = "consumer.cluster";
public static final String KEY_CONSUMER_VERSION = "consumer.version";
public static final String KEY_CONSUMER_HOST = "host";
public static final String KEY_CONSUMER_METHODS = "consumer.methods";
public static final String KEY_PROVIDER_APPLICATION = "provider.application";
// THEN KEY
public static final String KEY_PROVIDER_GROUP = "provider.cluster";
public static final String KEY_PROVIDER_PROTOCOL = "provider.protocol";
public static final String KEY_PROVIDER_VERSION = "provider.version";
public static final String KEY_PROVIDER_HOST = "provider.host";
public static final String KEY_PROVIDER_PORT = "provider.port";
private static final long serialVersionUID = -7630589008164140656L;
private long parentId; //default 0
private String name;
private String service;
private String rule;
private String matchRule;
private String filterRule;
private int priority;
private String username;
private boolean enabled;
private boolean force;
private boolean dynamic;
private boolean runtime;
private List<Route> children;
public Route() {
}
public Route(Long id) {
super(id);
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public long getParentId() {
return parentId;
}
public void setParentId(long parentId) {
this.parentId = parentId;
}
public List<Route> getChildren() {
return children;
}
public void setChildren(List<Route> subRules) {
this.children = subRules;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public boolean isDynamic() {
return dynamic;
}
public void setDynamic(boolean dynamic) {
this.dynamic = dynamic;
}
public boolean isRuntime() {
return runtime;
}
public void setRuntime(boolean runtime) {
this.runtime = runtime;
}
public boolean isForce() {
return force;
}
public void setForce(boolean force) {
this.force = force;
}
public String getService() {
return service;
}
public void setService(String service) {
this.service = service;
}
public String getRule() {
return rule;
}
public void setRule(String rule) {
this.rule = rule.trim();
String[] rules = rule.split("=>");
if (rules.length != 2) {
if (rule.endsWith("=>")) {
this.matchRule = rules[0].trim();
this.filterRule = "";
} else {
throw new IllegalArgumentException("Illegal Route Condition Rule");
}
} else {
this.matchRule = rules[0].trim();
this.filterRule = rules[1].trim();
}
}
public String getMatchRule() {
return matchRule;
}
public void setMatchRule(String matchRule) {
if (matchRule != null) {
this.matchRule = matchRule.trim();
} else {
this.matchRule = matchRule;
}
}
public String getFilterRule() {
return filterRule;
}
public void setFilterRule(String filterRule) {
if (filterRule != null) {
this.filterRule = filterRule.trim();
} else {
this.filterRule = filterRule;
}
}
@Override
public String toString() {
return "Route [parentId=" + parentId + ", name=" + name
+ ", serviceName=" + service + ", matchRule=" + matchRule
+ ", filterRule=" + filterRule + ", priority=" + priority
+ ", username=" + username + ", enabled=" + enabled + "]";
}
public URL toUrl() {
String group = Tool.getGroup(service);
String version = Tool.getVersion(service);
String interfaze = Tool.getInterface(service);
return URL.valueOf(Constants.ROUTE_PROTOCOL + "://" + Constants.ANYHOST_VALUE + "/" + interfaze
+ "?" + Constants.CATEGORY_KEY + "=" + Constants.ROUTERS_CATEGORY
+ "&router=condition&runtime=" + isRuntime() + "&enabled=" + isEnabled() + "&priority=" + getPriority() + "&force=" + isForce() + "&dynamic=" + isDynamic()
+ "&name=" + getName() + "&" + Constants.RULE_KEY + "=" + URL.encode(getMatchRule() + " => " + getFilterRule())
+ (group == null ? "" : "&" + Constants.GROUP_KEY + "=" + group)
+ (version == null ? "" : "&" + Constants.VERSION_KEY + "=" + version));
}
}
/*
* 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.secoo.mall.dubbo.monitor.dubbo.model.dto;
import org.apache.commons.lang3.StringUtils;
import java.util.Objects;
public class ServiceDTO implements Comparable<ServiceDTO> {
private String service;
private String appName;
private String group;
private String version;
private Long id;
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getService() {
return service;
}
public void setService(String service) {
this.service = service;
}
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
@Override
public int compareTo(ServiceDTO o) {
int result = StringUtils.trimToEmpty(appName).compareTo(StringUtils.trimToEmpty(o.getAppName()));
if (result == 0) {
result = StringUtils.trimToEmpty(service).compareTo(StringUtils.trimToEmpty(o.getService()));
if (result == 0) {
result = StringUtils.trimToEmpty(group).compareTo(StringUtils.trimToEmpty(o.getGroup()));
}
if (result == 0) {
result = StringUtils.trimToEmpty(version).compareTo(StringUtils.trimToEmpty(o.getVersion()));
}
}
return result;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ServiceDTO that = (ServiceDTO) o;
return Objects.equals(service, that.service) && Objects.equals(appName, that.appName) && Objects
.equals(group, that.group) && Objects.equals(version, that.version);
}
@Override
public int hashCode() {
return Objects.hash(service, appName, group, version);
}
}
/*
* 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.secoo.mall.dubbo.monitor.dubbo.service;
import com.secoo.mall.dubbo.monitor.dubbo.model.domain.Consumer;
import java.util.List;
/**
* Query service for consumer info
*/
public interface ConsumerService {
List<Consumer> findByService(String serviceName);
List<Consumer> findAll();
/**
* query for all consumer addresses
*/
List<Consumer> findByAddress(String consumerAddress);
}
...@@ -15,110 +15,20 @@ ...@@ -15,110 +15,20 @@
* limitations under the License. * limitations under the License.
*/ */
package com.secoo.mall.dubbo.monitor.dubbo.service; package com.secoo.mall.dubbo.monitor.dubbo.service;
import com.secoo.mall.dubbo.monitor.dubbo.model.domain.Provider;
import com.secoo.mall.dubbo.monitor.dubbo.model.dto.ServiceDTO;
import java.util.List; import java.util.List;
import java.util.Set;
/** /**
* ProviderService * ProviderService
*/ */
public interface ProviderService { public interface ProviderService {
void create(Provider provider);
// void enableProvider(String id);
// void disableProvider(String id);
// void doublingProvider(String id);
// void halvingProvider(String id);
void deleteStaticProvider(String id);
void updateProvider(Provider provider);
Provider findProvider(String id);
/**
* Get all provider's service name
*
* @return list of all provider's service name
*/
Set<String> findServices();
String findServiceVersion(String serviceName, String application);
String findVersionInApplication(String application);
List<String> findAddresses();
List<String> findAddressesByApplication(String application);
List<String> findAddressesByService(String serviceName);
List<String> findApplicationsByServiceName(String serviceName);
/** /**
* Get provider list with specific service name. * 根据ip地址和应用名字查询服务
* * @param address
* @param serviceName specific service name, cannot be fuzzy string * @param name
* @return list of provider object * @return
*/ */
List<Provider> findByService(String serviceName);
List<Provider> findByAppandService(String app, String serviceName);
List<Provider> findAll();
/**
* Get provider list with specific ip address.
*
* @param providerAddress provider's ip address
* @return list of provider object
*/
List<Provider> findByAddress(String providerAddress);
List<String> findServicesByAddress(String providerAddress);
Set<String> findApplications();
/**
* Get provider list with specific application name.
*
* @param application specific application name
* @return list of provider object
*/
List<Provider> findByApplication(String application);
List<String> findServicesByApplication(String application);
List<String> findMethodsByService(String serviceName);
Provider findByServiceAndAddress(String service, String address);
/**
* Get a set of service data object.
* <p>
* ServiceDTO object contains base information include
* service name , application, group and version.
*
* @param pattern {@code String} type of search
* @param filter {@code String} input filter string
* @param env {@code String}the environment of front end
* @return a set of services for fore-end page
*/
Set<ServiceDTO> getServiceDTOS(String pattern, String filter, String env);
List<Provider> getServiceDTOSByQuery(String pattern, String filter, String env);
public List<Provider> findProviderUrlByGivenApplication(String application, List<Provider> providers);
public List<String> findServicesByAddressAndName(String address,String name); public List<String> findServicesByAddressAndName(String address,String name);
} }
/*
* 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.secoo.mall.dubbo.monitor.dubbo.service; package com.secoo.mall.dubbo.monitor.dubbo.service;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.secoo.mall.common.util.date.DateUtil;
import com.secoo.mall.common.util.string.StringUtil; import com.secoo.mall.common.util.string.StringUtil;
import com.secoo.mall.dubbo.monitor.dubbo.Constants.Constants; import com.secoo.mall.dubbo.monitor.dubbo.Constants.Constants;
import com.secoo.mall.dubbo.monitor.utils.CoderUtil; import com.secoo.mall.dubbo.monitor.utils.CoderUtil;
import com.secoo.mall.dubbo.monitor.utils.Stack; import com.secoo.mall.dubbo.monitor.utils.Stack;
import com.secoo.mall.dubbo.monitor.utils.Tool; import com.secoo.mall.dubbo.monitor.utils.Tool;
import org.apache.dubbo.common.URL; import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.registry.NotifyListener; import org.apache.dubbo.registry.NotifyListener;
import org.apache.dubbo.registry.Registry; import org.apache.dubbo.registry.Registry;
...@@ -34,7 +14,6 @@ import org.apache.dubbo.registry.support.AbstractRegistryFactory; ...@@ -34,7 +14,6 @@ import org.apache.dubbo.registry.support.AbstractRegistryFactory;
import org.apache.dubbo.registry.zookeeper.ZookeeperRegistry; import org.apache.dubbo.registry.zookeeper.ZookeeperRegistry;
import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner; import org.springframework.boot.CommandLineRunner;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
...@@ -50,31 +29,10 @@ public class RegistryServerSync extends Observable implements InitializingBean, ...@@ -50,31 +29,10 @@ public class RegistryServerSync extends Observable implements InitializingBean,
private final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass()); private final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
private static final URL SUBSCRIBE = new URL(Constants.ADMIN_PROTOCOL, NetUtils.getLocalHost(), 0, "",
Constants.INTERFACE_KEY, Constants.ANY_VALUE,
Constants.GROUP_KEY, Constants.ANY_VALUE,
Constants.VERSION_KEY, Constants.ANY_VALUE,
Constants.CLASSIFIER_KEY, Constants.ANY_VALUE,
Constants.CATEGORY_KEY, Constants.PROVIDERS_CATEGORY + ","
+ Constants.CONSUMERS_CATEGORY + ","
+ Constants.ROUTERS_CATEGORY + ","
+ Constants.CONFIGURATORS_CATEGORY,
Constants.ENABLED_KEY, Constants.ANY_VALUE,
Constants.CHECK_KEY, String.valueOf(false));
private static final AtomicLong ID = new AtomicLong(); private static final AtomicLong ID = new AtomicLong();
/**
* Make sure ID never changed when the same url notified many times
*/
private final ConcurrentHashMap<String, String> URL_IDS_MAPPER = new ConcurrentHashMap<>(); private final ConcurrentHashMap<String, String> URL_IDS_MAPPER = new ConcurrentHashMap<>();
/**
* ConcurrentMap<category, ConcurrentMap<servicename, Map<MD5, URL>>>
* registryCache
*/
private final ConcurrentMap<String, ConcurrentMap<String, Map<String, URL>>> registryCache = new ConcurrentHashMap<>(); private final ConcurrentMap<String, ConcurrentMap<String, Map<String, URL>>> registryCache = new ConcurrentHashMap<>();
// @Autowired
public Registry registry; public Registry registry;
...@@ -85,6 +43,8 @@ public class RegistryServerSync extends Observable implements InitializingBean, ...@@ -85,6 +43,8 @@ public class RegistryServerSync extends Observable implements InitializingBean,
volatile boolean signal=false; volatile boolean signal=false;
List<URL> subscribedUrl;
public boolean isSignal() { public boolean isSignal() {
return signal; return signal;
} }
...@@ -95,21 +55,18 @@ public class RegistryServerSync extends Observable implements InitializingBean, ...@@ -95,21 +55,18 @@ public class RegistryServerSync extends Observable implements InitializingBean,
@Override @Override
public void afterPropertiesSet() throws Exception { public void afterPropertiesSet() throws Exception {
logger.info("Init Dubbo Admin Sync Cache...");
// registry.subscribe(SUBSCRIBE, this);
} }
@Override @Override
public void destroy() throws Exception { public void destroy() throws Exception {
registry.unsubscribe(SUBSCRIBE, this); if (subscribedUrl != null && subscribedUrl.size() > 0) {
logger.info("RegistryServerSync destory ------------>time:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date())); for (URL url : subscribedUrl) {
registry.unsubscribe(url, this);
}
}
} }
// Notification of of any service with any type (override、subcribe、route、provider) is full. //通知处理方法
@Override @Override
public void notify(List<URL> urls) { public void notify(List<URL> urls) {
...@@ -191,21 +148,10 @@ public class RegistryServerSync extends Observable implements InitializingBean, ...@@ -191,21 +148,10 @@ public class RegistryServerSync extends Observable implements InitializingBean,
services.putAll(categoryEntry.getValue()); services.putAll(categoryEntry.getValue());
} }
} }
//启动订阅
/**
* Callback used to run the bean.
*
* @param args incoming main method arguments
* @throws Exception on error
*/
@Override @Override
public void run(String... args) throws Exception { public void run(String... args) throws Exception {
logger.info("Init Dubbo Admin Sync Cache...");
Collection<Registry> registries = AbstractRegistryFactory.getRegistries(); Collection<Registry> registries = AbstractRegistryFactory.getRegistries();
// logger.info("registries already"+JSON.toJSONString(registries));
Iterator<Registry> iterator = registries.iterator(); Iterator<Registry> iterator = registries.iterator();
Set<URL> urls = null; Set<URL> urls = null;
ZookeeperRegistry zookperRegster=null; ZookeeperRegistry zookperRegster=null;
...@@ -217,10 +163,12 @@ public class RegistryServerSync extends Observable implements InitializingBean, ...@@ -217,10 +163,12 @@ public class RegistryServerSync extends Observable implements InitializingBean,
urls = zookperRegster.getRegistered(); urls = zookperRegster.getRegistered();
logger.info("RegistryServerSync urls-------------->"+((urls!=null&&urls.size()>0)?JSON.toJSONString(urls):"url si null")); logger.info("RegistryServerSync urls-------------->"+((urls!=null&&urls.size()>0)?JSON.toJSONString(urls):"url si null"));
if (urls != null && urls.size() > 0) { if (urls != null && urls.size() > 0) {
subscribedUrl = new ArrayList<URL>();
for(URL value: urls){ for(URL value: urls){
try { try {
logger.info("RegistryServerSync subscribe urls " + JSON.toJSONString(value)); logger.info("RegistryServerSync subscribe urls " + JSON.toJSONString(value));
registry.subscribe(value,this); registry.subscribe(value,this);
subscribedUrl.add(value);
}catch (Exception e){ }catch (Exception e){
logger.info(" error :matirx-monitor offDubbo " + Stack.errInfo(e) + StringUtil.line()); logger.info(" error :matirx-monitor offDubbo " + Stack.errInfo(e) + StringUtil.line());
} }
......
...@@ -19,42 +19,15 @@ package com.secoo.mall.dubbo.monitor.dubbo.service.impl; ...@@ -19,42 +19,15 @@ package com.secoo.mall.dubbo.monitor.dubbo.service.impl;
import com.secoo.mall.dubbo.monitor.dubbo.service.RegistryServerSync; import com.secoo.mall.dubbo.monitor.dubbo.service.RegistryServerSync;
import org.apache.dubbo.common.URL; import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.registry.Registry;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
public class AbstractService { public class AbstractService {
protected static final Logger logger = LoggerFactory.getLogger(AbstractService.class);
//@Autowired
protected Registry registry;
// @Autowired
private RegistryServerSync sync; private RegistryServerSync sync;
public AbstractService(Registry registry, RegistryServerSync sync) { public AbstractService(RegistryServerSync sync) {
this.registry=registry;
this.sync=sync;
}
public Registry getRegistry() {
return registry;
}
public void setRegistry(Registry registry) {
this.registry = registry;
}
public RegistryServerSync getSync() {
return sync;
}
public void setSync(RegistryServerSync sync) {
this.sync = sync; this.sync = sync;
} }
......
/*
* 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.secoo.mall.dubbo.monitor.dubbo.service.impl;
import com.secoo.mall.dubbo.monitor.dubbo.Constants.Constants;
import com.secoo.mall.dubbo.monitor.dubbo.model.domain.Consumer;
import com.secoo.mall.dubbo.monitor.dubbo.service.ConsumerService;
import com.secoo.mall.dubbo.monitor.dubbo.service.RegistryServerSync;
import com.secoo.mall.dubbo.monitor.utils.SyncUtils;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.registry.Registry;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class ConsumerServiceImpl extends AbstractService implements ConsumerService {
public ConsumerServiceImpl(Registry registry, RegistryServerSync sync) {
super(registry, sync);
}
@Override
public List<Consumer> findByService(String service) {
return SyncUtils.url2ConsumerList(findConsumerUrlByService(service));
}
@Override
public List<Consumer> findAll() {
return SyncUtils.url2ConsumerList(findAllConsumerUrl());
}
private Map<String, URL> findAllConsumerUrl() {
Map<String, String> filter = new HashMap<String, String>();
filter.put(Constants.CATEGORY_KEY, Constants.CONSUMERS_CATEGORY);
return SyncUtils.filterFromCategory(getRegistryCache(), filter);
}
@Override
public List<Consumer> findByAddress(String consumerAddress) {
return SyncUtils.url2ConsumerList(findConsumerUrlByAddress(consumerAddress));
}
private Map<String, URL> findConsumerUrlByAddress(String address) {
Map<String, String> filter = new HashMap<String, String>();
filter.put(Constants.CATEGORY_KEY, Constants.CONSUMERS_CATEGORY);
filter.put(SyncUtils.ADDRESS_FILTER_KEY, address);
return SyncUtils.filterFromCategory(getRegistryCache(), filter);
}
public Map<String, URL> findConsumerUrlByService(String service) {
Map<String, String> filter = new HashMap<String, String>();
filter.put(Constants.CATEGORY_KEY, Constants.CONSUMERS_CATEGORY);
filter.put(SyncUtils.SERVICE_FILTER_KEY, service);
return SyncUtils.filterFromCategory(getRegistryCache(), filter);
}
}
/*
* 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.secoo.mall.dubbo.monitor.utils;
import com.secoo.mall.dubbo.monitor.dubbo.Constants.Constants;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.metadata.definition.model.MethodDefinition;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ConvertUtil {
private ConvertUtil() {
}
public static Map<String, String> serviceName2Map(String serviceName) {
String group = Tool.getGroup(serviceName);
String version = Tool.getVersion(serviceName);
String interfaze = Tool.getInterface(serviceName);
Map<String, String> ret = new HashMap<String, String>();
if (!StringUtils.isEmpty(serviceName)) {
ret.put(Constants.INTERFACE_KEY, interfaze);
}
if (!StringUtils.isEmpty(version)) {
ret.put(Constants.VERSION_KEY, version);
}
if (!StringUtils.isEmpty(group)) {
ret.put(Constants.GROUP_KEY, group);
}
return ret;
}
public static Map methodList2Map(List<MethodDefinition> methods) {
Map<String, MethodDefinition> res = new HashMap<>();
for (int i = 0; i < methods.size(); i++) {
res.put(methods.get(i).getName(), methods.get(i));
}
return res;
}
}
package com.secoo.mall.dubbo.monitor.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileInputStream;
import java.util.HashMap;
/**
* @ClassName FileSystemUtils
* @Author QIANGLU
* @Date 2019/12/4 2:08 下午
* @Version 1.0
*/
public class FileUploadUtils {
private static String DEV_PATH = "http://172.17.105.26:6080/file-server/api/v1/upload/single";
private static Logger logger = LoggerFactory.getLogger(FileUploadUtils.class);
private static String APP_SECRET = "exwarn_secoo";
/**
* 上传文件
*
* @param fileName 文件名称
* @param path 内容
* @param appid 应用唯一标识
* @param uri 发送地址
*/
public static String uploadFile(String fileName, String path, String appid, String uri, String appSecret) {
try {
if (StringUtils.isEmpty(uri)) {
logger.warn("uploadFile uri can not null");
return "";
}
logger.info("开始进行文件上传,fileName:{} , path: {} ,appid:{} ", fileName, path, appid);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody("fileName", fileName);
builder.addTextBody("appId", appid);
builder.addTextBody("appSecret", appSecret);
builder.addTextBody("storageClass", "2");
builder.addTextBody("isDownloadLink", "1");
builder.addBinaryBody("file", new FileInputStream(path), ContentType.MULTIPART_FORM_DATA, fileName);
HttpEntity entity = builder.build();
String result = HttpClientUtils.doPost(uri, entity, new HashMap<>());
JSONObject obj = JSON.parseObject(result);
Integer code = obj.getInteger("code");
if (code == null && code.intValue() != 0) {
logger.error("upload fail :{}", result);
return "";
}
logger.info("upload succ -> filename:{},result:{}", fileName, result);
JSONObject data = obj.getJSONObject("data");
return data.getString("filePath");
} catch (Exception e) {
logger.warn("upload file fial,fileName:{},appid:{}.,error:{}", fileName, appid, e);
}
return "";
}
public static String downloadFile(String fileName, String appid, String uri) {
if (StringUtils.isEmpty(uri)) {
logger.warn("uploadFile uri can not null");
return null;
}
try {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody("appId", appid);
builder.addTextBody("fileName", fileName);
builder.addTextBody("policy", "1");
builder.addTextBody("appSecret", APP_SECRET);
HttpEntity entity = builder.build();
String result = HttpClientUtils.doPost(uri, entity, new HashMap<>());
JSONObject datas = JSON.parseObject(result);
JSONObject data = datas.getJSONObject("data");
return data != null ? data.getString("filePath") : null;
} catch (Exception e) {
logger.error("下载文件出错,fileName:{},appid:{},error:{}", fileName, appid, e);
}
return null;
}
}
package com.secoo.mall.dubbo.monitor.utils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
/**
* @author luqiang
*/
public class GzipUtils {
private static Logger logger = LoggerFactory.getLogger(GzipUtils.class);
/**
* 压缩字符串
*
* @param body 压缩的字符串
* @return 压缩后的字符串
*/
public static String compress(String body) {
if (StringUtils.isEmpty(body)) {
return body;
}
try {
ByteArrayOutputStream outputStream = compressToStream(body);
if (outputStream != null) {
// 通过解码字节将缓冲区内容转换为字符串
return new String(outputStream.toByteArray(), "ISO-8859-1");
}
} catch (Exception e) {
logger.warn("GZIP compress 压缩失败,使用源文件", e);
}
return body;
}
/**
* 压缩字符串
*
* @param body 压缩的字符串
* @return 压缩后的字符串
*/
public static ByteArrayOutputStream compressToStream(String body) {
try (
ByteArrayOutputStream bos = new ByteArrayOutputStream();
GZIPOutputStream os = new GZIPOutputStream(bos);
) {
// 写入输出流
os.write(body.getBytes());
return bos;
} catch (IOException e) {
logger.warn("GZIP compressToStream 压缩失败,使用源文件", e);
}
return null;
}
/**
* 解压缩字符串
*
* @param body 解压缩的字符串
* @return 解压后的字符串
*/
public static String decompress(String body) {
if (StringUtils.isEmpty(body)) {
return body;
}
byte[] buf = new byte[1024];
int len = 0;
try (
ByteArrayInputStream bis = new ByteArrayInputStream(body.getBytes("ISO-8859-1"));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
GZIPInputStream is = new GZIPInputStream(bis);
) {
// 将未压缩数据读入字节数组
while ((len = is.read(buf)) != -1) {
// 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此byte数组输出流
bos.write(buf, 0, len);
}
// 通过解码字节将缓冲区内容转换为字符串
return new String(bos.toByteArray());
} catch (Exception e) {
logger.warn("GZIP 解压失败,使用源文件", e);
return body;
}
}
}
package com.secoo.mall.dubbo.monitor.utils;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
/**
* @ClassName Md5Utils
* @Author QIANGLU
* @Date 2019/12/30 9:05 上午
* @Version 1.0
*/
public class Md5Utils {
private static final int HEX_VALUE_COUNT = 16;
public Md5Utils() {
}
public static String getMD5(byte[] bytes) {
char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
char[] str = new char[32];
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(bytes);
byte[] tmp = md.digest();
int k = 0;
for (int i = 0; i < HEX_VALUE_COUNT; ++i) {
byte byte0 = tmp[i];
str[k++] = hexDigits[byte0 >>> 4 & 15];
str[k++] = hexDigits[byte0 & 15];
}
} catch (Exception var8) {
var8.printStackTrace();
}
return new String(str);
}
public static String getMD5(String value, String encode) {
String result = "";
try {
result = getMD5(value.getBytes(encode));
} catch (UnsupportedEncodingException var4) {
var4.printStackTrace();
}
return result;
}
}
package com.secoo.mall.dubbo.monitor.utils;
import java.lang.management.ManagementFactory;
import java.net.*;
import java.util.*;
/**
* @author luqiang
*/
public class OSUtil {
private static volatile String OS_NAME;
private static volatile String HOST_NAME;
private static volatile List<String> IPV4_LIST;
private static volatile int PROCESS_NO = 0;
public static int getAvailableProcessors() {
return Runtime.getRuntime().availableProcessors() - 1;
}
public static String getOsName() {
if (OS_NAME == null) {
OS_NAME = System.getProperty("os.name");
}
return OS_NAME;
}
public static String getHostName() {
if (HOST_NAME == null) {
try {
InetAddress host = InetAddress.getLocalHost();
HOST_NAME = host.getHostName();
} catch (UnknownHostException e) {
HOST_NAME = "unknown";
}
}
return HOST_NAME;
}
/**
* 获取系统环境变量分隔符
*
* @return
*/
public static String getPathSeparator() {
return System.getProperty("path.separator");
}
public static List<String> getAllIPV4() {
if (IPV4_LIST == null) {
IPV4_LIST = new LinkedList<String>();
try {
Enumeration<NetworkInterface> interfs = NetworkInterface.getNetworkInterfaces();
while (interfs.hasMoreElements()) {
NetworkInterface networkInterface = interfs.nextElement();
Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
while (inetAddresses.hasMoreElements()) {
InetAddress address = inetAddresses.nextElement();
if (address instanceof Inet4Address) {
String addressStr = address.getHostAddress();
if ("127.0.0.1".equals(addressStr)) {
continue;
}
IPV4_LIST.add(addressStr);
}
}
}
} catch (SocketException e) {
}
}
return IPV4_LIST;
}
public static int getProcessNo() {
if (PROCESS_NO == 0) {
try {
PROCESS_NO = Integer.parseInt(ManagementFactory.getRuntimeMXBean().getName().split("@")[0]);
} catch (Exception e) {
PROCESS_NO = -1;
}
}
return PROCESS_NO;
}
public static Map<String, String> buildOSInfo() {
Map<String, String> osInfo = new HashMap<>();
String osName = getOsName();
if (osName != null) {
osInfo.put("os_name", osName);
}
String hostName = getHostName();
if (hostName != null) {
osInfo.put("host_name", hostName);
}
List<String> allIPV4 = getAllIPV4();
if (allIPV4.size() > 0) {
osInfo.put("ipv4", Arrays.toString(allIPV4.toArray()));
}
osInfo.put("process_no", getProcessNo() + "");
osInfo.put("language", "java");
return osInfo;
}
}
/*
* 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.secoo.mall.dubbo.monitor.utils;
import java.util.Map;
public class Pair<K, V> implements Map.Entry<K, V> {
private K key;
private V value;
public Pair() {
}
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() {
return key;
}
public void setKey(K key) {
this.key = key;
}
public V getValue() {
return value;
}
public V setValue(V value) {
V old = this.value;
this.value = value;
return old;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((key == null) ? 0 : key.hashCode());
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Pair<?, ?> other = (Pair<?, ?>) obj;
if (key == null) {
if (other.key != null)
return false;
} else if (!key.equals(other.key))
return false;
if (value == null) {
if (other.value != null)
return false;
} else if (!value.equals(other.value))
return false;
return true;
}
}
\ No newline at end of file
package com.secoo.mall.dubbo.monitor.utils;
import lombok.extern.slf4j.Slf4j;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
@Slf4j
public class ProcessUtil {
/**
* Buffer size of process input-stream (used for reading the
* output (sic!) of the process). Currently 64KB.
*/
public static final int BUFFER_SIZE = 65536;
public static final int EXEC_TIME_OUT = 2;
public static final String CMD_PRX = "ffmpeg -i ";
private ExecutorService exec;
private ProcessUtil() {
exec = new ThreadPoolExecutor(6,
12,
1,
TimeUnit.MINUTES,
new LinkedBlockingQueue<>(10),
new CustomThreadFactory("cmd-process"),
new ThreadPoolExecutor.CallerRunsPolicy());
}
public static ProcessUtil instance() {
return InputStreamConsumer.instance;
}
/**
* 处理基础命令
*
* @return
*/
public String cmdStr(String org, String def) {
StringBuffer buffer = new StringBuffer();
buffer.append(CMD_PRX).append(org).append(" ").append(def);
return buffer.toString();
}
/**
* 简单的封装, 执行cmd命令
*
* @param cmd 待执行的操作命令
* @return
* @throws IOException
* @throws InterruptedException
*/
public boolean process(String cmd) {
try {
log.info("开始进行文件webm -> mp4转换,cmd:{}", cmd);
Process process = Runtime.getRuntime().exec(cmd);
waitForProcess(process);
} catch (Exception e) {
log.error("process video cmd:{}", cmd, e);
return false;
}
return true;
}
/**
* Perform process input/output and wait for process to terminate.
* <p>
* 源码参考 im4java 的实现修改而来
*/
private int waitForProcess(final Process pProcess)
throws IOException, InterruptedException, TimeoutException, ExecutionException {
// Process stdout and stderr of subprocess in parallel.
// This prevents deadlock under Windows, if there is a lot of
// stderr-output (e.g. from ghostscript called by convert)
FutureTask<Object> outTask = new FutureTask<Object>(() -> {
processOutput(pProcess.getInputStream(), InputStreamConsumer.DEFAULT_CONSUMER);
return null;
});
exec.submit(outTask);
FutureTask<Object> errTask = new FutureTask<Object>(() -> {
processError(pProcess.getErrorStream(), InputStreamConsumer.DEFAULT_CONSUMER);
return null;
});
exec.submit(errTask);
// Wait and check IO exceptions (FutureTask.get() blocks).
try {
outTask.get();
errTask.get();
} catch (ExecutionException e) {
Throwable t = e.getCause();
if (t instanceof IOException) {
throw (IOException) t;
} else if (t instanceof RuntimeException) {
throw (RuntimeException) t;
} else {
throw new IllegalStateException(e);
}
}
FutureTask<Integer> processTask = new FutureTask<Integer>(() -> {
pProcess.waitFor();
return pProcess.exitValue();
});
exec.submit(processTask);
// 设置超时时间,防止死等
int rc = processTask.get(EXEC_TIME_OUT, TimeUnit.SECONDS);
// just to be on the safe side
try {
pProcess.getInputStream().close();
pProcess.getOutputStream().close();
pProcess.getErrorStream().close();
} catch (Exception e) {
log.error("close stream error! e: {}", e);
}
return rc;
}
//////////////////////////////////////////////////////////////////////////////
/**
* Let the OutputConsumer process the output of the command.
* <p>
* 方便后续对输出流的扩展
*/
private void processOutput(InputStream pInputStream,
InputStreamConsumer pConsumer) throws IOException {
pConsumer.consume(pInputStream);
}
/**
* Let the ErrorConsumer process the stderr-stream.
* <p>
* 方便对后续异常流的处理
*/
private void processError(InputStream pInputStream,
InputStreamConsumer pConsumer) throws IOException {
pConsumer.consume(pInputStream);
}
private static class InputStreamConsumer {
static ProcessUtil instance = new ProcessUtil();
static InputStreamConsumer DEFAULT_CONSUMER = new InputStreamConsumer();
void consume(InputStream stream) throws IOException {
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream), BUFFER_SIZE);
String temp;
while ((temp = reader.readLine()) != null) {
builder.append(temp);
}
if (log.isDebugEnabled()) {
log.info("cmd process input stream: {}", builder.toString());
}
reader.close();
}
}
private static class CustomThreadFactory implements ThreadFactory {
private String name;
private AtomicInteger count = new AtomicInteger(0);
public CustomThreadFactory(String name) {
this.name = name;
}
@Override
public Thread newThread(Runnable r) {
return new Thread(r, name + "-" + count.addAndGet(1));
}
}
}
/*
* 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.secoo.mall.dubbo.monitor.utils;
import com.secoo.mall.dubbo.monitor.dubbo.Constants.Constants;
import com.secoo.mall.dubbo.monitor.dubbo.model.domain.Consumer;
import com.secoo.mall.dubbo.monitor.dubbo.model.domain.Provider;
import org.apache.dubbo.common.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SyncUtils {
public static final String SERVICE_FILTER_KEY = ".service";
public static final String ADDRESS_FILTER_KEY = ".address";
public static final String ID_FILTER_KEY = ".id";
public static final String COLON = ":";
public static Provider url2Provider(Pair<String, URL> pair) {
if (pair == null) {
return null;
}
String id = pair.getKey();
URL url = pair.getValue();
if (url == null)
return null;
Provider p = new Provider();
p.setHash(id);
p.setService(url.getServiceKey());
p.setAddress(url.getAddress());
p.setApplication(url.getParameter(Constants.APPLICATION_KEY));
p.setUrl(url.toIdentityString());
p.setParameters(url.toParameterString());
p.setDynamic(url.getParameter("dynamic", true));
p.setEnabled(url.getParameter(Constants.ENABLED_KEY, true));
p.setWeight(url.getParameter(Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT));
p.setUsername(url.getParameter("owner"));
return p;
}
public static List<Provider> url2ProviderList(Map<String, URL> ps) {
List<Provider> ret = new ArrayList<>();
for (Map.Entry<String, URL> entry : ps.entrySet()) {
ret.add(url2Provider(new Pair<>(entry.getKey(), entry.getValue())));
}
return ret;
}
public static Consumer url2Consumer(Pair<String, URL> pair) {
if (pair == null) {
return null;
}
String id = pair.getKey();
URL url = pair.getValue();
if (null == url)
return null;
Consumer c = new Consumer();
c.setHash(id);
c.setService(url.getServiceKey());
c.setAddress(url.getHost());
c.setApplication(url.getParameter(Constants.APPLICATION_KEY));
c.setParameters(url.toParameterString());
return c;
}
public static List<Consumer> url2ConsumerList(Map<String, URL> cs) {
List<Consumer> list = new ArrayList<Consumer>();
if (cs == null) return list;
for (Map.Entry<String, URL> entry : cs.entrySet()) {
list.add(url2Consumer(new Pair<>(entry.getKey(), entry.getValue())));
}
return list;
}
// Map<category, Map<servicename, Map<Long, URL>>>0 = {ConcurrentHashMap$MapEntry@8908} "com.imooc.springboot.dubbo.demo.DemoService" -> " size = 2"
public static <SM extends Map<String, Map<String, URL>>> Map<String, URL> filterFromCategory(Map<String, SM> urls, Map<String, String> filter) {
String c = (String) filter.get(Constants.CATEGORY_KEY);
if (c == null) throw new IllegalArgumentException("no category");
filter.remove(Constants.CATEGORY_KEY);
return filterFromService(urls.get(c), filter);
}
// Map<servicename, Map<Long, URL>>
public static Map<String, URL> filterFromService(Map<String, Map<String, URL>> urls, Map<String, String> filter) {
Map<String, URL> ret = new HashMap<>();
if (urls == null) return ret;
String s = (String) filter.remove(SERVICE_FILTER_KEY);
if (s == null) {
for (Map.Entry<String, Map<String, URL>> entry : urls.entrySet()) {
filterFromUrls(entry.getValue(), ret, filter); //根据ip 或application 查询
}
} else {
Map<String, URL> map = urls.get(s);
filterFromUrls(map, ret, filter);
}
return ret;
}
// Map<Long, URL>
static void filterFromUrls(Map<String, URL> from, Map<String, URL> to, Map<String, String> filter) {
if (from == null || from.isEmpty()) return;
for (Map.Entry<String, URL> entry : from.entrySet()) {
URL url = entry.getValue();
boolean match = true;
for (Map.Entry<String, String> e : filter.entrySet()) {
String key = e.getKey();
String value = e.getValue();
if (ADDRESS_FILTER_KEY.equals(key)) {
// value is address:port
if (value.contains(COLON)) {
if (!value.equals(url.getIp() + COLON + url.getPort())) {
match = false;
break;
}
} else { // value is just address
if (!value.equals(url.getIp())) {
match = false;
break;
}
}
} else {
if (!value.equals(url.getParameter(key))) {
match = false;
break;
}
}
}
if (match) {
to.put(entry.getKey(), url);
}
}
}
public static <SM extends Map<String, Map<String, URL>>> Pair<String, URL> filterFromCategory(Map<String, SM> urls, String category, String id) {
SM services = urls.get(category);
if (services == null) return null;
for (Map.Entry<String, Map<String, URL>> e1 : services.entrySet()) {
Map<String, URL> u = e1.getValue();
if (u.containsKey(id)) return new Pair<>(id, u.get(id));
}
return null;
}
}
...@@ -3,13 +3,13 @@ package com.secoo.mall.dubbo.service; ...@@ -3,13 +3,13 @@ package com.secoo.mall.dubbo.service;
import com.secoo.mall.common.core.service.StopService; import com.secoo.mall.common.core.service.StopService;
import com.secoo.mall.common.core.bean.gracefulshowtdownBean.ExecutorDetail; import com.secoo.mall.common.core.bean.gracefulshowtdownBean.ExecutorDetail;
public abstract class AbstractStop implements StopService<ExecutorDetail>,Comparable { public abstract class AbstractShutDown implements StopService<ExecutorDetail>,Comparable {
//定义执行顺序 //定义执行顺序
public abstract Integer getHandleTypeOrder(); public abstract Integer getHandleTypeOrder();
@Override @Override
public int compareTo(Object o) { public int compareTo(Object o) {
AbstractStop stop=(AbstractStop) o; AbstractShutDown stop=(AbstractShutDown) o;
int result = (this.getHandleTypeOrder()>stop.getHandleTypeOrder()) ? 1 : -1;//升序 int result = (this.getHandleTypeOrder()>stop.getHandleTypeOrder()) ? 1 : -1;//升序
return result ; return result ;
} }
......
package com.secoo.mall.dubbo.service; package com.secoo.mall.dubbo.service;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.secoo.mall.common.core.bean.gracefulshowtdownBean.ExecutorDetail;
import com.secoo.mall.common.core.bean.gracefulshowtdownBean.ExecutorDetails;
import com.secoo.mall.common.core.service.StopService; import com.secoo.mall.common.core.service.StopService;
import com.secoo.mall.common.core.service.UpDatas; import com.secoo.mall.common.core.service.UpDatas;
import com.secoo.mall.common.util.date.DateUtil; import com.secoo.mall.common.util.date.DateUtil;
import com.secoo.mall.common.util.log.LoggerUtil; import com.secoo.mall.common.util.log.LoggerUtil;
import com.secoo.mall.common.core.bean.gracefulshowtdownBean.ExecutorDetail; import com.secoo.mall.dubbo.service.impl.TomcatShutDownHook;
import com.secoo.mall.common.core.bean.gracefulshowtdownBean.ExecutorDetails;
import com.secoo.mall.dubbo.monitor.dubbo.service.RegistryServerSync;
import com.secoo.mall.dubbo.service.impl.TomcatShutDownHock;
import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.config.DubboShutdownHook; import org.apache.dubbo.config.DubboShutdownHook;
import org.apache.dubbo.config.spring.extension.SpringExtensionFactory; import org.apache.dubbo.config.spring.extension.SpringExtensionFactory;
...@@ -29,21 +28,12 @@ import java.util.*; ...@@ -29,21 +28,12 @@ import java.util.*;
import static org.springframework.context.support.AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME; import static org.springframework.context.support.AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME;
public class GracefulShutDown implements CommandLineRunner, ApplicationListener<ContextClosedEvent>,ApplicationContextAware , Ordered { public class GracefulShutDown implements CommandLineRunner, ApplicationListener<ContextClosedEvent>, ApplicationContextAware, Ordered {
private final Logger logger = LoggerFactory.getLogger(this.getClass()); private final Logger logger = LoggerFactory.getLogger(this.getClass());
// TomcatShutDownHock tomcatShutDownHock;
private ApplicationContext context;
// private ProviderService providerService ;
RegistryServerSync registryServerSync;
private ApplicationContext context;
UpDatas dubboUpdata; UpDatas dubboUpdata;
// public GracefulShutDown(TomcatShutDownHock tomcatShutDownHock,ProviderService providerService,UpDatas dubboUpdata){
// this.tomcatShutDownHock=tomcatShutDownHock;
// this.providerService=providerService;
// this.dubboUpdata=dubboUpdata;
// }
public GracefulShutDown(UpDatas dubboUpdata) { public GracefulShutDown(UpDatas dubboUpdata) {
this.dubboUpdata = dubboUpdata; this.dubboUpdata = dubboUpdata;
...@@ -63,10 +53,20 @@ public class GracefulShutDown implements CommandLineRunner, ApplicationListener< ...@@ -63,10 +53,20 @@ public class GracefulShutDown implements CommandLineRunner, ApplicationListener<
multicaster.removeApplicationListener((ApplicationListener) getPrivateConst("SHUTDOWN_HOOK_LISTENER")); multicaster.removeApplicationListener((ApplicationListener) getPrivateConst("SHUTDOWN_HOOK_LISTENER"));
} }
}
public static Object getPrivateConst(String field) {
try {
Field f = SpringExtensionFactory.class.getDeclaredField(field);
f.setAccessible(true);
return f.get(null);
} catch (Exception e) {
}
return null;
}
public void test() {
//test-------------------------------------> begin logger.info("222222222222 closeevent");
ExecutorDetails result = new ExecutorDetails(); ExecutorDetails result = new ExecutorDetails();
result.setCode(0); result.setCode(0);
List<ExecutorDetail> details = new ArrayList<ExecutorDetail>(); List<ExecutorDetail> details = new ArrayList<ExecutorDetail>();
...@@ -75,12 +75,15 @@ public class GracefulShutDown implements CommandLineRunner, ApplicationListener< ...@@ -75,12 +75,15 @@ public class GracefulShutDown implements CommandLineRunner, ApplicationListener<
String name = ApplicationModel.getApplication(); String name = ApplicationModel.getApplication();
result.setIp(ip); result.setIp(ip);
result.setName(name); result.setName(name);
logger.info("GracefulShutDown execute:name:" + name + " ip:" + ip + " time:" + DateUtil.getDateTime());
Map<String, StopService> map = this.context.getBeansOfType(StopService.class); try {
Map<String, StopService> map = context.getBeansOfType(StopService.class);
if (map != null && map.size() > 0) { if (map != null && map.size() > 0) {
Set<StopService> ts = new TreeSet<StopService>(); Set<StopService> ts = new TreeSet<StopService>();
for (StopService service : map.values()) { for (StopService service : map.values()) {
if (!(service instanceof TomcatShutDownHock)) { if (!(service instanceof TomcatShutDownHook)) {
ts.add(service); ts.add(service);
} }
} }
...@@ -88,35 +91,25 @@ public class GracefulShutDown implements CommandLineRunner, ApplicationListener< ...@@ -88,35 +91,25 @@ public class GracefulShutDown implements CommandLineRunner, ApplicationListener<
Iterator<StopService> it = ts.iterator(); Iterator<StopService> it = ts.iterator();
while (it.hasNext()) { while (it.hasNext()) {
StopService service = (StopService) it.next(); StopService service = (StopService) it.next();
// ExecutorDetail one = (ExecutorDetail) service.stop(); ExecutorDetail one = (ExecutorDetail) service.stop();
// if (one != null) { if (one != null) {
// details.add(one); details.add(one);
// if (one.getCode() != 0) { if (one.getCode() != 0) {
// result.setCode(-1); result.setCode(-1);
// } }
// } }
}
try {
dubboUpdata.upData(result);
} catch (Exception e) {
LoggerUtil.error("matrix.GracefulShutDown update .error data:" + JSON.toJSONString(result), e);
} }
//test------------------------------------->
} }
} catch (Exception e) {
} LoggerUtil.error("matrix.GracefulShutDown.error", e);
}
public static Object getPrivateConst(String field) {
try { try {
Field f = SpringExtensionFactory.class.getDeclaredField(field); dubboUpdata.upData(result);
f.setAccessible(true);
return f.get(null);
} catch (Exception e) { } catch (Exception e) {
LoggerUtil.error("matrix.GracefulShutDown update .error data:" + JSON.toJSONString(result), e);
} }
return null; logger.info("GracefulShutDown execute end :name:" + name + " ip:" + ip + " time:" + DateUtil.getDateTime());
} }
...@@ -131,7 +124,7 @@ public class GracefulShutDown implements CommandLineRunner, ApplicationListener< ...@@ -131,7 +124,7 @@ public class GracefulShutDown implements CommandLineRunner, ApplicationListener<
String name = ApplicationModel.getApplication(); String name = ApplicationModel.getApplication();
result.setIp(ip); result.setIp(ip);
result.setName(name); result.setName(name);
logger.info("GracefulShutDown execute:name:"+name+" ip:"+ip+" time:"+ DateUtil.getDateTime()); logger.info("GracefulShutDown execute:name:" + name + " ip:" + ip + " time:" + DateUtil.getDateTime());
try { try {
...@@ -139,7 +132,7 @@ public class GracefulShutDown implements CommandLineRunner, ApplicationListener< ...@@ -139,7 +132,7 @@ public class GracefulShutDown implements CommandLineRunner, ApplicationListener<
if (map != null && map.size() > 0) { if (map != null && map.size() > 0) {
Set<StopService> ts = new TreeSet<StopService>(); Set<StopService> ts = new TreeSet<StopService>();
for (StopService service : map.values()) { for (StopService service : map.values()) {
if(!(service instanceof TomcatGracefulShutDownEntrance)) { if (!(service instanceof TomcatGracefulShutDown)) {
ts.add(service); ts.add(service);
} }
} }
...@@ -165,13 +158,13 @@ public class GracefulShutDown implements CommandLineRunner, ApplicationListener< ...@@ -165,13 +158,13 @@ public class GracefulShutDown implements CommandLineRunner, ApplicationListener<
} catch (Exception e) { } catch (Exception e) {
LoggerUtil.error("matrix.GracefulShutDown update .error data:" + JSON.toJSONString(result), e); LoggerUtil.error("matrix.GracefulShutDown update .error data:" + JSON.toJSONString(result), e);
} }
logger.info("GracefulShutDown execute end :name:"+name+" ip:"+ip+" time:"+ DateUtil.getDateTime()); logger.info("GracefulShutDown execute end :name:" + name + " ip:" + ip + " time:" + DateUtil.getDateTime());
} }
@Override @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context=applicationContext; this.context = applicationContext;
logger.info("222222222222 setContext"); logger.info("222222222222 setContext");
} }
......
...@@ -14,7 +14,7 @@ import org.slf4j.LoggerFactory; ...@@ -14,7 +14,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
public class DubboUpdata implements UpDatas<ExecutorDetails> { public class ShutDownDataReport implements UpDatas<ExecutorDetails> {
private final Logger logger = LoggerFactory.getLogger(this.getClass()); private final Logger logger = LoggerFactory.getLogger(this.getClass());
String path; String path;
URL url; URL url;
...@@ -45,7 +45,7 @@ public class DubboUpdata implements UpDatas<ExecutorDetails> { ...@@ -45,7 +45,7 @@ public class DubboUpdata implements UpDatas<ExecutorDetails> {
try { try {
ZookeeperClient cliet = zookeeperTransporter.connect(url); ZookeeperClient cliet = zookeeperTransporter.connect(url);
// String monitor = "/monitorZ/" + obj.getName() + "/" + obj.getIp(); // String monitor = "/monitorZ/" + obj.getName() + "/" + obj.getIp();
logger.info("DubboUpdata sendData: path:"+monitor+" data:"+ JSON.toJSONString(obj)); logger.info("ShutDownDataReport sendData: path:"+monitor+" data:"+ JSON.toJSONString(obj));
cliet.create(monitor, JSON.toJSONString(obj), true); cliet.create(monitor, JSON.toJSONString(obj), true);
} catch (Exception e) { } catch (Exception e) {
LoggerUtil.info("Exception e:" ,e); LoggerUtil.info("Exception e:" ,e);
......
package com.secoo.mall.dubbo.service; package com.secoo.mall.dubbo.service;
import com.secoo.mall.dubbo.service.impl.TomcatShutDownHock; import com.secoo.mall.dubbo.service.impl.TomcatShutDownHook;
import org.apache.catalina.connector.Connector; import org.apache.catalina.connector.Connector;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -12,22 +12,22 @@ import org.springframework.context.ApplicationListener; ...@@ -12,22 +12,22 @@ import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent; import org.springframework.context.event.ContextClosedEvent;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
public class TomcatGracefulShutDownEntrance implements TomcatConnectorCustomizer , ApplicationContextAware, Ordered , ApplicationListener<ContextClosedEvent> { public class TomcatGracefulShutDown implements TomcatConnectorCustomizer , ApplicationContextAware, Ordered , ApplicationListener<ContextClosedEvent> {
private ApplicationContext context; private ApplicationContext context;
TomcatShutDownHock tomcatShutDownHock; TomcatShutDownHook tomcatShutDownHook;
private final Logger logger = LoggerFactory.getLogger(this.getClass()); private final Logger logger = LoggerFactory.getLogger(this.getClass());
public TomcatGracefulShutDownEntrance(TomcatShutDownHock tomcatShutDownHock) { public TomcatGracefulShutDown(TomcatShutDownHook tomcatShutDownHook) {
this.tomcatShutDownHock = tomcatShutDownHock; this.tomcatShutDownHook = tomcatShutDownHook;
} }
@Override @Override
public void customize(Connector connector) { public void customize(Connector connector) {
tomcatShutDownHock.setConnector(connector); tomcatShutDownHook.setConnector(connector);
} }
...@@ -36,12 +36,12 @@ public class TomcatGracefulShutDownEntrance implements TomcatConnectorCustomizer ...@@ -36,12 +36,12 @@ public class TomcatGracefulShutDownEntrance implements TomcatConnectorCustomizer
this.context = applicationContext; this.context = applicationContext;
logger.info("11111111111111111111 setContext"); logger.info("11111111111111111111 setContext");
} }
public TomcatShutDownHock getTomcatShutDownHock() { public TomcatShutDownHook getTomcatShutDownHook() {
return tomcatShutDownHock; return tomcatShutDownHook;
} }
public void setTomcatShutDownHock(TomcatShutDownHock tomcatShutDownHock) { public void setTomcatShutDownHook(TomcatShutDownHook tomcatShutDownHook) {
this.tomcatShutDownHock = tomcatShutDownHock; this.tomcatShutDownHook = tomcatShutDownHook;
} }
@Override @Override
...@@ -52,6 +52,6 @@ public class TomcatGracefulShutDownEntrance implements TomcatConnectorCustomizer ...@@ -52,6 +52,6 @@ public class TomcatGracefulShutDownEntrance implements TomcatConnectorCustomizer
@Override @Override
public void onApplicationEvent(ContextClosedEvent contextClosedEvent) { public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
logger.info("11111111111111111111 "); logger.info("11111111111111111111 ");
tomcatShutDownHock.stop(); tomcatShutDownHook.stop();
} }
} }
...@@ -8,7 +8,7 @@ import com.secoo.mall.common.util.string.StringUtil; ...@@ -8,7 +8,7 @@ import com.secoo.mall.common.util.string.StringUtil;
import com.secoo.mall.dubbo.monitor.dubbo.service.ProviderService; import com.secoo.mall.dubbo.monitor.dubbo.service.ProviderService;
import com.secoo.mall.dubbo.monitor.dubbo.service.RegistryServerSync; import com.secoo.mall.dubbo.monitor.dubbo.service.RegistryServerSync;
import com.secoo.mall.dubbo.monitor.utils.Stack; import com.secoo.mall.dubbo.monitor.utils.Stack;
import com.secoo.mall.dubbo.service.AbstractStop; import com.secoo.mall.dubbo.service.AbstractShutDown;
import org.apache.dubbo.common.URL; import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.ExtensionLoader; import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.utils.NetUtils; import org.apache.dubbo.common.utils.NetUtils;
...@@ -28,7 +28,7 @@ import java.util.*; ...@@ -28,7 +28,7 @@ import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.*;
public class DubboCustomerShutDownHock extends AbstractStop implements Observer { public class DubboCustomerShutDownHook extends AbstractShutDown implements Observer {
private ProviderService providerService; private ProviderService providerService;
RegistryServerSync registryServerSync; RegistryServerSync registryServerSync;
...@@ -41,7 +41,7 @@ public class DubboCustomerShutDownHock extends AbstractStop implements Observer ...@@ -41,7 +41,7 @@ public class DubboCustomerShutDownHock extends AbstractStop implements Observer
this.registryServerSync = registryServerSync; this.registryServerSync = registryServerSync;
} }
public DubboCustomerShutDownHock(ProviderService providerService) { public DubboCustomerShutDownHook(ProviderService providerService) {
this.providerService = providerService; this.providerService = providerService;
} }
...@@ -56,7 +56,7 @@ public class DubboCustomerShutDownHock extends AbstractStop implements Observer ...@@ -56,7 +56,7 @@ public class DubboCustomerShutDownHock extends AbstractStop implements Observer
public ExecutorDetail stop() { public ExecutorDetail stop() {
ExecutorDetail detail = new ExecutorDetail(); ExecutorDetail detail = new ExecutorDetail();
detail.setBeginTime(DateUtil.getDateTime()); detail.setBeginTime(DateUtil.getDateTime());
detail.setServiceName("dubboDownHock1038"); detail.setServiceName("dubboDownHock1045");
List<String> str = new ArrayList<String>(); List<String> str = new ArrayList<String>();
detail.setDetail(str); detail.setDetail(str);
detail.setCode(0); detail.setCode(0);
...@@ -73,10 +73,10 @@ public class DubboCustomerShutDownHock extends AbstractStop implements Observer ...@@ -73,10 +73,10 @@ public class DubboCustomerShutDownHock extends AbstractStop implements Observer
while (iterator.hasNext()) { while (iterator.hasNext()) {
Registry r = iterator.next(); Registry r = iterator.next();
zookperRegster = (ZookeeperRegistry) r; zookperRegster = (ZookeeperRegistry) r;
str.add(" DubboCustomerShutDownHock zookperRegstered-------------->"+zookperRegster.getRegistered().toString()+StringUtil.line()); str.add(" DubboCustomerShutDownHook zookperRegstered-------------->"+zookperRegster.getRegistered().toString()+StringUtil.line());
if (zookperRegster.getRegistered() != null && zookperRegster.getRegistered().size() > 0) { if (zookperRegster.getRegistered() != null && zookperRegster.getRegistered().size() > 0) {
urls = zookperRegster.getRegistered(); urls = zookperRegster.getRegistered();
str.add(" DubboCustomerShutDownHock zookperRegstered urls-------------->"+((urls!=null&&urls.size()>0)?JSON.toJSONString(urls):"url si null")+StringUtil.line()); str.add(" DubboCustomerShutDownHook zookperRegstered urls-------------->"+((urls!=null&&urls.size()>0)?JSON.toJSONString(urls):"url si null")+StringUtil.line());
if (urls != null && urls.size() > 0) { if (urls != null && urls.size() > 0) {
checks=new ArrayList<URL>(); checks=new ArrayList<URL>();
for (URL value : urls) { for (URL value : urls) {
...@@ -113,7 +113,7 @@ public class DubboCustomerShutDownHock extends AbstractStop implements Observer ...@@ -113,7 +113,7 @@ public class DubboCustomerShutDownHock extends AbstractStop implements Observer
long startTime=System.currentTimeMillis(); //获取开始时间 long startTime=System.currentTimeMillis(); //获取开始时间
detail = checkNoticed(detail, str, checks, sessionExpireMs);//通过通知进行检查 detail = checkNoticed(detail, str, checks, sessionExpireMs);//通过通知进行检查
long endTime=System.currentTimeMillis(); //获取结束时间 long endTime=System.currentTimeMillis(); //获取结束时间
System.out.println("程序运行时间------------>: "+(endTime-startTime)+"ms"); logger.info("程序运行时间------------>: "+(endTime-startTime)+"ms");
if (checks.size() > 0) { // 处理 没有接到通知的场景 if (checks.size() > 0) { // 处理 没有接到通知的场景
logger.info("NNNNNNNNNNNNNNNNNNNNNNNNOOOOOOOOOOOO!"+JSON.toJSONString(checks)); logger.info("NNNNNNNNNNNNNNNNNNNNNNNNOOOOOOOOOOOO!"+JSON.toJSONString(checks));
......
...@@ -3,7 +3,7 @@ package com.secoo.mall.dubbo.service.impl; ...@@ -3,7 +3,7 @@ package com.secoo.mall.dubbo.service.impl;
import com.secoo.mall.common.util.date.DateUtil; import com.secoo.mall.common.util.date.DateUtil;
import com.secoo.mall.common.util.string.StringUtil; import com.secoo.mall.common.util.string.StringUtil;
import com.secoo.mall.common.core.bean.gracefulshowtdownBean.ExecutorDetail; import com.secoo.mall.common.core.bean.gracefulshowtdownBean.ExecutorDetail;
import com.secoo.mall.dubbo.service.AbstractStop; import com.secoo.mall.dubbo.service.AbstractShutDown;
import org.apache.catalina.connector.Connector; import org.apache.catalina.connector.Connector;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -12,7 +12,7 @@ import java.util.concurrent.ThreadPoolExecutor; ...@@ -12,7 +12,7 @@ import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
//springboot 容器关闭 //springboot 容器关闭
public class TomcatShutDownHock extends AbstractStop { public class TomcatShutDownHook extends AbstractShutDown {
private volatile Connector connector; private volatile Connector connector;
public Connector getConnector() { public Connector getConnector() {
......
...@@ -7,10 +7,10 @@ import com.secoo.mall.dubbo.monitor.dubbo.service.ProviderService; ...@@ -7,10 +7,10 @@ import com.secoo.mall.dubbo.monitor.dubbo.service.ProviderService;
import com.secoo.mall.dubbo.monitor.dubbo.service.RegistryServerSync; import com.secoo.mall.dubbo.monitor.dubbo.service.RegistryServerSync;
import com.secoo.mall.dubbo.monitor.dubbo.service.impl.ProviderServiceImpl; import com.secoo.mall.dubbo.monitor.dubbo.service.impl.ProviderServiceImpl;
import com.secoo.mall.dubbo.service.GracefulShutDown; import com.secoo.mall.dubbo.service.GracefulShutDown;
import com.secoo.mall.dubbo.service.TomcatGracefulShutDownEntrance; import com.secoo.mall.dubbo.service.ShutDownDataReport;
import com.secoo.mall.dubbo.service.impl.DubboCustomerShutDownHock; import com.secoo.mall.dubbo.service.TomcatGracefulShutDown;
import com.secoo.mall.dubbo.service.DubboUpdata; import com.secoo.mall.dubbo.service.impl.DubboCustomerShutDownHook;
import com.secoo.mall.dubbo.service.impl.TomcatShutDownHock; import com.secoo.mall.dubbo.service.impl.TomcatShutDownHook;
import org.apache.dubbo.common.URL; import org.apache.dubbo.common.URL;
import org.apache.dubbo.registry.Registry; import org.apache.dubbo.registry.Registry;
...@@ -28,8 +28,8 @@ public class MatrixGracefulShutDownAutoConfiguration { ...@@ -28,8 +28,8 @@ public class MatrixGracefulShutDownAutoConfiguration {
@Bean @Bean
@ConditionalOnClass(RegistryServerSync.class) @ConditionalOnClass(RegistryServerSync.class)
@ConditionalOnExpression("!T(org.springframework.util.StringUtils).isEmpty('${dubbo.registry.address:}')") @ConditionalOnExpression("!T(org.springframework.util.StringUtils).isEmpty('${dubbo.registry.address:}')")
ProviderService createProviderService(Registry registry, RegistryServerSync sync) { ProviderService createProviderService( RegistryServerSync sync) {
return new ProviderServiceImpl(registry,sync); return new ProviderServiceImpl(sync);
} }
@Bean @Bean
...@@ -37,7 +37,7 @@ public class MatrixGracefulShutDownAutoConfiguration { ...@@ -37,7 +37,7 @@ public class MatrixGracefulShutDownAutoConfiguration {
@ConditionalOnClass(ConfigCenter.class) @ConditionalOnClass(ConfigCenter.class)
@DependsOn("configCenter") @DependsOn("configCenter")
UpDatas dubboUpdata( ConfigCenter configCenter){ UpDatas dubboUpdata( ConfigCenter configCenter){
DubboUpdata transport= new DubboUpdata(); ShutDownDataReport transport= new ShutDownDataReport();
URL url =configCenter.formUrl(configCenter.registryAddress, configCenter.registryGroup, configCenter.username, configCenter.password); URL url =configCenter.formUrl(configCenter.registryAddress, configCenter.registryGroup, configCenter.username, configCenter.password);
transport.setUrl(url); transport.setUrl(url);
if(StringUtil.isEmpty(configCenter.patch)){ if(StringUtil.isEmpty(configCenter.patch)){
...@@ -51,8 +51,8 @@ public class MatrixGracefulShutDownAutoConfiguration { ...@@ -51,8 +51,8 @@ public class MatrixGracefulShutDownAutoConfiguration {
@Bean @Bean
@ConditionalOnClass({ProviderService.class,RegistryServerSync.class}) @ConditionalOnClass({ProviderService.class,RegistryServerSync.class})
@ConditionalOnExpression("!T(org.springframework.util.StringUtils).isEmpty('${dubbo.registry.address:}')") @ConditionalOnExpression("!T(org.springframework.util.StringUtils).isEmpty('${dubbo.registry.address:}')")
DubboCustomerShutDownHock createDubboConsumerDownHock(ProviderService providerService, RegistryServerSync registryServerSync) { DubboCustomerShutDownHook createDubboConsumerDownHock(ProviderService providerService, RegistryServerSync registryServerSync) {
DubboCustomerShutDownHock hock= new DubboCustomerShutDownHock(providerService); DubboCustomerShutDownHook hock= new DubboCustomerShutDownHook(providerService);
registryServerSync.addObserver(hock); registryServerSync.addObserver(hock);
hock.setRegistryServerSync(registryServerSync); hock.setRegistryServerSync(registryServerSync);
return hock; return hock;
...@@ -77,21 +77,16 @@ public class MatrixGracefulShutDownAutoConfiguration { ...@@ -77,21 +77,16 @@ public class MatrixGracefulShutDownAutoConfiguration {
@Bean @Bean
@ConditionalOnClass(TomcatConnectorCustomizer.class) @ConditionalOnClass(TomcatConnectorCustomizer.class)
TomcatShutDownHock createServletConnectShoutDownHock() { TomcatShutDownHook createServletConnectShoutDownHock() {
return new TomcatShutDownHock(); return new TomcatShutDownHook();
} }
@Bean @Bean
@ConditionalOnClass(TomcatShutDownHock.class) @ConditionalOnClass(TomcatShutDownHook.class)
public TomcatGracefulShutDownEntrance createSpringbootTomcatInit(TomcatShutDownHock tomcatShutDownHock){ public TomcatGracefulShutDown createSpringbootTomcatInit(TomcatShutDownHook tomcatShutDownHook){
return new TomcatGracefulShutDownEntrance(tomcatShutDownHock); return new TomcatGracefulShutDown(tomcatShutDownHook);
} }
// @Bean
// public GracefulShutDown createGraceObject(TomcatShutDownHock servletConnectShoutDownHock, ProviderService providerService, UpDatas dubboUpdata) {
// return new GracefulShutDown( servletConnectShoutDownHock, providerService, dubboUpdata);
// }
@Bean @Bean
@ConditionalOnExpression("!T(org.springframework.util.StringUtils).isEmpty('${dubbo.registry.address:}')") @ConditionalOnExpression("!T(org.springframework.util.StringUtils).isEmpty('${dubbo.registry.address:}')")
public GracefulShutDown createGraceObject(UpDatas dubboUpdata) { public GracefulShutDown createGraceObject(UpDatas dubboUpdata) {
......
package com.secoo.mall.dubbo.spring.boot.autoconfigure;
import com.secoo.mall.common.core.service.StopService;
import com.secoo.mall.common.util.log.LoggerUtil;
import com.secoo.mall.common.util.string.StringUtil;
import com.secoo.mall.dubbo.service.ServletConnectShoutDownHock;
import org.apache.catalina.connector.Connector;
import org.apache.dubbo.config.DubboShutdownHook;
import org.apache.dubbo.config.spring.extension.SpringExtensionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.ContextClosedEvent;
import java.text.SimpleDateFormat;
import java.util.*;
import static org.springframework.context.support.AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME;
public class GracefullyShoutDown implements CommandLineRunner, ApplicationListener<ContextClosedEvent>, TomcatConnectorCustomizer, ApplicationContextAware {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private ApplicationContext context;
private static final int TIMEOUT = 10;
@Autowired
ServletConnectShoutDownHock servletConnectShoutDownHock;
//容器初始化后执行
@Override
public void run(String... args) throws Exception {
if(DubboShutdownHook.getDubboShutdownHook()!=null) {
//hock卸载
DubboShutdownHook.getDubboShutdownHook().unregister();
//listener 卸载
ApplicationEventMulticaster multicaster= context.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
multicaster.removeApplicationListener(SpringExtensionFactory.SHUTDOWN_HOOK_LISTENER);
}
}
//容器关闭后执行
@Override
public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
LoggerUtil.info("=============time:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date() ));
try {
StringBuilder up=new StringBuilder();
up.append("begin time:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date())+StringUtil.line());
//排序
Map<String, StopService> map = contextClosedEvent.getApplicationContext().getBeansOfType(StopService.class);
if(map!=null&&map.size()>0) {
Set<StopService> ts = new TreeSet<StopService>();
for (StopService service : map.values()) {
ts.add(service);
}
//按定义顺序执行
Iterator<StopService> it=ts.iterator();
while(it.hasNext()){
StopService service= (StopService)it.next();
up.append("name:"+service.toString()+",excute begin time:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date() )+ StringUtil.line());
StringBuilder result= (StringBuilder) service.stop();
up.append("name:"+service.toString()+",excute end time:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date())+StringUtil.line()+",result:"+result);
}
//上报数据
}
} catch (Exception e) {
LoggerUtil.error("matrix.GracefullyShoutDown.error", e);
}
}
@Override
public void customize(Connector connector) {
servletConnectShoutDownHock.setConnector(connector);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context=applicationContext;
}
}
package com.secoo.mall.dubbo.spring.boot.autoconfigure;
import com.alibaba.fastjson.JSON;
import com.secoo.mall.common.util.date.DateUtil;
import com.secoo.mall.dubbo.monitor.dubbo.service.ProviderService;
import org.apache.catalina.connector.Connector;
import org.apache.dubbo.config.DubboShutdownHook;
import org.apache.tomcat.util.threads.ThreadPoolExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
public class GracefullyShoutDownYuanShi implements CommandLineRunner, ApplicationListener<ContextClosedEvent>, TomcatConnectorCustomizer {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private ApplicationContext context;
private static final int TIMEOUT = 10;
private volatile Connector connector;
@Autowired
ProviderService providerService;
//容器初始化后执行
@Override
public void run(String... args) throws Exception {
logger.info("nimam2-------------->");
if(DubboShutdownHook.getDubboShutdownHook()!=null) {
DubboShutdownHook.getDubboShutdownHook().unregister();
//TODO 遍历listern 然后移除,一种方法更改dubbo源码,第二种方法,反射搞定。
// ApplicationEventMulticaster multicaster= context.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
// multicaster.removeApplicationListener(SpringExtensionFactory.SHUTDOWN_HOOK_LISTENER);
logger.info("dubbo unreister success");
}else{
logger.info("dubbo unreister obj is null");
}
logger.info(JSON.toJSONString("query--------->"+providerService.getServiceDTOSByQuery("ip", "172.17.76.196", "")));
}
//容器关闭后执行
@Override
public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
logger.info("zidingyi zhixing application close version2=============time:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date() ));
try {
this.connector.pause();
//TODO 挪到定义的实现类里面
Executor executor = this.connector.getProtocolHandler().getExecutor();
if (executor instanceof ThreadPoolExecutor) {
try {
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
threadPoolExecutor.shutdown();
logger.info("apache connector executor shutdown=============time:" + DateUtil.getDateTime());
if (!threadPoolExecutor.awaitTermination(TIMEOUT, TimeUnit.SECONDS)) {
logger.info("Tomcat thread pool did not shut down gracefully within "
+ TIMEOUT + " seconds. Proceeding with forceful shutdown");
threadPoolExecutor.shutdownNow();
if (!threadPoolExecutor.awaitTermination(TIMEOUT, TimeUnit.SECONDS)) {
logger.info("Tomcat thread pool did not terminate");
}
}
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
logger.info("DubboShutdownHook begin time------------>" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date() ));
DubboShutdownHook.getDubboShutdownHook().doDestroy();
logger.info("DubboShutdownHook begin end------------>" +new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date() ));
} catch (Exception e) {
logger.error("matrix.GracefullyShoutDown.error", e);
}
}
@Override
public void customize(Connector connector) {
this.connector = connector;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment