Commit fef970e6 authored by Pid's avatar Pid Committed by GitHub
Browse files

Merge pull request #11 from microservices-demo/add_health_endpoint

Initial checkin for healthcheck
parents 038527e2 5daa3fc3
package works.weave.socks.cart.configuration;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.mongodb.MongoClientOptions;
@Configuration
@AutoConfigureBefore(MongoAutoConfiguration.class)
public class MongoConfiguration {
@Bean
public MongoClientOptions optionsProvider() {
MongoClientOptions.Builder optionsBuilder = new MongoClientOptions.Builder();
optionsBuilder.serverSelectionTimeout(10000);
MongoClientOptions options = optionsBuilder.build();
return options;
}
}
......@@ -10,6 +10,7 @@ import works.weave.socks.cart.cart.CartDAO;
import works.weave.socks.cart.cart.CartResource;
import works.weave.socks.cart.entities.Cart;
@RestController
@RequestMapping(path = "/carts")
public class CartsController {
......
package works.weave.socks.cart.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import works.weave.socks.cart.entities.HealthCheck;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
public class HealthCheckController {
@Autowired
private MongoTemplate mongoTemplate;
@ResponseStatus(HttpStatus.OK)
@RequestMapping(method = RequestMethod.GET, path = "/health")
public
@ResponseBody
Map<String, List<HealthCheck>> getHealth() {
Map<String, List<HealthCheck>> map = new HashMap<String, List<HealthCheck>>();
List<HealthCheck> healthChecks = new ArrayList<HealthCheck>();
Date dateNow = Calendar.getInstance().getTime();
HealthCheck app = new HealthCheck("cart", "OK", dateNow);
HealthCheck database = new HealthCheck("cart-db", "OK", dateNow);
try {
mongoTemplate.executeCommand("{ buildInfo: 1 }");
} catch (Exception e) {
database.setStatus("err");
}
healthChecks.add(app);
healthChecks.add(database);
map.put("health", healthChecks);
return map;
}
}
package works.weave.socks.cart.entities;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.util.Calendar;
import java.util.Date;
@JsonIgnoreProperties(ignoreUnknown = true)
public class HealthCheck {
private String service;
private String status;
@JsonFormat(pattern="yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
private Date date = Calendar.getInstance().getTime();
public HealthCheck() {
}
public HealthCheck(String service, String status, Date date) {
this.service = service;
this.status = status;
this.date = date;
}
@Override
public String toString() {
return "HealthCheck{" +
"service='" + service + '\'' +
", status='" + status + '\'' +
", date='" + date +
'}';
}
public String getService() {
return service;
}
public void setService(String service) {
this.service = service;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
server.port=${port:8081}
spring.data.mongodb.uri=mongodb://${db:cart-db}:27017/data
endpoints.health.enabled=false
package works.weave.socks.cart.controllers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import static org.junit.Assert.assertThat;
import works.weave.socks.cart.entities.HealthCheck;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class UnitHealthCheckController {
@Autowired
private HealthCheckController healthCheckController;
@Test
public void shouldGetHealth() {
Map<String, List<HealthCheck>> results = healthCheckController.getHealth();
assertThat(results.get("health").size(), is(equalTo(2)));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment