\345\210\206\345\270\203\345\274\217\347\263\273\347\273\237\346\236\266\346\236\204\346\226\271\346\241\210\350\256\276\350\256\241\345\210\235\347\250\277.md
... ...
@@ -250,6 +250,37 @@ public class ReverseProxyServlet extends HttpServlet {
250 250
}
251 251
252 252
```
253
+自定义服务注册与发现功能
254
+```java
255
+import java.util.ArrayList;
256
+import java.util.List;
257
+import java.util.Random;
258
+
259
+public class ServiceRegistry {
260
+ private final List<String> services = new ArrayList<>();
261
+ private final Random random = new Random();
262
+
263
+ public void register(String serviceUrl) {
264
+ services.add(serviceUrl);
265
+ }
266
+
267
+ public void deregister(String serviceUrl) {
268
+ services.remove(serviceUrl);
269
+ }
270
+
271
+ public String getRandomService() {
272
+ if (services.isEmpty()) {
273
+ throw new IllegalStateException("No registered services");
274
+ }
275
+ return services.get(random.nextInt(services.size()));
276
+ }
277
+
278
+ public List<String> getAllServices() {
279
+ return new ArrayList<>(services);
280
+ }
281
+}
282
+
283
+```
253 284
254 285
#### 2.2 MySQL 维护app和容器服务的关系
255 286