Skip to content

Commit

Permalink
add docker-compose support and fix some issues
Browse files Browse the repository at this point in the history
Signed-off-by: sczyh30 <sczyh16@gmail.com>
  • Loading branch information
sczyh30 committed May 20, 2016
1 parent 2884d77 commit 9195418
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 122 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Expand Up @@ -8,8 +8,8 @@ ENV VERTICLE_HOME /usr/verticles
EXPOSE 8082

COPY $VERTICLE_FILE $VERTICLE_HOME/
# COPY config.json $VERTICLE_HOME/
COPY config/config_docker.json $VERTICLE_HOME/

WORKDIR $VERTICLE_HOME
ENTRYPOINT ["sh", "-c"]
CMD ["java -jar vertx-blueprint-todo-backend-fat.jar"]
CMD ["java -jar vertx-blueprint-todo-backend-fat.jar -conf config_docker.json"]
12 changes: 8 additions & 4 deletions README.md
Expand Up @@ -12,16 +12,20 @@ Detailed documents(both in Chinese and English) are provided below.
- [English Version](docs/doc-en.md)
- [中文文档](docs/doc-zh-cn.md)

## Building
## Build

To build the code:

gradle build

Run service locally:

- with Redis: `java -jar build/libs/vertx-blueprint-todo-backend-fat.jar -conf config/config.json`
- with MySQL: `java -jar build/libs/vertx-blueprint-todo-backend-fat.jar -conf config/config_jdbc.json`

To build and run with Docker:
Run with Docker Compose:

gradle build
docker build -t sczyh30/vert-todo-backend .
docker-compose up --build

## OpenShift

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Expand Up @@ -9,7 +9,7 @@ jar {
manifest {
//attributes 'Main-Class': 'io.vertx.blueprint.todolist.Application'
attributes 'Main-Class': 'io.vertx.core.Launcher'
attributes 'Main-Verticle': 'io.vertx.blueprint.todolist.verticles.SingleApplicationVerticle'
attributes 'Main-Verticle': 'io.vertx.blueprint.todolist.verticles.TodoVerticle'
}
}

Expand All @@ -26,7 +26,7 @@ task annotationProcessing(type: JavaCompile, group: 'build') {
options.compilerArgs = [
"-proc:only",
"-processor", "io.vertx.codegen.CodeGenProcessor",
"-AoutputDirectory=" + destinationDir.absolutePath
"-AoutputDirectory=${destinationDir.absolutePath}"
]
}

Expand Down
3 changes: 3 additions & 0 deletions config/config.json
@@ -0,0 +1,3 @@
{
"service.type": "redis"
}
3 changes: 2 additions & 1 deletion config.json → config/config_docker.json
@@ -1,6 +1,7 @@
{
"service.type": "redis",
"http.port": 8082,
"http.address": "localhost",
"http.address": "0.0.0.0",
"redis.host": "redis",
"redis.port": 6379
}
8 changes: 8 additions & 0 deletions config/config_jdbc.json
@@ -0,0 +1,8 @@
{
"service.type": "jdbc",
"url": "jdbc:mysql://localhost/vertx_blueprint?characterEncoding=UTF-8&useSSL=false",
"driver_class": "com.mysql.cj.jdbc.Driver",
"user": "vbpdb1",
"password": "666666*",
"max_pool_size": 30
}
79 changes: 0 additions & 79 deletions src/main/java/io/vertx/blueprint/todolist/Application.java

This file was deleted.

