21
21
import com .tencent .polaris .api .config .consumer .OutlierDetectionConfig .When ;
22
22
import com .tencent .polaris .api .config .consumer .ServiceRouterConfig ;
23
23
import com .tencent .polaris .api .exception .PolarisException ;
24
+ import com .tencent .polaris .api .plugin .Plugin ;
24
25
import com .tencent .polaris .api .plugin .Supplier ;
25
26
import com .tencent .polaris .api .plugin .cache .FlowCache ;
26
27
import com .tencent .polaris .api .plugin .circuitbreaker .CircuitBreaker ;
35
36
import java .util .ArrayList ;
36
37
import java .util .Collections ;
37
38
import java .util .List ;
39
+ import org .slf4j .Logger ;
40
+ import org .slf4j .LoggerFactory ;
38
41
39
42
/**
40
43
* 流程编排所需要用到的插件实例列表
43
46
*/
44
47
public class Extensions {
45
48
49
+ private static final Logger LOG = LoggerFactory .getLogger (Extensions .class );
50
+
46
51
private LocalRegistry localRegistry ;
47
52
48
53
private ServerConnector serverConnector ;
@@ -89,28 +94,32 @@ public void init(Configuration config, Supplier plugins, ValueContext valueConte
89
94
loadBalancer = (LoadBalancer ) plugins .getPlugin (PluginTypes .LOAD_BALANCER .getBaseType (), loadBalanceType );
90
95
91
96
List <ServiceRouter > beforeRouters = loadServiceRouters (config .getConsumer ().getServiceRouter ().getBeforeChain (),
92
- plugins );
97
+ plugins , true );
93
98
List <ServiceRouter > coreRouters = loadServiceRouters (config .getConsumer ().getServiceRouter ().getChain (),
94
- plugins );
99
+ plugins , false );
95
100
List <ServiceRouter > afterRouters = loadServiceRouters (config .getConsumer ().getServiceRouter ().getAfterChain (),
96
- plugins );
101
+ plugins , true );
97
102
configRouterChainGroup = new DefaultRouterChainGroup (beforeRouters , coreRouters , afterRouters );
98
103
//加载系统路由链
99
104
List <String > sysBefore = new ArrayList <>();
100
105
sysBefore .add (ServiceRouterConfig .DEFAULT_ROUTER_ISOLATED );
101
106
List <String > sysAfter = new ArrayList <>();
102
107
sysAfter .add (ServiceRouterConfig .DEFAULT_ROUTER_RECOVER );
103
- List <ServiceRouter > sysBeforeRouters = loadServiceRouters (sysBefore , plugins );
104
- List <ServiceRouter > sysAfterRouters = loadServiceRouters (sysAfter , plugins );
108
+ List <ServiceRouter > sysBeforeRouters = loadServiceRouters (sysBefore , plugins , true );
109
+ List <ServiceRouter > sysAfterRouters = loadServiceRouters (sysAfter , plugins , true );
105
110
sysRouterChainGroup = new DefaultRouterChainGroup (sysBeforeRouters , Collections .emptyList (), sysAfterRouters );
106
111
107
112
//加载熔断器
108
113
boolean enable = config .getConsumer ().getCircuitBreaker ().isEnable ();
109
114
List <String > cbChain = config .getConsumer ().getCircuitBreaker ().getChain ();
110
115
if (enable && CollectionUtils .isNotEmpty (cbChain )) {
111
116
for (String cbName : cbChain ) {
112
- circuitBreakers .add ((CircuitBreaker ) plugins .getPlugin (
113
- PluginTypes .CIRCUIT_BREAKER .getBaseType (), cbName ));
117
+ Plugin pluginValue = plugins .getOptionalPlugin (PluginTypes .CIRCUIT_BREAKER .getBaseType (), cbName );
118
+ if (null == pluginValue ) {
119
+ LOG .warn ("circuitBreaker plugin {} not found" , cbName );
120
+ continue ;
121
+ }
122
+ circuitBreakers .add ((CircuitBreaker ) pluginValue );
114
123
}
115
124
}
116
125
@@ -126,11 +135,21 @@ public ValueContext getValueContext() {
126
135
return valueContext ;
127
136
}
128
137
129
- public static List <ServiceRouter > loadServiceRouters (List <String > routerChain , Supplier plugins ) {
138
+ public static List <ServiceRouter > loadServiceRouters (List <String > routerChain , Supplier plugins , boolean force ) {
130
139
List <ServiceRouter > routers = new ArrayList <>();
131
140
if (CollectionUtils .isNotEmpty (routerChain )) {
132
141
for (String routerName : routerChain ) {
133
- routers .add ((ServiceRouter ) plugins .getPlugin (PluginTypes .SERVICE_ROUTER .getBaseType (), routerName ));
142
+ Plugin routerPlugin ;
143
+ if (force ) {
144
+ routerPlugin = plugins .getPlugin (PluginTypes .SERVICE_ROUTER .getBaseType (), routerName );
145
+ } else {
146
+ routerPlugin = plugins .getOptionalPlugin (PluginTypes .SERVICE_ROUTER .getBaseType (), routerName );
147
+ }
148
+ if (null == routerPlugin ) {
149
+ LOG .warn ("router {} not found" , routerName );
150
+ continue ;
151
+ }
152
+ routers .add ((ServiceRouter ) routerPlugin );
134
153
}
135
154
}
136
155
return Collections .unmodifiableList (routers );
@@ -141,8 +160,12 @@ private void loadOutlierDetector(Configuration config, Supplier plugins) throws
141
160
List <String > detectionChain = config .getConsumer ().getOutlierDetection ().getChain ();
142
161
if (enable && CollectionUtils .isNotEmpty (detectionChain )) {
143
162
for (String detectorName : detectionChain ) {
144
- outlierDetectors .add ((OutlierDetector ) plugins .getPlugin (
145
- PluginTypes .OUTLIER_DETECTOR .getBaseType (), detectorName ));
163
+ Plugin pluginValue = plugins .getOptionalPlugin (PluginTypes .OUTLIER_DETECTOR .getBaseType (), detectorName );
164
+ if (null == pluginValue ) {
165
+ LOG .warn ("outlierDetector plugin {} not found" , detectorName );
166
+ continue ;
167
+ }
168
+ outlierDetectors .add ((OutlierDetector ) pluginValue );
146
169
}
147
170
}
148
171
}
0 commit comments