Skip to content

Commit

Permalink
Merge branch '2018-09-30-case-show'
Browse files Browse the repository at this point in the history
  • Loading branch information
qiujiayu committed Sep 30, 2018
2 parents 33568d3 + e196f9f commit 1d1f5a6
Show file tree
Hide file tree
Showing 14 changed files with 410 additions and 161 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
spring:
application:
name: config-demo
name: config-keeper-demo

profiles:
active: ${profile:dev}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.suixingpay.config.client;

import com.suixingpay.config.client.dao.ConfigDAO;
import com.suixingpay.config.client.dao.ConfigDAOImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

Expand All @@ -17,25 +14,12 @@
* @review: qiujiayu[[email protected]]/2017年9月21日 下午3:33:11
*/
@Configuration
@ConditionalOnBean(ConfigDAO.class)
@ConditionalOnClass(Endpoint.class)
public class SxfConfigClientAutoConfiguration {

@Autowired
private SxfConfigClientProperties configClientProperties;

@Bean
@ConditionalOnMissingBean(ConfigDAO.class)
public ConfigDAO sxfConfigDAO() {
return new ConfigDAOImpl(configClientProperties);
}

@Configuration
@ConditionalOnBean(ConfigDAO.class)
@ConditionalOnClass(Endpoint.class)
protected static class SxfConfigVersionEndpointConfiguration {

@Bean
public SxfConfigLocalVersionEndpoint sxfConfigLocalVersionEndpoint(ConfigDAO configDAO) {
return new SxfConfigLocalVersionEndpoint(configDAO);
}
public SxfConfigLocalVersionEndpoint sxfConfigLocalVersionEndpoint(ConfigDAO configDAO) {
return new SxfConfigLocalVersionEndpoint(configDAO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public ConfigDAO sxfConfigDAO(SxfConfigClientProperties sxfConfigClientPropertie
return new ConfigDAOImpl(sxfConfigClientProperties);
}


@Bean
@ConditionalOnMissingBean(SxfConfigServicePropertySourceLocator.class)
@ConditionalOnProperty(value = "suixingpay.config.enabled", matchIfMissing = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.suixingpay.config.client;

import com.suixingpay.config.client.dao.ConfigDAO;
import com.suixingpay.config.common.to.PropertySource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.MapPropertySource;

import com.suixingpay.config.client.dao.ConfigDAO;
import com.suixingpay.config.common.to.PropertySource;

import lombok.extern.slf4j.Slf4j;

/**
* @author: qiujiayu[[email protected]]
* @date: 2017年9月1日 下午5:08:52
Expand All @@ -29,7 +27,7 @@ public class SxfConfigServicePropertySourceLocator implements PropertySourceLoca
private SxfConfigClientProperties configClientProperties;

public SxfConfigServicePropertySourceLocator(ConfigDAO configDAO,
SxfConfigClientProperties configClientProperties) {
SxfConfigClientProperties configClientProperties) {
this.configDAO = configDAO;
this.configClientProperties = configClientProperties;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,35 @@ public interface ConfigDAO {

/**
* 获取版本配置中心配置最新版本信息
*
*
* @return
*/
VersionDTO getVersion();

/**
* 获取全局配置
*
*
* @return
*/
PropertySource getGlobalConfig();

/**
* 从本地缓存中获取数据
*
*
* @return
*/
PropertySource getGlobalConfigLocalCache();

/**
* 获取应用配置
*
*
* @return
*/
PropertySource getApplicationConfig();

/**
* 从本地缓存中获取
*
*
* @return
*/
PropertySource getApplicationConfigLocalCache();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
package com.suixingpay.config.client.dao;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import com.suixingpay.config.client.AddApplicationInstanceInfoInterceptor;
import com.suixingpay.config.client.SxfConfigClientProperties;
import com.suixingpay.config.client.util.JsonUtil;
import com.suixingpay.config.common.to.PropertySource;
import com.suixingpay.config.common.to.VersionDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.client.support.BasicAuthorizationInterceptor;
import org.springframework.web.client.RestTemplate;

import com.suixingpay.config.client.SxfConfigClientProperties;
import com.suixingpay.config.client.util.JsonUtil;
import com.suixingpay.config.common.to.PropertySource;
import com.suixingpay.config.common.to.VersionDTO;

import lombok.extern.slf4j.Slf4j;
import java.io.*;
import java.util.List;

/**
* @author: qiujiayu[[email protected]]
Expand Down Expand Up @@ -49,9 +42,10 @@ public class ConfigDAOImpl implements ConfigDAO {

private String applicationConfigCacheFile;

private PropertySource globalConfigCache;
// refresh 后 ConfigDAOImpl 在Spring 容器中不是单例,需要使用static
private static PropertySource globalConfigCache;

private PropertySource applicationConfigCache;
private static PropertySource applicationConfigCache;

private RestTemplate restTemplate;

Expand Down Expand Up @@ -198,7 +192,7 @@ public PropertySource getApplicationConfig() {
String json = responseEntity.getBody();
propertySource = JsonUtil.jsonToObject(json, PropertySource.class);
if (null != propertySource) {
this.applicationConfigCache = propertySource;
applicationConfigCache = propertySource;
writeFile(this.applicationConfigCacheFile, json);
}
}
Expand All @@ -213,12 +207,12 @@ public PropertySource getApplicationConfig() {

@Override
public PropertySource getApplicationConfigLocalCache() {
if (null != this.applicationConfigCache) {
return this.applicationConfigCache;
if (null != applicationConfigCache) {
return applicationConfigCache;
}
String json = readFile(applicationConfigCacheFile);
if (null != json && !json.trim().isEmpty()) {
this.applicationConfigCache = JsonUtil.jsonToObject(json, PropertySource.class);
applicationConfigCache = JsonUtil.jsonToObject(json, PropertySource.class);
}
return applicationConfigCache;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/**
* 连接配置中心失败
*
*
* @author: qiujiayu[[email protected]]
* @date: 2017年9月25日 上午11:24:48
* @version: V1.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/**
* bootstrap.yml未设置spring.application.name
*
*
* @author: qiujiayu[[email protected]]
* @date: 2017年9月25日 下午1:04:12
* @version: V1.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/**
* 未设置profile异常
*
*
* @author: qiujiayu[[email protected]]
* @date: 2017年9月25日 上午11:43:18
* @version: V1.0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.suixingpay.config.client.failureanalyzer;

import com.suixingpay.config.client.exception.ConfigConnectionFailureException;
import org.springframework.boot.diagnostics.AbstractFailureAnalyzer;
import org.springframework.boot.diagnostics.FailureAnalysis;

import com.suixingpay.config.client.exception.ConfigConnectionFailureException;

/**
* @author: qiujiayu[[email protected]]
* @date: 2017年9月25日 上午11:26:36
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.suixingpay.config.client.failureanalyzer;

import com.suixingpay.config.client.exception.UnSetApplicationNameException;
import org.springframework.boot.diagnostics.AbstractFailureAnalyzer;
import org.springframework.boot.diagnostics.FailureAnalysis;

import com.suixingpay.config.client.exception.UnSetApplicationNameException;

/**
* 未设置spring.application.name异常分析
*
*
* @author: qiujiayu[[email protected]]
* @date: 2017年9月25日 上午11:45:32
* @version: V1.0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.suixingpay.config.client.failureanalyzer;

import com.suixingpay.config.client.exception.UnSetProfileException;
import org.springframework.boot.diagnostics.AbstractFailureAnalyzer;
import org.springframework.boot.diagnostics.FailureAnalysis;

import com.suixingpay.config.client.exception.UnSetProfileException;

/**
* 未设置profile异常分析
*
*
* @author: qiujiayu[[email protected]]
* @date: 2017年9月25日 上午11:45:32
* @version: V1.0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.suixingpay.config.client.util;

import java.io.IOException;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Map;

/**
* @author: qiujiayu[[email protected]]
Expand All @@ -25,7 +24,7 @@ public class JsonUtil {

/**
* 生成JSON字符串
*
*
* @param obj
* @return
* @throws JsonProcessingException
Expand All @@ -43,7 +42,7 @@ public static String objectToJson(Object obj) {

/**
* 生成JSON字符串
*
*
* @param obj
* @param serializationView
* @return
Expand All @@ -60,9 +59,9 @@ public static String objectToJson(Object obj, Class<?> serializationView) throws

/**
* 将JSON字符串转化为 指定类型
*
*
* @param json json
* @param c 期望类型
* @param c 期望类型
* @return T 结果
*/
public static <T> T jsonToObject(String json, Class<T> c) {
Expand All @@ -88,9 +87,9 @@ public static <T> Map<String, T> jsonToMap(String json) {

/**
* 将JSON字符串转化为 指定类型
*
*
* @param json json
* @param c 期望类型
* @param c 期望类型
* @return T 结果
*/
public static <T> T jsonToObject(String json, TypeReference<T> c) {
Expand All @@ -107,7 +106,7 @@ public static <T> T jsonToObject(String json, TypeReference<T> c) {

/**
* 获取Json树
*
*
* @param json
* @return
*/
Expand Down
Loading

0 comments on commit 1d1f5a6

Please sign in to comment.