Expand Up @@ -50,7 +50,11 @@ public class JdbcTodoService implements TodoService {
private static final String SQL_DELETE_ALL = "DELETE FROM `todo`";

public JdbcTodoService(JsonObject config) {
this.vertx = Vertx.vertx();
this(Vertx.vertx(), config);
}

public JdbcTodoService(Vertx vertx, JsonObject config) {
this.vertx = vertx;
this.config = config;
this.client = JDBCClient.createShared(vertx, config);
}
Expand Down
Expand Up @@ -25,12 +25,12 @@ public class RedisTodoService implements TodoService {
private final RedisOptions config;
private final RedisClient redis;

public RedisTodoService() {
this(new RedisOptions());
public RedisTodoService(RedisOptions config) {
this(Vertx.vertx(), config);
}

public RedisTodoService(RedisOptions config) {
this.vertx = Vertx.vertx();
public RedisTodoService(Vertx vertx, RedisOptions config) {
this.vertx = vertx;
this.config = config;
this.redis = RedisClient.create(vertx, config);
}
Expand Down
Expand Up @@ -6,7 +6,6 @@

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.DecodeException;
Expand All @@ -30,8 +29,10 @@
*/
public class SingleApplicationVerticle extends AbstractVerticle {

private static final String HOST = "127.0.0.1";
private static final int PORT = 8082;
private static final String HTTP_HOST = "0.0.0.0";
private static final String REDIS_HOST = "127.0.0.1";
private static final int HTTP_PORT = 8082;
private static final int REDIS_PORT = 6379;

private RedisClient redis;

Expand All @@ -47,8 +48,9 @@ private void initData() {
config = new RedisOptions()
.setHost(osHost).setPort(Integer.parseInt(osPort));
else
config = new RedisOptions().setHost(config().getString("redis.host", HOST));
//.setPort(config().getInteger("redis.port", 6379));
config = new RedisOptions()
.setHost(config().getString("redis.host", REDIS_HOST))
.setPort(config().getInteger("redis.port", REDIS_PORT));

this.redis = RedisClient.create(vertx, config);

Expand Down Expand Up @@ -95,8 +97,8 @@ public void start(Future<Void> future) throws Exception {

vertx.createHttpServer()
.requestHandler(router::accept)
.listen(config().getInteger("http.port", PORT),
config().getString("http.address", HOST), result -> {
.listen(config().getInteger("http.port", HTTP_PORT),
config().getString("http.address", HTTP_HOST), result -> {
if (result.succeeded())
future.complete();
else
Expand Down
Expand Up @@ -2,6 +2,8 @@

import io.vertx.blueprint.todolist.Constants;
import io.vertx.blueprint.todolist.entity.Todo;
import io.vertx.blueprint.todolist.service.JdbcTodoService;
import io.vertx.blueprint.todolist.service.RedisTodoService;
import io.vertx.blueprint.todolist.service.TodoService;

import io.vertx.core.AbstractVerticle;
Expand All @@ -16,6 +18,7 @@
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.BodyHandler;
import io.vertx.ext.web.handler.CorsHandler;
import io.vertx.redis.RedisOptions;

import java.util.HashSet;
import java.util.Random;
Expand All @@ -30,16 +33,26 @@
*/
public class TodoVerticle extends AbstractVerticle {

private static final String HOST = "127.0.0.1";
private static final String HOST = "0.0.0.0";
private static final int PORT = 8082;

private final TodoService service;

public TodoVerticle(TodoService service) {
this.service = service;
}
private TodoService service;

private void initData() {
final String serviceType = config().getString("service.type", "redis");
System.out.println("[INFO]Service Type: " + serviceType);
switch (serviceType) {
case "redis":
RedisOptions config = new RedisOptions()
.setHost(config().getString("redis.host", "127.0.0.1"))
.setPort(config().getInteger("redis.port", 6379));
service = new RedisTodoService(vertx, config);
break;
case "jdbc":
service = new JdbcTodoService(vertx, config());
break;
}

service.initData().setHandler(res -> {
if (res.failed()) {
System.err.println("[Error] Persistence service is not running!");
Expand All @@ -50,6 +63,8 @@ private void initData() {

@Override
public void start(Future<Void> future) throws Exception {
initData();

Router router = Router.router(vertx);
// CORS support
Set<String> allowHeaders = new HashSet<>();
Expand Down Expand Up @@ -86,8 +101,6 @@ public void start(Future<Void> future) throws Exception {
else
future.fail(result.cause());
});

initData();
}

/**
Expand Down
17 changes: 3 additions & 14 deletions src/test/java/io/vertx/blueprint/todolist/TodoApiTest.java
Expand Up @@ -37,22 +37,11 @@ public class TodoApiTest {
@Before
public void before(TestContext context) {
vertx = Vertx.vertx();
final int port = Integer.getInteger("http.port", PORT);
final DeploymentOptions options = new DeploymentOptions()
.setConfig(new JsonObject().put("http.port", port)
.setConfig(new JsonObject().put("http.port", 8082)
);

RedisOptions config;
// this is for OpenShift Redis Cartridge
String osPort = System.getenv("OPENSHIFT_REDIS_PORT");
String osHost = System.getenv("OPENSHIFT_REDIS_HOST");
if (osPort != null && osHost != null)
config = new RedisOptions()
.setHost(osHost).setPort(Integer.parseInt(osPort));
else
config = new RedisOptions().setHost("127.0.0.1");

TodoVerticle verticle = new TodoVerticle(new RedisTodoService(config));
// default config
TodoVerticle verticle = new TodoVerticle();

vertx.deployVerticle(verticle, options,
context.asyncAssertSuccess());
Expand Down

0 comments on commit 9195418

Please sign in to comment.