Skip to content

Commit

Permalink
env key
Browse files Browse the repository at this point in the history
  • Loading branch information
dzdx committed Dec 9, 2021
1 parent d02246e commit 536cfcc
Show file tree
Hide file tree
Showing 20 changed files with 75 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ image_push:
app_run:
docker run -e REGISTRY_APP_NAME=integration -d --net=host --name=sofa-registry --rm -v /Users/dzdx/Desktop/registry-all/conf/application.properties:/registry-distribution/registry-all/conf/application.properties dzdx/sofa-registry-test:latest
mysql_run:
docker run --rm -e MARIADB_ROOT_PASSWORD=root -p 3306:3306 --name=mysql -v /Users/dzdx/Desktop/registry-all:/registry-distribution/registry-all -d --net=host mariadb:10.7
docker run --rm -e MARIADB_ROOT_PASSWORD=root -p 3306:3306 --name=mysql -v /Users/dzdx/Desktop/registry-all:/registry-distribution/registry-all -d mariadb:10.7
2 changes: 1 addition & 1 deletion docker/kube/integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ data:
nodes.localRegion=DEFAULT_ZONE
jdbc.url=jdbc:mysql://mysql.default.svc.cluster.local:3306/registrymetadb
jdbc.username=root
jdbc.password=
jdbc.password=root
---
apiVersion: v1
kind: Secret
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@ private SystemUtils() {}
public static int getSystemInteger(String name, int def) {
String v = System.getProperty(name);
if (v == null) {
v = System.getenv(name);
v = System.getenv(convertEnvKey(name));
}
return v == null ? def : Integer.parseInt(v);
}

public static long getSystemLong(String name, long def) {
String v = System.getProperty(name);
if (v == null) {
v = System.getenv(name);
v = System.getenv(convertEnvKey(name));
}
return v == null ? def : Long.parseLong(v);
}

public static String getSystem(String name, String def) {
String v = System.getProperty(name);
if (v == null) {
v = System.getenv(name);
v = System.getenv(convertEnvKey(name));
}
return v == null ? def : v;
}
Expand All @@ -49,7 +49,7 @@ public static String getSystem(String name) {
return getSystem(name, null);
}

public static String convertEnvKey(String key) {
private static String convertEnvKey(String key) {
return StringUtils.replace(key, ".", "_").toUpperCase();
}
}
12 changes: 9 additions & 3 deletions server/distribution/all/bin/base/start_base.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ if [[ -z "$REGISTRY_APP_NAME" ]]; then
fi
BASE_DIR=`cd $(dirname $0)/../..; pwd`
APP_JAR="${BASE_DIR}/registry-${REGISTRY_APP_NAME}.jar"
# set user.home
JAVA_OPTS="$JAVA_OPTS -Duser.home=${BASE_DIR}"

if [[ -z "$SPRING_CONFIG_LOCATION" ]]; then
SPRING_CONFIG_LOCATION=${BASE_DIR}/conf/application.properties
SPRING_CONFIG_LOCATION=${BASE_DIR}/conf/
fi
JAVA_OPTS="$JAVA_OPTS -Dspring.config.additional-location=${SPRING_CONFIG_LOCATION}"

if [[ -n "$SPRING_PROFILES_ACTIVE" ]]; then
JAVA_OPTS="$JAVA_OPTS -Dspring.profiles.active=${SPRING_PROFILES_ACTIVE}"
fi

# set user.home
JAVA_OPTS="$JAVA_OPTS -Duser.home=${BASE_DIR} -Dspring.config.location=${SPRING_CONFIG_LOCATION}"

# springboot conf
SPRINGBOOT_OPTS="${SPRINGBOOT_OPTS} -DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector"
Expand Down
1 change: 1 addition & 0 deletions server/distribution/all/bin/integration/start.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash

BASE_DIR=`cd $(dirname $0)/../..; pwd`
export SPRING_PROFILES_ACTIVE="prod"
REGISTRY_APP_NAME=integration exec ${BASE_DIR}/bin/base/start_base.sh
2 changes: 1 addition & 1 deletion server/distribution/all/bin/integration/start_dev.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash

