Skip to content

Commit 4a5f7ca

Browse files
committed
added BaseMicroserviceVerticle
1 parent 7c1addc commit 4a5f7ca

File tree

2 files changed

+146
-3
lines changed

2 files changed

+146
-3
lines changed
Lines changed: 146 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,169 @@
11
package io.vertx.easyerp.common;
22

33
import io.vertx.circuitbreaker.CircuitBreaker;
4+
import io.vertx.circuitbreaker.CircuitBreakerOptions;
45
import io.vertx.core.AbstractVerticle;
6+
import io.vertx.core.CompositeFuture;
7+
import io.vertx.core.Future;
58
import io.vertx.core.impl.ConcurrentHashSet;
9+
import io.vertx.core.json.JsonObject;
610
import io.vertx.core.logging.Logger;
711
import io.vertx.core.logging.LoggerFactory;
12+
import io.vertx.rxjava.servicediscovery.types.HttpEndpoint;
13+
import io.vertx.rxjava.servicediscovery.types.MessageSource;
814
import io.vertx.servicediscovery.Record;
915
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;
1019

20+
import java.util.ArrayList;
21+
import java.util.List;
1122
import java.util.Set;
1223

24+
1325
public class BaseMicroserviceVerticle extends AbstractVerticle {
1426
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);
1628

1729
protected ServiceDiscovery discovery;
1830
protected CircuitBreaker circuitBreaker;
1931
protected Set<Record> registeredRecords = new ConcurrentHashSet<>();
2032

2133
@Override
2234
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+
}
24168
}
25169
}

microservice-easyerp-common/src/main/java/io/vertx/easyerp/common/config/ConfigRetrieverHelper.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import io.vertx.core.json.JsonObject;
77
import io.vertx.core.logging.Logger;
88
import io.vertx.core.logging.LoggerFactory;
9-
import io.vertx.rx.java.RxHelper;
109
import rx.Observable;
1110
import io.vertx.core.Vertx;
1211

0 commit comments

Comments
 (0)