Commit bb7008d4 by 房斌

1 gracefull单独抽出来模块2 解决了ip 和application api 3未来以前dubbo 相关的协议干掉

parent 3b33c210
package com.secoo.mall.common.core.service;
public abstract class AbstractStop implements StopService<StringBuilder> ,Comparable {
//定义执行顺序
public abstract Integer getHandleTypeOrder();
@Override
public int compareTo(Object o) {
AbstractStop stop=(AbstractStop) o;
int result = (this.getHandleTypeOrder()>stop.getHandleTypeOrder()) ? 1 : -1;//升序
return result ;
}
}
package com.secoo.mall.common.core.service;
public interface StopService <BizResponse>{
public interface StopService <T>{
public T stop();
public BizResponse stop();
}
......@@ -77,6 +77,11 @@ public class StringUtil extends StringUtils {
}
public static String line(){
String lineSeparator = System.getProperty("line.separator", "\n");
return lineSeparator;
}
public static void main(String[] args) {
String str = "testJdate_order";
System.out.println(toCamelCase(str));
......
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>matrix</artifactId>
<groupId>com.secoo.mall</groupId>
<version>2.0.9-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>matrix-gracefulshutdown</artifactId>
<properties>
<secoo-dubbo.version>2.7.4.1-secoo1.5-SNAPSHOT</secoo-dubbo.version>
<dubbo-starter.version>2.7.4.1</dubbo-starter.version>
<dubbo-zookper.version> 2.7.4.1</dubbo-zookper.version>
</properties>
<dependencies>
<dependency>
<groupId>com.secoo.mall</groupId>
<artifactId>matrix-protocol-core</artifactId>
</dependency>
<dependency>
<groupId>com.secoo.mall</groupId>
<artifactId>common-util</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${secoo-dubbo.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo-starter.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--zookeeper-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo-zookper.version}</version>
<type>pom</type>
<exclusions>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
/*
* 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.config;
import com.secoo.mall.dubbo.monitor.dubbo.Constants.Constants;
import com.secoo.mall.dubbo.monitor.dubbo.exception.ConfigurationException;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.registry.Registry;
import org.apache.dubbo.registry.RegistryFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ConfigCenter {
private static final Logger logger = LoggerFactory.getLogger(ConfigCenter.class);
//centers in dubbo 2.7
@Value("${admin.config-center:}")
private String configCenter;
@Value("${admin.registry.address:}")
private String registryAddress;
@Value("${admin.metadata-report.address:}")
private String metadataAddress;
@Value("${admin.metadata-report.cluster:false}")
private boolean cluster;
@Value("${admin.registry.group:dubbo}")
private String registryGroup;
@Value("${admin.config-center.group:dubbo}")
private String configCenterGroup;
@Value("${admin.metadata-report.group:dubbo}")
private String metadataGroup;
@Value("${admin.config-center.username:}")
private String username;
@Value("${admin.config-center.password:}")
private String password;
private URL configCenterUrl;
private URL registryUrl;
private URL metadataUrl;
/*
* generate registry client
*/
@Bean
Registry getRegistry() {
Registry registry = null;
if (registryUrl == null) {
if (StringUtils.isBlank(registryAddress)) {
throw new ConfigurationException("Either config center or registry address is needed, please refer to https://github.com/apache/incubator-dubbo-admin/wiki/Dubbo-Admin-configuration");
}
registryUrl = formUrl(registryAddress, registryGroup, username, password);
}
RegistryFactory registryFactory = ExtensionLoader.getExtensionLoader(RegistryFactory.class).getAdaptiveExtension();
registry = registryFactory.getRegistry(registryUrl);
return registry;
}
private URL formUrl(String config, String group, String username, String password) {
URL url = URL.valueOf(config);
if (StringUtils.isNotEmpty(group)) {
url = url.addParameter(Constants.GROUP_KEY, group);
}
if (StringUtils.isNotEmpty(username)) {
url = url.setUsername(username);
}
if (StringUtils.isNotEmpty(password)) {
url = url.setPassword(password);
}
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.Constants;
import java.util.HashSet;
import java.util.Set;
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 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 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 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 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 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 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<>();
static {
CONFIGS.add(WEIGHT);
CONFIGS.add(BALANCING);
}
}
/*
* 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;
public class ConfigurationException extends RuntimeException {
public ConfigurationException(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;
/**
* 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);
}
/*
* 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.Provider;
import com.secoo.mall.dubbo.monitor.dubbo.model.dto.ServiceDTO;
import java.util.List;
import java.util.Set;
/**
* 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.
*
* @param serviceName specific service name, cannot be fuzzy string
* @return list of provider object
*/
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);
}
/*
* 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.Constants.Constants;
import com.secoo.mall.dubbo.monitor.utils.CoderUtil;
import com.secoo.mall.dubbo.monitor.utils.Tool;
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.registry.NotifyListener;
import org.apache.dubbo.registry.Registry;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicLong;
public class RegistryServerSync implements InitializingBean, DisposableBean, NotifyListener, CommandLineRunner {
private static final Logger logger = LoggerFactory.getLogger(RegistryServerSync.class);
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();
/**
* Make sure ID never changed when the same url notified many times
*/
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<>();
@Autowired
private Registry registry;
public ConcurrentMap<String, ConcurrentMap<String, Map<String, URL>>> getRegistryCache() {
return registryCache;
}
@Override
public void afterPropertiesSet() throws Exception {
logger.info("Init Dubbo Admin Sync Cache...");
// FailbackRegistry parentRegister= (FailbackRegistry)registry;
// Set<URL> urls= parentRegister.getRegistered();
// for(URL u: urls){
// if(u.getParameter(Constants.CATEGORY_KEY).endsWith(Constants.PROVIDER_PROTOCOL)) {
// registry.subscribe(u, this);
// }
// }
registry.subscribe(SUBSCRIBE, this);
}
@Override
public void destroy() throws Exception {
registry.unsubscribe(SUBSCRIBE, this);
}
// Notification of of any service with any type (override、subcribe、route、provider) is full.
@Override
public void notify(List<URL> urls) {
if (urls == null || urls.isEmpty()) {
return;
}
// Map<category, Map<servicename, Map<Long, URL>>>
final Map<String, Map<String, Map<String, URL>>> categories = new HashMap<>();
String interfaceName = null;
for (URL url : urls) {
String category = url.getParameter(Constants.CATEGORY_KEY, Constants.PROVIDERS_CATEGORY);
// NOTE: group and version in empty protocol is *
if (Constants.EMPTY_PROTOCOL.equalsIgnoreCase(url.getProtocol())) {
ConcurrentMap<String, Map<String, URL>> services = registryCache.get(category);
if (services != null) {
String group = url.getParameter(Constants.GROUP_KEY);
String version = url.getParameter(Constants.VERSION_KEY);
// NOTE: group and version in empty protocol is *
if (!Constants.ANY_VALUE.equals(group) && !Constants.ANY_VALUE.equals(version)) {
services.remove(url.getServiceKey());
} else {
for (Map.Entry<String, Map<String, URL>> serviceEntry : services.entrySet()) {
String service = serviceEntry.getKey();
if (Tool.getInterface(service).equals(url.getServiceInterface())
&& (Constants.ANY_VALUE.equals(group) || StringUtils.isEquals(group, Tool.getGroup(service)))
&& (Constants.ANY_VALUE.equals(version) || StringUtils.isEquals(version, Tool.getVersion(service)))) {
services.remove(service);//版本 和分组匹配就干掉?
}
}
}
}
} else {
if (StringUtils.isEmpty(interfaceName)) {
interfaceName = url.getServiceInterface();
}
Map<String, Map<String, URL>> services = categories.get(category);
if (services == null) {
services = new HashMap<>();
categories.put(category, services);
}
String service = url.getServiceKey();
Map<String, URL> ids = services.get(service);
if (ids == null) {
ids = new HashMap<>();
services.put(service, ids);
}
// Make sure we use the same ID for the same URL
if (URL_IDS_MAPPER.containsKey(url.toFullString())) {
ids.put(URL_IDS_MAPPER.get(url.toFullString()), url);
} else {
String md5 = CoderUtil.MD5_16bit(url.toFullString());
ids.put(md5, url);
URL_IDS_MAPPER.putIfAbsent(url.toFullString(), md5);
}
}
}
if (categories.size() == 0) {
return;
}
for (Map.Entry<String, Map<String, Map<String, URL>>> categoryEntry : categories.entrySet()) {
String category = categoryEntry.getKey();
ConcurrentMap<String, Map<String, URL>> services = registryCache.get(category);
if (services == null) {
services = new ConcurrentHashMap<String, Map<String, URL>>();
registryCache.put(category, services);
} else {// Fix map can not be cleared when service is unregistered: when a unique “group/service:version” service is unregistered, but we still have the same services with different version or group, so empty protocols can not be invoked.
Set<String> keys = new HashSet<String>(services.keySet());
for (String key : keys) { //接口
if (Tool.getInterface(key).equals(interfaceName) && !categoryEntry.getValue().entrySet().contains(key)) {
services.remove(key);
}
}
}
services.putAll(categoryEntry.getValue());
}
}
/**
* Callback used to run the bean.
*
* @param args incoming main method arguments
* @throws Exception on error
*/
@Override
public void run(String... args) throws Exception {
logger.info("Init Dubbo Admin Sync Cache...");
// FailbackRegistry parentRegister= (FailbackRegistry)registry;
// Set<URL> urls= parentRegister.getRegistered();
// for(URL u: urls){
// if(u.getParameter(Constants.CATEGORY_KEY).endsWith(Constants.PROVIDER_PROTOCOL)) {
// registry.subscribe(u, this);
// }
// }
}
}
/*
* 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.service.RegistryServerSync;
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.concurrent.ConcurrentMap;
public class AbstractService {
protected static final Logger logger = LoggerFactory.getLogger(AbstractService.class);
@Autowired
protected Registry registry;
@Autowired
private RegistryServerSync sync;
public ConcurrentMap<String, ConcurrentMap<String, Map<String, URL>>> getRegistryCache() {
return sync.getRegistryCache();
}
}
/*
* 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.utils.SyncUtils;
import org.apache.dubbo.common.URL;
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 {
@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 org.apache.dubbo.common.io.Bytes;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class CoderUtil {
private static final Logger logger = LoggerFactory.getLogger(CoderUtil.class);
private static final char[] hexCode = "0123456789ABCDEF".toCharArray();
private static MessageDigest md;
static {
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
logger.error(e.getMessage(), e);
}
}
public static String MD5_16bit(String input) {
String hash = MD5_32bit(input);
if (hash == null) {
return null;
}
return hash.substring(8, 24);
}
public static String MD5_32bit(String input) {
if (input == null || input.length() == 0) {
return null;
}
md.update(input.getBytes());
byte[] digest = md.digest();
String hash = convertToString(digest);
return hash;
}
public static String MD5_32bit(byte[] input) {
if (input == null || input.length == 0) {
return null;
}
md.update(input);
byte[] digest = md.digest();
String hash = convertToString(digest);
return hash;
}
private static String convertToString(byte[] data) {
StringBuilder r = new StringBuilder(data.length * 2);
for (byte b : data) {
r.append(hexCode[(b >> 4) & 0xF]);
r.append(hexCode[(b & 0xF)]);
}
return r.toString();
}
public static String decodeBase64(String source) {
return new String(Bytes.base642bytes(source));
}
}
/*
* 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;
}
}
/*
* 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;
/**
* Tool
*/
public class Tool {
public static String getInterface(String service) {
if (service != null && service.length() > 0) {
int i = service.indexOf('/');
if (i >= 0) {
service = service.substring(i + 1);
}
i = service.lastIndexOf(':');
if (i >= 0) {
service = service.substring(0, i);
}
}
return service;
}
public static String getGroup(String service) {
if (service != null && service.length() > 0) {
int i = service.indexOf('/');
if (i >= 0) {
return service.substring(0, i);
}
}
return null;
}
public static String getVersion(String service) {
if (service != null && service.length() > 0) {
int i = service.lastIndexOf(':');
if (i >= 0) {
return service.substring(i + 1);
}
}
return null;
}
}
package com.secoo.mall.dubbo.service;
import com.secoo.mall.common.core.service.AbstractStop;
import com.secoo.mall.common.util.log.LoggerUtil;
import com.secoo.mall.common.util.string.StringUtil;
import com.secoo.mall.dubbo.monitor.dubbo.model.domain.Provider;
import com.secoo.mall.dubbo.monitor.dubbo.service.ProviderService;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.config.DubboShutdownHook;
import org.apache.dubbo.registry.support.AbstractRegistryFactory;
import org.apache.dubbo.rpc.Protocol;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Iterator;
import java.util.List;
public class DubboCustomerDownHock extends AbstractStop {
@Autowired
private final ProviderService providerService;
public DubboCustomerDownHock() {
providerService = null;
}
@Override
public StringBuilder stop() {
StringBuilder builderStr = new StringBuilder();
String lineSeparator = System.getProperty("line.separator", "\n");
try {
//step1 register销毁
AbstractRegistryFactory.destroyAll();
//TODO step2 写在阿波罗里,具体数据 以后根据客户端上报的数据配置,目前无详细依据。等待
Thread.sleep(10000);
String ip = "";
ip = NetUtils.getIpByHost(NetUtils.getLocalAddress().getHostName());
String name = ApplicationModel.getApplication();
builderStr.append("dubbo ip result---------->" + ip+" application:"+name+StringUtil.line());
List<Provider> serviceDTOS = providerService.getServiceDTOSByQuery("ip", ip, "");
if (StringUtil.isNoneEmpty(name) && serviceDTOS != null && serviceDTOS.size() > 0) {
serviceDTOS = providerService.findProviderUrlByGivenApplication(name, serviceDTOS);
}
//step3 check检查
Iterator<Provider> iteratorCheck = serviceDTOS.iterator();
while (iteratorCheck.hasNext()) {
Provider obj = iteratorCheck.next();
if(StringUtil.isNotEmpty(obj.getAddress())&&StringUtil.isNotEmpty(name)){
List<String> check = providerService.findServicesByAddressAndName(obj.getAddress(),name);
if (check != null && check.size() > 0) {
builderStr.append("warn:matrix-monitor off DubboService check service failure" + obj.getUrl() + " already on line after off " + StringUtil.line());
}
}
}
//step4 协议层销毁
destoryProtocol();
} catch (Exception e) {
DubboShutdownHook.getDubboShutdownHook().doDestroy();
builderStr.append(" error :matirx-monitor offDubbo " + e.toString() + lineSeparator);
}
return builderStr;
}
public void destoryProtocol() {
ExtensionLoader<Protocol> loader = ExtensionLoader.getExtensionLoader(Protocol.class);
for (String protocolName : loader.getLoadedExtensions()) {
try {
Protocol protocol = loader.getLoadedExtension(protocolName);
if (protocol != null) {
protocol.destroy();
}
} catch (Throwable t) {
LoggerUtil.info(t.getMessage());
}
}
}
@Override
public Integer getHandleTypeOrder() {
return 2;
}
}
package com.secoo.mall.dubbo.service;
import com.secoo.mall.common.core.service.AbstractStop;
import com.secoo.mall.common.util.date.DateUtil;
import com.secoo.mall.common.util.log.LoggerUtil;
import com.secoo.mall.common.util.string.StringUtil;
import org.apache.catalina.connector.Connector;
import org.springframework.stereotype.Component;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
//springboot 容器关闭
@Component
public class ServletConnectShoutDownHock extends AbstractStop {
private volatile Connector connector;
public Connector getConnector() {
return connector;
}
public void setConnector(Connector connector) {
this.connector = connector;
}
@Override
public StringBuilder stop() {
StringBuilder sb = new StringBuilder();
this.connector.pause();
Executor executor = this.connector.getProtocolHandler().getExecutor();
if (executor instanceof ThreadPoolExecutor) {
try {
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
threadPoolExecutor.shutdown();
sb.append("apache connector executor shutdown=============time:" + DateUtil.getDateTime()+ StringUtil.line());
//TODO 超时时间
if (!threadPoolExecutor.awaitTermination(10, TimeUnit.SECONDS)) {
sb.append("warn:Tomcat thread pool did not shut down gracefully within "
+ "10" + " seconds. Proceeding with forceful shutdown" + StringUtil.line());
threadPoolExecutor.shutdownNow();
if (!threadPoolExecutor.awaitTermination(10, TimeUnit.SECONDS)) {
sb.append("warn:Tomcat thread pool did not terminate");
sb.append("Tomcat thread pool did not terminate"+StringUtil.line());
}
}
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
sb.append("error:"+ex.getMessage()+StringUtil.line());
}
}
return sb;
}
@Override
public Integer getHandleTypeOrder() {
return 1;
}
}
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.monitor.dubbo.model.domain.Provider;
import com.secoo.mall.dubbo.monitor.dubbo.service.ProviderService;
import com.secoo.mall.dubbo.service.ServletConnectShoutDownHock;
import org.apache.catalina.connector.Connector;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.config.DubboShutdownHook;
import org.apache.dubbo.config.spring.extension.SpringExtensionFactory;
import org.apache.dubbo.rpc.model.ApplicationModel;
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 static final int TIMEOUT = 10;
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
ServletConnectShoutDownHock servletConnectShoutDownHock;
private ApplicationContext context;
@Autowired
private final ProviderService providerService = null;
//容器初始化后执行
@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);
}
LoggerUtil.info("==========================result:"+up.toString());
//上报数据
}
} 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.secoo.mall.dubbo.monitor.dubbo.service.ProviderService;
import com.secoo.mall.dubbo.monitor.dubbo.service.RegistryServerSync;
import com.secoo.mall.dubbo.monitor.dubbo.service.impl.ProviderServiceImpl;
import com.secoo.mall.dubbo.service.DubboCustomerDownHock;
import com.secoo.mall.dubbo.service.ServletConnectShoutDownHock;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableDubbo
public class MatrixGracefulShutDownAutoConfiguration {
@Bean
public GracefullyShoutDown createGraceObject() {
return new GracefullyShoutDown();
}
@Bean
public RegistryServerSync createSynObject() {
return new RegistryServerSync();
}
@Bean
ProviderService createProviderService() {
return new ProviderServiceImpl();
}
@Bean
ServletConnectShoutDownHock createServletConnectShoutDownHock() {
return new ServletConnectShoutDownHock();
}
@Bean
DubboCustomerDownHock createDubboConsumerDownHock() {
return new DubboCustomerDownHock();
}
}
\ No newline at end of file
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.secoo.mall.dubbo.spring.boot.autoconfigure.MatrixGracefulShutDownAutoConfiguration,\
com.secoo.mall.dubbo.monitor.config.ConfigCenter
\ No newline at end of file
......@@ -49,6 +49,14 @@
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
......
......@@ -70,9 +70,7 @@ public class ConfigCenter {
private static final Logger logger = LoggerFactory.getLogger(ConfigCenter.class);
private URL configCenterUrl;
private URL registryUrl;
private URL metadataUrl;
......
......@@ -18,11 +18,15 @@ package com.secoo.mall.dubbo.monitor.dubbo.service;
import com.secoo.mall.dubbo.monitor.dubbo.domain.Provider;
import com.secoo.mall.dubbo.monitor.dubbo.model.dto.ServiceDTO;
import com.secoo.mall.dubbo.monitor.dubbo.model.domain.Provider;
import org.apache.dubbo.common.URL;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;
/**
* ProviderService
......@@ -119,4 +123,6 @@ public interface ProviderService {
List<Provider> getServiceDTOSByQuery(String pattern, String filter, String env);
public List<Provider> findProviderUrlByGivenApplication(String application, List<Provider> providers);
}
......@@ -17,6 +17,7 @@
package com.secoo.mall.dubbo.monitor.dubbo.service.impl;
import com.secoo.mall.common.util.string.StringUtil;
import com.secoo.mall.dubbo.monitor.dubbo.Constants.Constants;
import com.secoo.mall.dubbo.monitor.dubbo.exception.ParamValidationException;
import com.secoo.mall.dubbo.monitor.dubbo.model.domain.Provider;
......@@ -27,8 +28,6 @@ import com.secoo.mall.dubbo.monitor.utils.ParseUtils;
import com.secoo.mall.dubbo.monitor.utils.SyncUtils;
import com.secoo.mall.dubbo.monitor.utils.Tool;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.metadata.identifier.MetadataIdentifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.*;
......@@ -52,7 +51,6 @@ public class ProviderServiceImpl extends AbstractService implements ProviderServ
//删除服务
@Override
public void deleteStaticProvider(String id) {
......@@ -63,6 +61,7 @@ public class ProviderServiceImpl extends AbstractService implements ProviderServ
registry.unregister(oldProvider);
}
//更新服务
@Override
public void updateProvider(Provider provider) {
......@@ -327,6 +326,21 @@ public class ProviderServiceImpl extends AbstractService implements ProviderServ
}
@Override
public List<Provider> findProviderUrlByGivenApplication(String application,List<Provider> providers) {
List<Provider> reg=null;
if(providers!=null&&providers.size()>0&& StringUtil.isNoneEmpty(application)){
reg=new ArrayList<Provider>();
for(int i=0;i<providers.size();i++){
Provider p= providers.get(i);
if(p.getApplication().equals(application)){
reg.add(p);
}
}
}
return reg;
}
@Override
public List<String> findServicesByApplication(String application) {
List<String> ret = new ArrayList<String>();
......
package com.secoo.mall.dubbo.service;
import com.secoo.mall.common.core.service.StopService;
import com.secoo.mall.common.core.service.AbstractStop;
import com.secoo.mall.common.util.log.LoggerUtil;
import com.secoo.mall.common.util.string.StringUtil;
import com.secoo.mall.dubbo.monitor.dubbo.domain.Provider;
import com.secoo.mall.dubbo.monitor.dubbo.service.ProviderService;
import com.secoo.mall.dubbo.monitor.utils.OSUtil;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.registry.support.AbstractRegistryFactory;
import org.apache.dubbo.rpc.Protocol;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.springframework.beans.factory.annotation.Autowired;
import com.secoo.mall.dubbo.monitor.dubbo.model.domain.Provider;
import org.springframework.beans.factory.annotation.Value;
import java.util.Iterator;
import java.util.List;
public class DubboCustomerDownHock implements StopService<StringBuilder> {
public class DubboCustomerDownHock extends AbstractStop {
@Autowired
private final ProviderService providerService;
......@@ -35,21 +40,28 @@ public class DubboCustomerDownHock implements StopService<StringBuilder> {
//TODO step2 写在阿波罗里,具体数据 以后根据客户端上报的数据配置,目前无详细依据。等待
Thread.sleep(10000);
String ip = "";
String name = "";
final List<Provider> serviceDTOS = providerService.getServiceDTOSByQuery("ip", ip, "");
String ip ="";
ip= NetUtils.getIpByHost(NetUtils.getLocalAddress().getHostName());
LoggerUtil.info("dubbo ip result---------->"+ip);
String name= ApplicationModel.getApplication();
LoggerUtil.info("application name:--------------->"+name);
List<Provider> serviceDTOS = providerService.getServiceDTOSByQuery("ip", ip, "");
if(StringUtil.isNoneEmpty(name)&&serviceDTOS!=null&&serviceDTOS.size()>0){
serviceDTOS=providerService.findProviderUrlByGivenApplication(name,serviceDTOS);
}
//step3 check检查
Iterator<Provider> iteratorCheck = serviceDTOS.iterator();
while (iteratorCheck.hasNext()) {
Provider obj = iteratorCheck.next();
if (StringUtil.isNoneEmpty(obj.getAddress()) && obj.getApplication().equals(name)) { //本应用的服务
Provider obj = iteratorCheck.next();
List<String> check = providerService.findServicesByAddress(obj.getAddress());
if (check != null && check.size() > 0) {
builderStr.append("matrix-monitor off DubboService check service:" + obj.getUrl() + " already on line after off " + lineSeparator);
}
}
}
//step4 协议层销毁
destoryProtocol();
} catch (Exception e) {
......@@ -71,4 +83,12 @@ public class DubboCustomerDownHock implements StopService<StringBuilder> {
}
}
}
@Override
public Integer getHandleTypeOrder() {
return 2;
}
}
package com.secoo.mall.dubbo.service;
import com.secoo.mall.common.core.service.AbstractStop;
import com.secoo.mall.common.util.date.DateUtil;
import com.secoo.mall.common.util.log.LoggerUtil;
import org.apache.catalina.connector.Connector;
import org.springframework.stereotype.Component;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
//springboot 容器关闭
@Component
public class ServletConnectShoutDownHock extends AbstractStop {
private volatile Connector connector;
public Connector getConnector() {
return connector;
}
public void setConnector(Connector connector) {
this.connector = connector;
}
@Override
public StringBuilder stop() {
StringBuilder sb = new StringBuilder();
this.connector.pause();
Executor executor = this.connector.getProtocolHandler().getExecutor();
if (executor instanceof ThreadPoolExecutor) {
try {
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
threadPoolExecutor.shutdown();
LoggerUtil.info("apache connector executor shutdown=============time:" + DateUtil.getDateTime());
//TODO 超时时间
if (!threadPoolExecutor.awaitTermination(10, TimeUnit.SECONDS)) {
LoggerUtil.info("Tomcat thread pool did not shut down gracefully within "
+ "10" + " seconds. Proceeding with forceful shutdown");
threadPoolExecutor.shutdownNow();
if (!threadPoolExecutor.awaitTermination(10, TimeUnit.SECONDS)) {
LoggerUtil.info("Tomcat thread pool did not terminate");
sb.append("Tomcat thread pool did not terminate");
}
}
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
sb.append(ex.getMessage());
}
}
return sb;
}
@Override
public Integer getHandleTypeOrder() {
return 1;
}
}
......@@ -11,7 +11,6 @@
<artifactId>matrix-pro2.7.4.1tocol-dubbo-starter</artifactId>
<dependencies>
<dependency>
<groupId>com.secoo.mall</groupId>
......@@ -31,5 +30,23 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
\ No newline at end of file
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 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.tomcat.util.threads.ThreadPoolExecutor;
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.Date;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.*;
public class GracefullyShoutDown implements CommandLineRunner, ApplicationListener<ContextClosedEvent>, TomcatConnectorCustomizer {
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;
private volatile Connector connector;
@Autowired
ProviderService providerService;
ServletConnectShoutDownHock servletConnectShoutDownHock;
//容器初始化后执行
@Override
public void run(String... args) throws Exception {
logger.info("nimam2-------------->");
if(DubboShutdownHook.getDubboShutdownHook()!=null) {
//hock卸载
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");
//listener 卸载
ApplicationEventMulticaster multicaster= context.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
multicaster.removeApplicationListener(SpringExtensionFactory.SHUTDOWN_HOOK_LISTENER);
}
logger.info(JSON.toJSONString("query--------->"+providerService.getServiceDTOSByQuery("ip", "172.17.76.196", "")));
}
......@@ -53,47 +51,45 @@ public class GracefullyShoutDown implements CommandLineRunner, ApplicationListen
@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() ));
LoggerUtil.info("=============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();
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);
}
}
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);
LoggerUtil.error("matrix.GracefullyShoutDown.error", e);
}
}
@Override
public void customize(Connector connector) {
this.connector = connector;
servletConnectShoutDownHock.setConnector(connector);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context=applicationContext;
}
}
......@@ -78,7 +78,7 @@ public class GracefullyShoutDownYuanShi implements CommandLineRunner, Applicatio
logger.info("DubboShutdownHook begin time------------>" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date() ));
//DubboShutdownHook.getDubboShutdownHook().doDestroy();
DubboShutdownHook.getDubboShutdownHook().doDestroy();
......
......@@ -7,6 +7,8 @@ import com.secoo.mall.dubbo.monitor.config.ConfigCenter;
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.impl.ProviderServiceImpl;
import com.secoo.mall.dubbo.service.DubboCustomerDownHock;
import com.secoo.mall.dubbo.service.ServletConnectShoutDownHock;
import com.secoo.mall.dubbo.swagger.annotations.EnableDubboSwagger;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.beans.factory.annotation.Value;
......@@ -63,6 +65,17 @@ public class MatrixDubboAutoConfiguration {
return new ProviderServiceImpl();
}
@Bean
ServletConnectShoutDownHock createServletConnectShoutDownHock(){
return new ServletConnectShoutDownHock();
}
@Bean
DubboCustomerDownHock createDubboConsumerDownHock(){
return new DubboCustomerDownHock();
}
......
......@@ -21,7 +21,7 @@
<properties>
<dubbo-starter.version>2.7.4.1</dubbo-starter.version>
<dubbo.version>2.7.4.1</dubbo.version>
<secoo-dubbo.version>2.7.4.1-secoo1.4</secoo-dubbo.version>
<secoo-dubbo.version>2.7.4.1-secoo1.5-SNAPSHOT</secoo-dubbo.version>
</properties>
<dependencyManagement>
......
......@@ -24,6 +24,7 @@
<module>matrix-bus</module>
<module>matrix-datahelper</module>
<module>matrix-client</module>
<module>matrix-gracefulshutdown</module>
</modules>
......
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