1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

sync code in edgware

This commit is contained in:
fangjian0423 2019-05-28 11:36:18 +08:00
parent 9aaa0f91fd
commit 2dc4061c58
9 changed files with 172 additions and 54 deletions

View File

@ -16,7 +16,7 @@
<description>Spring Cloud Alibaba Dependencies</description> <description>Spring Cloud Alibaba Dependencies</description>
<properties> <properties>
<sentinel.version>1.6.0</sentinel.version> <sentinel.version>1.6.1</sentinel.version>
<oss.version>3.1.0</oss.version> <oss.version>3.1.0</oss.version>
<nacos.version>1.0.0</nacos.version> <nacos.version>1.0.0</nacos.version>
<fescar.version>0.4.2</fescar.version> <fescar.version>0.4.2</fescar.version>

View File

@ -40,7 +40,12 @@ public class NacosConfigEndpoint extends AbstractEndpoint<Map<String, Object>> {
private final NacosRefreshHistory refreshHistory; private final NacosRefreshHistory refreshHistory;
private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private ThreadLocal<DateFormat> dateFormat = new ThreadLocal<DateFormat>(){
@Override
protected DateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
};
public NacosConfigEndpoint(NacosConfigProperties properties, public NacosConfigEndpoint(NacosConfigProperties properties,
NacosRefreshHistory refreshHistory) { NacosRefreshHistory refreshHistory) {
@ -60,7 +65,7 @@ public class NacosConfigEndpoint extends AbstractEndpoint<Map<String, Object>> {
for (NacosPropertySource ps : all) { for (NacosPropertySource ps : all) {
Map<String, Object> source = new HashMap<>(16); Map<String, Object> source = new HashMap<>(16);
source.put("dataId", ps.getDataId()); source.put("dataId", ps.getDataId());
source.put("lastSynced", dateFormat.format(ps.getTimestamp())); source.put("lastSynced", dateFormat.get().format(ps.getTimestamp()));
sources.add(source); sources.add(source);
} }
result.put("Sources", sources); result.put("Sources", sources);

View File

@ -27,10 +27,15 @@ public class NacosRefreshHistory {
private LinkedList<Record> records = new LinkedList<>(); private LinkedList<Record> records = new LinkedList<>();
private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private ThreadLocal<DateFormat> dateFormat = new ThreadLocal<DateFormat>(){
@Override
protected DateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
};
public void add(String dataId, String md5) { public void add(String dataId, String md5) {
records.addFirst(new Record(dateFormat.format(new Date()), dataId, md5)); records.addFirst(new Record(dateFormat.get().format(new Date()), dataId, md5));
if (records.size() > MAX_SIZE) { if (records.size() > MAX_SIZE) {
records.removeLast(); records.removeLast();
} }

View File

@ -16,17 +16,11 @@
package org.springframework.cloud.alibaba.nacos; package org.springframework.cloud.alibaba.nacos;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.alibaba.nacos.discovery.NacosDiscoveryClientAutoConfiguration;
import org.springframework.cloud.alibaba.nacos.registry.NacosAutoServiceRegistration; import org.springframework.cloud.alibaba.nacos.registry.NacosAutoServiceRegistration;
import org.springframework.cloud.alibaba.nacos.registry.NacosRegistration; import org.springframework.cloud.alibaba.nacos.registry.NacosRegistration;
import org.springframework.cloud.alibaba.nacos.registry.NacosServiceRegistry; import org.springframework.cloud.alibaba.nacos.registry.NacosServiceRegistry;

View File

@ -0,0 +1,33 @@
/*
* Copyright (C) 2018 the original author or authors.
*
* Licensed 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
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.alibaba.sentinel.zuul;
/**
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
public interface ConfigConstants {
String APP_TYPE_ZUUL_GATEWAY = "12";
String ZUUl_PREFIX = "spring.cloud.sentinel.zuul";
String GATEWAY_PREFIX = "spring.cloud.sentinel.scg";
String FALLBACK_MSG_RESPONSE = "response";
String FALLBACK_REDIRECT = "redirect";
}

View File

@ -23,19 +23,20 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.alibaba.sentinel.zuul.handler.FallBackProviderHandler; import org.springframework.cloud.alibaba.sentinel.zuul.handler.FallBackProviderHandler;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import com.alibaba.csp.sentinel.adapter.gateway.zuul.callback.RequestOriginParser; import com.alibaba.csp.sentinel.adapter.gateway.zuul.callback.RequestOriginParser;
import com.alibaba.csp.sentinel.adapter.gateway.zuul.callback.ZuulGatewayCallbackManager; import com.alibaba.csp.sentinel.adapter.gateway.zuul.callback.ZuulGatewayCallbackManager;
import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulErrorFilter; import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulErrorFilter;
import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulPostFilter; import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulPostFilter;
import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulPreFilter; import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulPreFilter;
import com.alibaba.csp.sentinel.config.SentinelConfig;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.http.ZuulServlet; import com.netflix.zuul.http.ZuulServlet;
/** /**
@ -45,16 +46,15 @@ import com.netflix.zuul.http.ZuulServlet;
*/ */
@Configuration @Configuration
@ConditionalOnClass(ZuulServlet.class) @ConditionalOnClass(ZuulServlet.class)
@ConditionalOnProperty(prefix = SentinelZuulAutoConfiguration.PREFIX, name = "enabled", havingValue = "true", matchIfMissing = true) @ConditionalOnProperty(prefix = ConfigConstants.ZUUl_PREFIX, name = "enabled", havingValue = "true", matchIfMissing = true)
@EnableConfigurationProperties(SentinelZuulProperties.class)
public class SentinelZuulAutoConfiguration { public class SentinelZuulAutoConfiguration {
private static final Logger logger = LoggerFactory private static final Logger logger = LoggerFactory
.getLogger(SentinelZuulAutoConfiguration.class); .getLogger(SentinelZuulAutoConfiguration.class);
public static final String PREFIX = "spring.cloud.sentinel.zuul";
@Autowired @Autowired
private Environment environment; private SentinelZuulProperties zuulProperties;
@Autowired(required = false) @Autowired(required = false)
private RequestOriginParser requestOriginParser; private RequestOriginParser requestOriginParser;
@ -64,48 +64,32 @@ public class SentinelZuulAutoConfiguration {
if (requestOriginParser != null) { if (requestOriginParser != null) {
ZuulGatewayCallbackManager.setOriginParser(requestOriginParser); ZuulGatewayCallbackManager.setOriginParser(requestOriginParser);
} }
System.setProperty(SentinelConfig.APP_TYPE,
String.valueOf(ConfigConstants.APP_TYPE_ZUUL_GATEWAY));
} }
@Bean @Bean
public ZuulFilter sentinelZuulPreFilter() { @ConditionalOnMissingBean
String preOrderStr = environment.getProperty(PREFIX + "." + "order.pre"); public SentinelZuulPreFilter sentinelZuulPreFilter() {
int order = 10000; logger.info("[Sentinel Zuul] register SentinelZuulPreFilter {}",
try { zuulProperties.getOrder().getPre());
order = Integer.parseInt(preOrderStr); return new SentinelZuulPreFilter(zuulProperties.getOrder().getPre());
}
catch (NumberFormatException e) {
// ignore
}
logger.info("[Sentinel Zuul] register SentinelZuulPreFilter {}", order);
return new SentinelZuulPreFilter(order);
} }
@Bean @Bean
public ZuulFilter sentinelZuulPostFilter() { @ConditionalOnMissingBean
String postOrderStr = environment.getProperty(PREFIX + "." + "order.post"); public SentinelZuulPostFilter sentinelZuulPostFilter() {
int order = 1000; logger.info("[Sentinel Zuul] register SentinelZuulPostFilter {}",
try { zuulProperties.getOrder().getPost());
order = Integer.parseInt(postOrderStr); return new SentinelZuulPostFilter(zuulProperties.getOrder().getPost());
}
catch (NumberFormatException e) {
// ignore
}
logger.info("[Sentinel Zuul] register SentinelZuulPostFilter {}", order);
return new SentinelZuulPostFilter(order);
} }
@Bean @Bean
public ZuulFilter sentinelZuulErrorFilter() { @ConditionalOnMissingBean
String errorOrderStr = environment.getProperty(PREFIX + "." + "order.error"); public SentinelZuulErrorFilter sentinelZuulErrorFilter() {
int order = -1; logger.info("[Sentinel Zuul] register SentinelZuulErrorFilter {}",
try { zuulProperties.getOrder().getError());
order = Integer.parseInt(errorOrderStr); return new SentinelZuulErrorFilter(zuulProperties.getOrder().getError());
}
catch (NumberFormatException e) {
// ignore
}
logger.info("[Sentinel Zuul] register SentinelZuulErrorFilter {}", order);
return new SentinelZuulErrorFilter(order);
} }
@Bean @Bean

View File

@ -0,0 +1,87 @@
/*
* Copyright (C) 2018 the original author or authors.
*
* Licensed 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
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.alibaba.sentinel.zuul;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import com.alibaba.csp.sentinel.adapter.gateway.zuul.constants.ZuulConstant;
import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulErrorFilter;
import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulPostFilter;
import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulPreFilter;
/**
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
@ConfigurationProperties(prefix = ConfigConstants.ZUUl_PREFIX)
public class SentinelZuulProperties {
@NestedConfigurationProperty
private SentinelZuulProperties.Order order;
public Order getOrder() {
return order;
}
public SentinelZuulProperties setOrder(Order order) {
this.order = order;
return this;
}
public static class Order {
/**
* The order of {@link SentinelZuulPreFilter}.
*/
private int pre = 10000;
/**
* The order of {@link SentinelZuulPostFilter}.
*/
private int post = ZuulConstant.SEND_RESPONSE_FILTER_ORDER;
/**
* The order of {@link SentinelZuulErrorFilter}.
*/
private int error = -1;
public int getPre() {
return pre;
}
public void setPre(int pre) {
this.pre = pre;
}
public int getPost() {
return post;
}
public void setPost(int post) {
this.post = post;
}
public int getError() {
return error;
}
public void setError(int error) {
this.error = error;
}
}
}

View File

@ -42,7 +42,12 @@ public class AcmEndpoint extends AbstractEndpoint<Map<String, Object>> {
private final AcmPropertySourceRepository propertySourceRepository; private final AcmPropertySourceRepository propertySourceRepository;
private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private ThreadLocal<DateFormat> dateFormat = new ThreadLocal<DateFormat>(){
@Override
protected DateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
};
public AcmEndpoint(AcmProperties properties, AcmRefreshHistory refreshHistory, public AcmEndpoint(AcmProperties properties, AcmRefreshHistory refreshHistory,
AcmPropertySourceRepository propertySourceRepository) { AcmPropertySourceRepository propertySourceRepository) {
@ -64,7 +69,7 @@ public class AcmEndpoint extends AbstractEndpoint<Map<String, Object>> {
for (AcmPropertySource ps : all) { for (AcmPropertySource ps : all) {
Map<String, Object> source = new HashMap<>(); Map<String, Object> source = new HashMap<>();
source.put("dataId", ps.getDataId()); source.put("dataId", ps.getDataId());
source.put("lastSynced", dateFormat.format(ps.getTimestamp())); source.put("lastSynced", dateFormat.get().format(ps.getTimestamp()));
sources.add(source); sources.add(source);
} }
runtime.put("sources", sources); runtime.put("sources", sources);

View File

@ -30,10 +30,15 @@ public class AcmRefreshHistory {
private LinkedList<Record> records = new LinkedList<>(); private LinkedList<Record> records = new LinkedList<>();
private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private ThreadLocal<DateFormat> dateFormat = new ThreadLocal<DateFormat>(){
@Override
protected DateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
};
public void add(String dataId, String md5) { public void add(String dataId, String md5) {
records.addFirst(new Record(dateFormat.format(new Date()), dataId, md5)); records.addFirst(new Record(dateFormat.get().format(new Date()), dataId, md5));
if (records.size() > MAX_SIZE) { if (records.size() > MAX_SIZE) {
records.removeLast(); records.removeLast();
} }