21
21
import java .util .Map ;
22
22
23
23
import com .tencent .polaris .api .utils .StringUtils ;
24
+ import org .apache .commons .logging .Log ;
24
25
25
26
import org .springframework .boot .SpringApplication ;
26
27
import org .springframework .boot .context .config .ConfigDataEnvironmentPostProcessor ;
27
28
import org .springframework .boot .env .EnvironmentPostProcessor ;
29
+ import org .springframework .boot .logging .DeferredLogFactory ;
28
30
import org .springframework .core .Ordered ;
29
31
import org .springframework .core .env .ConfigurableEnvironment ;
30
32
import org .springframework .core .env .MapPropertySource ;
34
36
*
35
37
* @author Haotian Zhang
36
38
*/
37
- public class TsfCoreEnvironmentPostProcessor implements EnvironmentPostProcessor , Ordered {
39
+ public final class TsfCoreEnvironmentPostProcessor implements EnvironmentPostProcessor , Ordered {
38
40
39
41
/**
40
42
* run before {@link ConfigDataEnvironmentPostProcessor}.
41
43
*/
42
44
public static final int ORDER = ConfigDataEnvironmentPostProcessor .ORDER - 1 ;
43
45
46
+ private final Log LOGGER ;
47
+
48
+ private TsfCoreEnvironmentPostProcessor (DeferredLogFactory logFactory ) {
49
+ this .LOGGER = logFactory .getLog (getClass ());
50
+ }
51
+
44
52
@ Override
45
53
public int getOrder () {
46
54
return ORDER ;
@@ -51,17 +59,78 @@ public void postProcessEnvironment(ConfigurableEnvironment environment, SpringAp
51
59
String tsfAppId = environment .getProperty ("tsf_app_id" );
52
60
if (StringUtils .isNotBlank (tsfAppId )) {
53
61
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
+ }
54
102
55
- // TODO 接入consul配置后需要改动这个选项的判断
56
103
// 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
+ }
58
121
59
122
// tse_polaris_ip
60
123
defaultProperties .put ("spring.cloud.polaris.address" , "grpc://" + environment .getProperty ("tse_polaris_ip" , "" ) + ":8091" );
61
124
62
- // tse_polaris_ip
125
+ // tsf_sctt_extensions_port
63
126
defaultProperties .put ("spring.cloud.polaris.stat.port" , environment .getProperty ("tsf_sctt_extensions_port" , "11134" ));
64
127
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
+
65
134
MapPropertySource propertySource = new MapPropertySource ("tsf-polaris-properties" , defaultProperties );
66
135
environment .getPropertySources ().addFirst (propertySource );
67
136
}
0 commit comments