A prototype with dropwizard which demonstrates attributes of microservices.
- REST Endpoint Documentation: Swagger
- Service Container: DropWizard
- Dynamic Configuration: Archaius
- Monitoring: NewRelic
- Circuit Breaker: Hystrix
- Logging: SLF4j
There are two services here
- Person Service: It has REST endpoints to create (POST) and find a person by id (GET). The person service connects to the address service to get an address for the person. All created Person entries are cached in an in-memory map.
- Address Service: The address service provides ability to create (POST) and find an address by id (GET). The address service is built to return sporadic errors, for every 10 requests, the service responds as follows
- request-1: 200
- request-2: 404
- request-3: delayed response (delay of 1s)
- request-4: 200
- request-5: 200
- request-6: 503
- request-7: 503
- request-8: 404
- request-9: 200
- request-10: 200 (after which it resets to start back at 1)
You need the following to exercise these samples.
- JDK 1.7 or higher
- Apache Maven version 3.2.5 or higher
- New Relic SDK version 3.14.x if you want to see metrics
If you have NewRelic account, download the newrelic zip file, see instructions here Accessing NewRelic Java SDK.
Once you have the newrelic zip file downloaded, unzip the contents into the person service such that newrelic directory is a sibling to the pom.xml.
To build the person and address service do the following
- mvn clean package at the folder where the pom.xml exists
Copy the person.properties and address.properties into ${user.home}/config-root/person/person.properties and ${user.home}/config-root/address/address.properties where user.home is your home folder
.
To exercise the samples, do the following
You can start the address service by using the run.sh shell script. The address service by default starts at port 9100
You can create an address using the following payload to the url
curl -d '{"id": 1, "street": "my street", "city": "fremont", "state": "california","zipCode": "94536"}' http://localhost:9100/address -H "Content-Type: application/json"
You can fetch an address using the following url
curl -G http://localhost:9100/address/1 -H "Accept: application/json"
You can start the address service by using the run-metrics.sh (if you have a new relic account) else use run.sh shell script. The address service by default starts at port 7100
You can create a person entry using the following payload to the url
curl -d '{"id": 1,"firstName": "John","lastName": "Doe","age": 100}' http://localhost:7100/person -H "Content-Type: application/json"
You can fetch a person using the following url
curl -G http://localhost:7100/person/1 -H "Accept: application/json"
- With the address service running, add an address and retrieve it. The second call should return a 404.
- Now go to the address.properties located at
${user.home}/config-root/address/address.properties
and change the text for 404. - Rerun the GET calls and on the next 404 you should see the updated message.
- With both Person and Address services running make calls to the Person service to create a Person
- Call GET Person and you should see that a few responses do not have an address. See below
With Address
{"firstName":"John","lastName":"Doe","message":"","id":1,"age":100,"address":{"id":1,"street":"my street","city":"fantastic-city","state":"NY","zipCode":"00000"}}
Without Address
{"firstName":"John","lastName":"Doe","message":"","id":1,"age":100}
- Now go to the Person configuration under
${user.home}/config-root/person/person.configuration
and change the propertyuseFailSafe
to true - Kill the Address service and make sure it is no longer running
- Now make the same invokes and you should consistently see the address in the person GET response even if you shutdown the Address Service. What happens here is that whenever the HTTP Client experiences an exception the circuit trips and the fallback of fetch from cache kicks in. I