diff --git a/pom.xml b/pom.xml index 6f6fd32..ce14ad3 100644 --- a/pom.xml +++ b/pom.xml @@ -37,6 +37,7 @@ 2.13.3 4.1.53.Final 4.5.13 + 0.2.7 @@ -148,6 +149,17 @@ 2.2.5.RELEASE + + com.alibaba.boot + nacos-config-spring-boot-starter + ${nacos.version} + + + com.alibaba.boot + nacos-discovery-spring-boot-starter + ${nacos.version} + + org.apache.commons commons-pool2 diff --git a/src/main/java/we/FizzGatewayApplication.java b/src/main/java/we/FizzGatewayApplication.java index 319b5ed..25d46ae 100644 --- a/src/main/java/we/FizzGatewayApplication.java +++ b/src/main/java/we/FizzGatewayApplication.java @@ -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 { diff --git a/src/main/java/we/config/ApolloConfig.java b/src/main/java/we/config/ApolloConfig.java new file mode 100644 index 0000000..f41e8a6 --- /dev/null +++ b/src/main/java/we/config/ApolloConfig.java @@ -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 { +} diff --git a/src/main/java/we/config/AppConfigProperties.java b/src/main/java/we/config/AppConfigProperties.java index 5e19b1d..268131c 100644 --- a/src/main/java/we/config/AppConfigProperties.java +++ b/src/main/java/we/config/AppConfigProperties.java @@ -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; diff --git a/src/main/java/we/config/SystemConfig.java b/src/main/java/we/config/SystemConfig.java index 0d8bab6..4911ff4 100644 --- a/src/main/java/we/config/SystemConfig.java +++ b/src/main/java/we/config/SystemConfig.java @@ -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 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); + } } diff --git a/src/main/java/we/controller/ManagerConfigController.java b/src/main/java/we/controller/ManagerConfigController.java index 92fa2f3..eb2929b 100644 --- a/src/main/java/we/controller/ManagerConfigController.java +++ b/src/main/java/we/controller/ManagerConfigController.java @@ -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; diff --git a/src/main/java/we/filter/PreFilter.java b/src/main/java/we/filter/PreFilter.java index 1e2241e..7d7dcc1 100644 --- a/src/main/java/we/filter/PreFilter.java +++ b/src/main/java/we/filter/PreFilter.java @@ -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 bServices = new HashSet<>(); diff --git a/src/main/java/we/fizz/ConfigLoader.java b/src/main/java/we/fizz/ConfigLoader.java index e8a661c..81254a9 100644 --- a/src/main/java/we/fizz/ConfigLoader.java +++ b/src/main/java/we/fizz/ConfigLoader.java @@ -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; diff --git a/src/main/java/we/fizz/input/ScriptHelper.java b/src/main/java/we/fizz/input/ScriptHelper.java index a6644d9..eee3aa8 100644 --- a/src/main/java/we/fizz/input/ScriptHelper.java +++ b/src/main/java/we/fizz/input/ScriptHelper.java @@ -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; } diff --git a/src/main/java/we/plugin/auth/ApiConfigService.java b/src/main/java/we/plugin/auth/ApiConfigService.java index 84751f4..31e6b8d 100644 --- a/src/main/java/we/plugin/auth/ApiConfigService.java +++ b/src/main/java/we/plugin/auth/ApiConfigService.java @@ -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; diff --git a/src/main/java/we/plugin/stat/StatPluginFilter.java b/src/main/java/we/plugin/stat/StatPluginFilter.java index a314537..8eb25d6 100644 --- a/src/main/java/we/plugin/stat/StatPluginFilter.java +++ b/src/main/java/we/plugin/stat/StatPluginFilter.java @@ -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; diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index eaa6ac7..365f21e 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -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