This repository has been archived by the owner on Dec 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from microservices-demo/add_health_endpoint
Initial checkin for healthcheck
- Loading branch information
Showing
6 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
src/main/java/works/weave/socks/cart/configuration/MongoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/works/weave/socks/cart/controllers/HealthCheckController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
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; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/works/weave/socks/cart/entities/HealthCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
server.port=${port:8081} | ||
spring.data.mongodb.uri=mongodb://${db:cart-db}:27017/data | ||
endpoints.health.enabled=false |
39 changes: 39 additions & 0 deletions
39
src/test/java/works/weave/socks/cart/controllers/UnitHealthCheckController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
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))); | ||
} | ||
} |