Skip to content

Commit 9c47535

Browse files
committed
fix: fix context autoconfigure
1 parent 9d4073b commit 9c47535

File tree

3 files changed

+79
-7
lines changed

3 files changed

+79
-7
lines changed

spring-cloud-tencent-polaris-context/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@
119119
</dependency>
120120
<!-- Polaris dependencies end -->
121121

122+
<dependency>
123+
<groupId>org.springframework.cloud</groupId>
124+
<artifactId>spring-cloud-starter-bootstrap</artifactId>
125+
</dependency>
126+
122127
<dependency>
123128
<groupId>org.springframework.boot</groupId>
124129
<artifactId>spring-boot-starter-test</artifactId>

spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/tsf/consul/TsfConsulBootstrapConfiguration.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@
1717

1818
package com.tencent.cloud.polaris.context.tsf.consul;
1919

20-
import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration;
21-
2220
import org.springframework.context.annotation.Configuration;
2321
import org.springframework.context.annotation.Import;
2422

2523
@Configuration(proxyBeanMethods = false)
26-
@Import({PolarisContextAutoConfiguration.class, TsfConsulAutoConfiguration.class})
24+
@Import({TsfConsulAutoConfiguration.class})
2725
public class TsfConsulBootstrapConfiguration {
2826

2927
}

spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/tsf/env/TsfCoreEnvironmentPostProcessor.java

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
import java.util.Map;
2222

2323
import com.tencent.polaris.api.utils.StringUtils;
24+
import org.apache.commons.logging.Log;
2425

2526
import org.springframework.boot.SpringApplication;
2627
import org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor;
2728
import org.springframework.boot.env.EnvironmentPostProcessor;
29+
import org.springframework.boot.logging.DeferredLogFactory;
2830
import org.springframework.core.Ordered;
2931
import org.springframework.core.env.ConfigurableEnvironment;
3032
import org.springframework.core.env.MapPropertySource;
@@ -34,13 +36,19 @@
3436
*
3537
* @author Haotian Zhang
3638
*/
37-
public class TsfCoreEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered {
39+
public final class TsfCoreEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered {
3840

3941
/**
4042
* run before {@link ConfigDataEnvironmentPostProcessor}.
4143
*/
4244
public static final int ORDER = ConfigDataEnvironmentPostProcessor.ORDER - 1;
4345

46+
private final Log LOGGER;
47+
48+
private TsfCoreEnvironmentPostProcessor(DeferredLogFactory logFactory) {
49+
this.LOGGER = logFactory.getLog(getClass());
50+
}
51+
4452
@Override
4553
public int getOrder() {
4654
return ORDER;
@@ -51,17 +59,78 @@ public void postProcessEnvironment(ConfigurableEnvironment environment, SpringAp
5159
String tsfAppId = environment.getProperty("tsf_app_id");
5260
if (StringUtils.isNotBlank(tsfAppId)) {
5361
Map<String, Object> defaultProperties = new HashMap<>();
62+
//默认开启polaris
63+
defaultProperties.put("spring.cloud.polaris.enabled", "true");
64+
// tsf_application_id
65+
String tsfApplicationId = environment.getProperty("tsf_application_id");
66+
if (StringUtils.isBlank(tsfApplicationId)) {
67+
LOGGER.error("tsf_application_id is empty");
68+
}
69+
70+
// tsf_group_id
71+
String tsfGroupId = environment.getProperty("tsf_group_id");
72+
if (StringUtils.isBlank(tsfGroupId)) {
73+
LOGGER.error("tsf_group_id is empty");
74+
}
75+
76+
// tsf_namespace_id
77+
String tsfNamespaceId = environment.getProperty("tsf_namespace_id");
78+
if (StringUtils.isBlank(tsfNamespaceId)) {
79+
LOGGER.error("tsf_namespace_id is empty");
80+
}
81+
else {
82+
defaultProperties.put("spring.cloud.polaris.namespace", tsfNamespaceId);
83+
}
84+
85+
// tsf_consul_ip
86+
String tsfConsulIp = environment.getProperty("tsf_consul_ip");
87+
if (StringUtils.isBlank(tsfConsulIp)) {
88+
LOGGER.error("tsf_consul_ip is empty");
89+
}
90+
91+
// tsf_consul_port
92+
String tsfConsulPort = environment.getProperty("tsf_consul_port");
93+
if (StringUtils.isBlank(tsfConsulPort)) {
94+
LOGGER.error("tsf_consul_port is empty");
95+
}
96+
97+
// tsf_token
98+
String tsfConsulToken = environment.getProperty("tsf_token");
99+
if (StringUtils.isBlank(tsfConsulToken)) {
100+
LOGGER.error("tsf_token is empty");
101+
}
54102

55-
// TODO 接入consul配置后需要改动这个选项的判断
56103
// tse_polaris_enable
57-
defaultProperties.put("spring.cloud.polaris.config.enabled", environment.getProperty("tse_polaris_enable", "false"));
104+
String tsePolarisEnable = environment.getProperty("tse_polaris_enable", "false");
105+
if (StringUtils.equals(tsePolarisEnable, "true")) {
106+
defaultProperties.put("spring.cloud.polaris.config.enabled", "true");
107+
}
108+
else {
109+
defaultProperties.put("spring.cloud.polaris.contract.report.enabled", "false");
110+
defaultProperties.put("spring.cloud.polaris.config.enabled", "true");
111+
defaultProperties.put("spring.cloud.polaris.config.internal-enabled", "false");
112+
defaultProperties.put("spring.cloud.polaris.config.data-source", "consul");
113+
defaultProperties.put("spring.cloud.polaris.config.address", "http://" + tsfConsulIp + ":" + tsfConsulPort);
114+
defaultProperties.put("spring.cloud.polaris.config.port", tsfConsulPort);
115+
defaultProperties.put("spring.cloud.polaris.config.token", tsfConsulToken);
116+
defaultProperties.put("spring.cloud.polaris.config.groups[0].namespace", "config");
117+
defaultProperties.put("spring.cloud.polaris.config.groups[0].name", "application");
118+
defaultProperties.put("spring.cloud.polaris.config.groups[0].files[0]", tsfApplicationId + "/" + tsfGroupId + "/");
119+
defaultProperties.put("spring.cloud.polaris.config.groups[0].files[1]", tsfNamespaceId + "/");
120+
}
58121

59122
// tse_polaris_ip
60123
defaultProperties.put("spring.cloud.polaris.address", "grpc://" + environment.getProperty("tse_polaris_ip", "") + ":8091");
61124

62-
// tse_polaris_ip
125+
// tsf_sctt_extensions_port
63126
defaultProperties.put("spring.cloud.polaris.stat.port", environment.getProperty("tsf_sctt_extensions_port", "11134"));
64127

128+
// rule based router fail over type
129+
defaultProperties.put("spring.cloud.polaris.router.rule-router.fail-over", "none");
130+
131+
// namespace affinity router
132+
defaultProperties.put("spring.cloud.polaris.router.namespace-router.enabled", "true");
133+
65134
MapPropertySource propertySource = new MapPropertySource("tsf-polaris-properties", defaultProperties);
66135
environment.getPropertySources().addFirst(propertySource);
67136
}

0 commit comments

Comments
 (0)