|
1 | 1 | package io.vertx.easyerp.common; |
2 | 2 |
|
3 | 3 | import io.vertx.circuitbreaker.CircuitBreaker; |
| 4 | +import io.vertx.circuitbreaker.CircuitBreakerOptions; |
4 | 5 | import io.vertx.core.AbstractVerticle; |
| 6 | +import io.vertx.core.CompositeFuture; |
| 7 | +import io.vertx.core.Future; |
5 | 8 | import io.vertx.core.impl.ConcurrentHashSet; |
| 9 | +import io.vertx.core.json.JsonObject; |
6 | 10 | import io.vertx.core.logging.Logger; |
7 | 11 | import io.vertx.core.logging.LoggerFactory; |
| 12 | +import io.vertx.rxjava.servicediscovery.types.HttpEndpoint; |
| 13 | +import io.vertx.rxjava.servicediscovery.types.MessageSource; |
8 | 14 | import io.vertx.servicediscovery.Record; |
9 | 15 | import io.vertx.servicediscovery.ServiceDiscovery; |
| 16 | +import io.vertx.servicediscovery.ServiceDiscoveryOptions; |
| 17 | +import io.vertx.servicediscovery.types.EventBusService; |
| 18 | +import io.vertx.servicediscovery.types.JDBCDataSource; |
10 | 19 |
|
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
11 | 22 | import java.util.Set; |
12 | 23 |
|
| 24 | + |
13 | 25 | public class BaseMicroserviceVerticle extends AbstractVerticle { |
14 | 26 | private static final String LOG_EVENT_ADDRESS = "events.log"; |
15 | | - private static final Logger LOGGER = LoggerFactory.getLogger(BaseMicroserviceVerticle.class); |
| 27 | + private static final Logger logger = LoggerFactory.getLogger(BaseMicroserviceVerticle.class); |
16 | 28 |
|
17 | 29 | protected ServiceDiscovery discovery; |
18 | 30 | protected CircuitBreaker circuitBreaker; |
19 | 31 | protected Set<Record> registeredRecords = new ConcurrentHashSet<>(); |
20 | 32 |
|
21 | 33 | @Override |
22 | 34 | public void start() throws Exception { |
23 | | - super.start(); |
| 35 | + logger.info("Initializing discovery and circuit breaker"); |
| 36 | + |
| 37 | + //Initialize service discovery |
| 38 | + discovery = ServiceDiscovery.create(vertx, new ServiceDiscoveryOptions().setBackendConfiguration(config())); |
| 39 | + // init circuit breaker instance |
| 40 | + JsonObject cbOpts = config().getJsonObject("circuit-breaker") != null ? |
| 41 | + config().getJsonObject("circuit-breaker") : new JsonObject(); |
| 42 | + |
| 43 | + circuitBreaker = CircuitBreaker.create(cbOpts.getString("name", "circuit-breaker"), vertx, |
| 44 | + new CircuitBreakerOptions() |
| 45 | + .setMaxFailures(cbOpts.getInteger("max-failures", 5)) |
| 46 | + .setTimeout(cbOpts.getLong("timeout", 10000L)) |
| 47 | + .setFallbackOnFailure(true) |
| 48 | + .setResetTimeout(cbOpts.getLong("reset-timeout", 30000L)) |
| 49 | + ); |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Helper methods for adding resources to the discovery service |
| 55 | + * |
| 56 | + * @param name |
| 57 | + * @param host |
| 58 | + * @param port |
| 59 | + * @return |
| 60 | + */ |
| 61 | + protected Future<Void> publishHttpEndpoint(String name, String host, int port) { |
| 62 | + Record record = HttpEndpoint.createRecord(name, host, port, "/", |
| 63 | + new JsonObject().put("api.name", config().getString("api.name", ""))); |
| 64 | + return publish(record); |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + protected Future<Void> publishApiGateway(String host, int port) { |
| 69 | + Record record = HttpEndpoint.createRecord("api-gateway", true, host, port, "/", null) |
| 70 | + .setType("api-gateway"); |
| 71 | + return publish(record); |
| 72 | + } |
| 73 | + |
| 74 | + protected Future<Void> publishMessageSource(String name, String address) { |
| 75 | + Record record = MessageSource.createRecord(name, address); |
| 76 | + return publish(record); |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + protected Future<Void> publishJDBCDataSource(String name, JsonObject location) { |
| 81 | + Record record = JDBCDataSource.createRecord(name, location, new JsonObject()); |
| 82 | + return publish(record); |
| 83 | + } |
| 84 | + |
| 85 | + protected Future<Void> publishEventBusService(String name, String address, Class serviceClass) { |
| 86 | + Record record = EventBusService.createRecord(name, address, serviceClass); |
| 87 | + return publish(record); |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + /** |
| 92 | + * A helper method that simply publish logs on the event bus. |
| 93 | + * |
| 94 | + * @param type log type |
| 95 | + * @param data log message data |
| 96 | + */ |
| 97 | + protected void publishLogEvent(String type, JsonObject data) { |
| 98 | + JsonObject msg = new JsonObject().put("type", type) |
| 99 | + .put("message", data); |
| 100 | + vertx.eventBus().publish(LOG_EVENT_ADDRESS, msg); |
| 101 | + } |
| 102 | + |
| 103 | + protected void publishLogEvent(String type, JsonObject data, boolean succeeded) { |
| 104 | + JsonObject msg = new JsonObject().put("type", type) |
| 105 | + .put("status", succeeded) |
| 106 | + .put("message", data); |
| 107 | + vertx.eventBus().publish(LOG_EVENT_ADDRESS, msg); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @param record |
| 112 | + * @return |
| 113 | + */ |
| 114 | + protected Future<Void> publish(Record record) { |
| 115 | + if (discovery == null) { |
| 116 | + try { |
| 117 | + start(); |
| 118 | + } catch (Exception e) { |
| 119 | + throw new IllegalStateException("Cannot create discovery service"); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + Future<Void> future = Future.future(); |
| 124 | + discovery.publish(record, ar -> { |
| 125 | + if (ar.succeeded()) { |
| 126 | + registeredRecords.add(record); |
| 127 | + logger.info("Service <" + ar.result().getName() + "> published"); |
| 128 | + future.complete(); |
| 129 | + } else { |
| 130 | + logger.info("Error::Service <" + ar.result().getName() + "> failed published"); |
| 131 | + future.fail(ar.cause()); |
| 132 | + } |
| 133 | + }); |
| 134 | + |
| 135 | + return future; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Shutdown and cleanup all resources in discovery service |
| 140 | + * |
| 141 | + * @param stopFuture |
| 142 | + * @throws Exception |
| 143 | + */ |
| 144 | + @SuppressWarnings("rawtypes") |
| 145 | + @Override |
| 146 | + public void stop(Future<Void> stopFuture) throws Exception { |
| 147 | + List<Future> futures = new ArrayList<>(); |
| 148 | + registeredRecords.forEach(record -> { |
| 149 | + Future<Void> cleanUpFuture = Future.future(); |
| 150 | + discovery.unpublish(record.getRegistration(), cleanUpFuture.completer()); |
| 151 | + futures.add(cleanUpFuture); |
| 152 | + }); |
| 153 | + |
| 154 | + if (futures.isEmpty()) { |
| 155 | + discovery.close(); |
| 156 | + stopFuture.complete(); |
| 157 | + } else { |
| 158 | + CompositeFuture.all(futures) |
| 159 | + .setHandler(ar -> { |
| 160 | + discovery.close(); |
| 161 | + if (ar.failed()) { |
| 162 | + stopFuture.fail(ar.cause()); |
| 163 | + } else { |
| 164 | + stopFuture.complete(); |
| 165 | + } |
| 166 | + }); |
| 167 | + } |
24 | 168 | } |
25 | 169 | } |
0 commit comments