BASE_DIR=`cd $(dirname $0)/../..; pwd`
export SPRING_CONFIG_LOCATION=${BASE_DIR}/conf/application-h2.properties
export SPRING_PROFILES_ACTIVE="dev"
REGISTRY_APP_NAME=integration exec ${BASE_DIR}/bin/base/start_base.sh
3 changes: 2 additions & 1 deletion server/distribution/all/conf/application.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
nodes.localDataCenter=DefaultDataCenter
nodes.localRegion=DEFAULT_ZONE
## connect db
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/registrymetadb
jdbc.username=root
jdbc.password=
jdbc.password=root
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ private void gracefulShutdown() {
}
return true;
});
LOGGER.info("add data self to blacklist successfully");
} catch (Throwable e) {
LOGGER.error("add blacklist failed:", e);
}
Expand Down
8 changes: 5 additions & 3 deletions server/server/data/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
nodes.localDataCenter=DefaultDataCenter
nodes.localRegion=DEFAULT_ZONE
## connect db
jdbc.url=jdbc:mysql://127.0.0.1:2883/registrymetadb?useUnicode=true&characterEncoding=utf8
jdbc.username=username
jdbc.password=password
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/registrymetadb?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root
data.server.gracefulShutdown=true
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ public class RegistryApplication {
private static ConfigurableApplicationContext dataApplicationContext;

public static void main(String[] args) throws Exception {
System.setProperty("spring.profiles.active", "integrate");
if (StringUtils.isBlank(System.getProperty("spring.profiles.active"))) {
System.setProperty("spring.profiles.active", "dev");
}
System.setProperty("registry.lease.duration.secs", "10");
System.setProperty("registry.elector.warm.up.millis", "2000");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ jdbc.url=jdbc:h2:mem:metadatadb;DB_CLOSE_DELAY=-1;MODE=MySQL;MV_STORE=FALSE
jdbc.mapperLocations=classpath:h2-mapper/*.xml
jdbc.username=sa
jdbc.password=
session.server.gracefulShutdown=false
data.server.gracefulShutdown=false

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
nodes.localDataCenter=DefaultDataCenter
#nodes.metaNode=DefaultDataCenter:localhost
nodes.localRegion=DEFAULT_ZONE
## connect db
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/registrymetadb?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root
9 changes: 4 additions & 5 deletions server/server/meta/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ nodes.localDataCenter=DefaultDataCenter
#nodes.metaNode=DefaultDataCenter:localhost
nodes.localRegion=DEFAULT_ZONE
## connect db
#persistence.profile.active=jdbc
#jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:2883/registrymetadb?useUnicode=true&characterEncoding=utf8
jdbc.username=username
jdbc.password=password
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/registrymetadb?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ private void gracefulShutdown() {
metaNodeService.addSelfToMetaBlacklist();
return true;
});
LOGGER.error("add session self to blacklist succeed");
LOGGER.info("add session self to blacklist successfully");
} catch (Throwable e) {
LOGGER.error("add session self to blacklist failed", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ nodes.localDataCenter=DefaultDataCenter
nodes.localRegion=DEFAULT_ZONE
## connect db
#persistence.profile.active=jdbc
#jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:2883/registrymetadb?useUnicode=true&characterEncoding=utf8
jdbc.username=username
jdbc.password=password
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/registrymetadb?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root
session.server.gracefulShutdown=true
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
import com.alipay.sofa.registry.log.LoggerFactory;
import com.alipay.sofa.registry.store.api.meta.RecoverConfigRepository;
import com.alipay.sofa.registry.store.api.spring.SpringContext;
import com.alipay.sofa.registry.util.SystemUtils;
import com.google.common.annotations.VisibleForTesting;
import java.util.Set;
import javax.annotation.PostConstruct;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -46,28 +44,36 @@ public class DefaultCommonConfigBean implements DefaultCommonConfig {
@Value("${persistence.profile.active:jdbc}")
private String persistenceProfileActive;

private String recoverClusterId = SystemUtils.getSystem("nodes.recoverClusterId", "");
@Value("${nodes.recoverClusterId:}")
private String recoverClusterId;

private static final String ALL_KEY = "ALL";

@Autowired private RecoverConfigRepository recoverConfigRepository;

@PostConstruct
public void init() throws InterruptedException {
if (StringUtils.isEmpty(clusterId) || StringUtils.isEmpty(recoverClusterId)) {
throw new InterruptedException("clusterId or recoverClusterId is empty.");
private String getDefaultClusterId() {
if (StringUtils.isNotBlank(clusterId)) {
return clusterId;
}
return dataCenter;
}

private String getDefaultRecoverClusterId() {
if (StringUtils.isNotBlank(recoverClusterId)) {
return recoverClusterId;
}
return getDefaultClusterId();
}

@Override
public String getClusterId(String table) {
Set<String> keys = recoverConfigRepository.queryKey(table);
String recoverClusterId = getDefaultRecoverClusterId();
if (!CollectionUtils.isEmpty(keys) && keys.contains(ALL_KEY)) {
LOG.info("[GetClusterId]propertyTable:{}, clusterId:{}", table, recoverClusterId);
return recoverClusterId;
return getDefaultRecoverClusterId();
}

return clusterId;
return getDefaultClusterId();
}

@Override
Expand All @@ -80,6 +86,7 @@ public String getClusterId(String table, String key) {
}

Set<String> keys = recoverConfigRepository.queryKey(table);
String recoverClusterId = getDefaultRecoverClusterId();
if (!CollectionUtils.isEmpty(keys) && keys.contains(key)) {
LOG.info(
"[GetClusterId]propertyTable:{}, propertyKey:{}, clusterId:{}",
Expand All @@ -89,12 +96,12 @@ public String getClusterId(String table, String key) {
return recoverClusterId;
}

return clusterId;
return getDefaultClusterId();
}

@Override
public boolean isRecoverCluster() {
return !StringUtils.equals(clusterId, recoverClusterId);
return !StringUtils.equals(getDefaultClusterId(), getDefaultRecoverClusterId());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,23 +137,20 @@ public DataSource dataSource(JdbcDriverConfig jdbcDriverConfig) throws Exception
props.put(
PROP_DRIVERCLASSNAME,
SystemUtils.getSystem(
SystemUtils.convertEnvKey(JdbcDriverConfigBean.PRE_FIX + "." + PROP_DRIVERCLASSNAME),
JdbcDriverConfigBean.PRE_FIX + "." + PROP_DRIVERCLASSNAME,
jdbcDriverConfig.getDriverClassName()));
props.put(
PROP_URL,
SystemUtils.getSystem(
SystemUtils.convertEnvKey(JdbcDriverConfigBean.PRE_FIX + "." + PROP_URL),
jdbcDriverConfig.getUrl()));
JdbcDriverConfigBean.PRE_FIX + "." + PROP_URL, jdbcDriverConfig.getUrl()));
props.put(
PROP_USERNAME,
SystemUtils.getSystem(
SystemUtils.convertEnvKey(JdbcDriverConfigBean.PRE_FIX + "." + PROP_USERNAME),
jdbcDriverConfig.getUsername()));
JdbcDriverConfigBean.PRE_FIX + "." + PROP_USERNAME, jdbcDriverConfig.getUsername()));
props.put(
PROP_PASSWORD,
SystemUtils.getSystem(
SystemUtils.convertEnvKey(JdbcDriverConfigBean.PRE_FIX + "." + PROP_PASSWORD),
jdbcDriverConfig.getPassword()));
JdbcDriverConfigBean.PRE_FIX + "." + PROP_PASSWORD, jdbcDriverConfig.getPassword()));

// todo connection pool config
props.put(PROP_MINIDLE, jdbcDriverConfig.getMinIdle() + "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class JdbcDriverConfigBean implements JdbcDriverConfig {
private String driverClassName = "com.mysql.jdbc.Driver";

private String url =
"jdbc:mysql://127.0.0.1:2883/registrymetadb?useUnicode=true&characterEncoding=utf8";
"jdbc:mysql://127.0.0.1:3306/registrymetadb?useUnicode=true&characterEncoding=utf8";

private String username = "username";

Expand Down
6 changes: 3 additions & 3 deletions server/store/jdbc/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#

## connect db
jdbc.url = jdbc:mysql://127.0.0.1:2883/registrymetadb?useUnicode=true&characterEncoding=utf8
jdbc.username = username
jdbc.password = password
jdbc.url = jdbc:mysql://127.0.0.1:3306/registrymetadb?useUnicode=true&characterEncoding=utf8
jdbc.username = root
jdbc.password = root


0 comments on commit 536cfcc

Please sign in to comment.