1.pom add the Nacos dependencies;

2.use Nacos to refresh the config;
3.refactor Apollo listener for compatible with Nacos;
4.refactor application.yml file and add related config.
This commit is contained in:
zhongjie
2020-11-06 17:35:55 +08:00
committed by dxfeng10
parent b0257d956c
commit af357c00f7
12 changed files with 172 additions and 33 deletions

12
pom.xml
View File

@@ -37,6 +37,7 @@
<log4j2.version>2.13.3</log4j2.version>
<netty.version>4.1.53.Final</netty.version>
<httpclient.version>4.5.13</httpclient.version>
<nacos.version>0.2.7</nacos.version>
</properties>
<dependencies>
@@ -148,6 +149,17 @@
<version>2.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>${nacos.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-discovery-spring-boot-starter</artifactId>
<version>${nacos.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>

View File

@@ -1,6 +1,6 @@
package we;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
@@ -12,7 +12,7 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
exclude = {ErrorWebFluxAutoConfiguration.class, RedisAutoConfiguration.class, RedisReactiveAutoConfiguration.class},
scanBasePackages = {"we"}
)
@EnableApolloConfig
@NacosPropertySource(dataId = "application", groupId = "fizz-gateway", autoRefreshed = true)
@EnableDiscoveryClient
public class FizzGatewayApplication {

View File

@@ -0,0 +1,15 @@
package we.config;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
/**
* Apollo config
* @author zhongjie
*/
@ConditionalOnProperty(name = "apollo.enabled")
@EnableApolloConfig
@Configuration
public class ApolloConfig {
}

View File

@@ -17,6 +17,7 @@
package we.config;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
@@ -27,6 +28,7 @@ import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfigProperties {
@NacosValue(value = "${spring.profiles.active}", autoRefreshed = true)
@Value("${spring.profiles.active}")
private String env;

View File

@@ -17,10 +17,12 @@
package we.config;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import org.springframework.util.ObjectUtils;
import we.plugin.auth.GatewayGroup;
import we.util.Constants;
import we.util.JacksonUtils;
@@ -61,6 +63,7 @@ public class SystemConfig {
//
// private Set<Character> currentServerGatewayGroupSet;
@NacosValue(value = "${spring.profiles.active}")
@Value("${spring.profiles.active}")
private String profile;
@@ -140,11 +143,9 @@ public class SystemConfig {
String nv = c.getNewValue();
log.info(p + " old: " + ov + ", new: " + nv);
if (p.equals("log.response-body")) {
logResponseBody = Boolean.valueOf(nv);
afterLogResponseBodySet();
this.updateLogResponseBody(Boolean.parseBoolean(nv));
} else if (p.equals("log.headers")) {
logHeaders = nv;
afterLogHeadersSet();
this.updateLogHeaders(nv);
} /*else if (p.equals("gateway-group")) {
gatewayGroup = nv;
afterGatewayGroupSet();
@@ -152,4 +153,32 @@ public class SystemConfig {
}
);
}
private void updateLogResponseBody(boolean newValue) {
logResponseBody = newValue;
this.afterLogResponseBodySet();
}
private void updateLogHeaders(String newValue) {
logHeaders = newValue;
afterLogHeadersSet();
}
@NacosValue(value = "${log.response-body:false}", autoRefreshed = true)
public void setLogResponseBody(boolean logResponseBody) {
if (this.logResponseBody == logResponseBody) {
return;
}
log.info("log.response-body old: " + this.logResponseBody + ", new: " + logResponseBody);
this.updateLogResponseBody(logResponseBody);
}
@NacosValue(value = "${log.headers:x}", autoRefreshed = true)
public void setLogHeaders(String logHeaders) {
if (ObjectUtils.nullSafeEquals(this.logHeaders, logHeaders)) {
return;
}
log.info("log.headers old: " + this.logHeaders + ", new: " + logHeaders);
this.updateLogHeaders(logHeaders);
}
}

View File

@@ -17,6 +17,7 @@
package we.controller;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
@@ -44,6 +45,7 @@ import java.util.stream.Collectors;
@RestController
@RequestMapping(value = "/managerConfig")
public class ManagerConfigController {
@NacosValue(value = "${fizz.manager.config.key:fizz-manager-key}", autoRefreshed = true)
@Value("${fizz.manager.config.key:fizz-manager-key}")
private String key;

View File

@@ -17,6 +17,7 @@
package we.filter;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -56,9 +57,11 @@ public class PreFilter extends ProxyAggrFilter {
private static final FilterResult succFr = FilterResult.SUCCESS(PRE_FILTER);
@NacosValue(value = "${spring.profiles.active}")
@Value("${spring.profiles.active}")
private String profile;
@NacosValue(value = "${b-services:x}")
@Value("${b-services:x}")
private Set<String> bServices = new HashSet<>();

View File

@@ -20,6 +20,7 @@ package we.fizz;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import we.config.AppConfigProperties;
import we.fizz.input.ClientInputConfig;
import we.fizz.input.Input;
@@ -69,6 +70,7 @@ public class ConfigLoader {
@Resource(name = AGGREGATE_REACTIVE_REDIS_TEMPLATE)
private ReactiveStringRedisTemplate reactiveStringRedisTemplate;
@NacosValue(value = "${fizz.aggregate.read-local-config-flag:false}", autoRefreshed = true)
@Value("${fizz.aggregate.read-local-config-flag:false}")
private Boolean readLocalConfigFlag;

View File

@@ -29,7 +29,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import org.springframework.util.StringUtils;
import we.constants.CommonConstants;
import we.exception.StopAndResponseException;
@@ -57,7 +57,7 @@ public class ScriptHelper {
Script script = new Script();
script.setType((String) scriptCfg.get("type"));
script.setSource((String) scriptCfg.get("source"));
if (StringUtils.isBlank(script.getType()) || StringUtils.isBlank(script.getSource())) {
if (!StringUtils.hasText(script.getType()) || !StringUtils.hasText(script.getSource())) {
return null;
}

View File

@@ -17,6 +17,7 @@
package we.plugin.auth;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
@@ -30,6 +31,7 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@@ -75,13 +77,26 @@ public class ApiConfigService {
k -> {
ConfigChange cc = cce.getChange(k);
if (cc.getPropertyName().equalsIgnoreCase("serviceWhiteList")) {
log.info("old service white list: " + cc.getOldValue());
serviceWhiteList = cc.getNewValue();
afterServiceWhiteListSet();
this.updateServiceWhiteList(cc.getOldValue(), cc.getNewValue());
}
}
);
}
private void updateServiceWhiteList(String oldValue, String newValue) {
if (ObjectUtils.nullSafeEquals(oldValue, newValue)) {
return;
}
log.info("old service white list: " + oldValue);
serviceWhiteList = newValue;
afterServiceWhiteListSet();
}
@NacosValue(value = "${serviceWhiteList:x}", autoRefreshed = true)
public void setServiceWhiteList(String serviceWhiteList) {
this.updateServiceWhiteList(this.serviceWhiteList, serviceWhiteList);
}
public void afterServiceWhiteListSet() {
if (StringUtils.isNotBlank(serviceWhiteList)) {
whiteListSet.clear();
@@ -94,6 +109,7 @@ public class ApiConfigService {
}
}
@NacosValue(value = "${auth.compatible-wh:false}", autoRefreshed = true)
@Value("${auth.compatible-wh:false}")
private boolean compatibleWh;
@@ -109,6 +125,7 @@ public class ApiConfigService {
@Autowired(required = false)
private CustomAuth customAuth;
@NacosValue(value = "${openServiceWhiteList:false}", autoRefreshed = true)
@Value("${openServiceWhiteList:false}")
private boolean openServiceWhiteList = false;

View File

@@ -17,6 +17,7 @@
package we.plugin.stat;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -63,12 +64,15 @@ public class StatPluginFilter extends PluginFilter {
private static final String reqTime = "\"reqTime\":";
@NacosValue(value = "${stat.open:false}", autoRefreshed = true)
@Value("${stat.open:false}")
private boolean statOpen = false;
@NacosValue(value = "${stat.channel:fizz_access_stat}", autoRefreshed = true)
@Value("${stat.channel:fizz_access_stat}")
private String fizzAccessStatChannel;
@NacosValue(value = "${stat.topic:}", autoRefreshed = true)
@Value("${stat.topic:}")
private String fizzAccessStatTopic;

View File

@@ -1,27 +1,80 @@
#apollo:
# bootstrap:
# enabled: true
# namespaces: application
# eagerLoad:
# enabled: true
# if you do not use Apollo, ignore the follow config
######################### Apollo config start #####################################
apollo:
# if use Apollo set this flag to true
enabled: false
bootstrap:
# if use Apollo set this flag to true
enabled: false
namespaces: application
eagerLoad:
# if use Apollo set this flag to true
enabled: false
######################### Apollo config end #######################################
spring.profiles.active: dev
# if you do not use Nacos, ignore the follow config
######################### Nacos config start ######################################
nacos:
config:
# if use Nacos config set this flag to true
enabled: false
# need replace
server-addr: localhost:8848
auto-refresh: true
group: fizz-gateway
data-id: application
type: PROPERTIES
# need replace
namespace: c37ecd3b-e8b3-42cc-ba94-98e931e33683
discovery:
# if use Nacos discovery set this flag to true
enabled: false
# need replace
server-addr: localhost:8848
auto-register: true
######################### Nacos config end #######################################
server.port: 8600
# if you do not use Eureka, ignore the follow config
######################### Eureka config start ####################################
eureka:
client:
# if use Eureka set this flag to true
enabled: false
serviceUrl:
# need replace
defaultZone: http://localhost:6600/eureka/
instance:
prefer-ip-address: true
######################### Eureka config end ######################################
eureka.instance.prefer-ip-address: true
eureka.client.serviceUrl.defaultZone: http://localhost:6600/eureka/
spring.application.name: fizz-gateway
spring.cloud.loadbalancer.ribbon.enabled: false
server:
port: 8600
spring:
profiles:
active: dev
application:
name: fizz-gateway
cloud:
loadbalancer:
ribbon:
enabled: false
aggregate.redis.host: localhost
aggregate.redis.port: 6379
aggregate.redis.password: 123456
aggregate.redis.database: 10
aggregate:
redis:
# need replace
host: localhost
# need replace
port: 6379
# need replace
password: 123456
# need replace
database: 9
proxy-webclient:
name: proxy
aggr-webclient:
name: aggr
fizz-web-client:
timeout: 20000
log:
headers: COOKIE,FIZZ-APPID,FIZZ-SECRETKEY,FIZZ-SIGN,FIZZ-TS,FIZZ-RSV
proxy-webclient.name: proxy
aggr-webclient.name: aggr
fizz-web-client.timeout: 20000
log.headers: COOKIE,FIZZ-APPID,FIZZ-SECRETKEY,FIZZ-SIGN,FIZZ-TS,FIZZ-RSV