Microservices with NetflixOSS: Karyon and Services part 3
This is the next post on the services of bulding a microservice with the netflix stack . If you did not read the previous posts i recommend you do:
* NetflixOSS The DevOps Stack for Microservices - http://diego-pacheco.blogspot.com/2015/09/netflixoss-devops-stack-for.html
* Microservices with NetflixOSS: Karyon, Ribbon and Eureka part 1 - http://diego-pacheco.blogspot.com/2015/09/microservices-with-netflixoss-karyon.html
* Microservices with NetflixOSS: Building and Running Eureka part 2 - http://diego-pacheco.blogspot.com/2015/09/microservices-with-netflixoss-building.html
It`s time to build a simple REST service using Karyon. Karyon will self-register as an Eureka client and we will do this using java annotations. To do this task we will create 8 classes. Lets see what are the responsibility of each class.
The Classes
* AuthenticationService -> Just a contract to perform authentication on the services.
* AuthenticationServiceImpl -> Authentication implementation it just check for a header and if not present dont let the call pass tought the service.
* AuthInterceptor -> Karyon Interceptor to glue the authentication implementation with the server requests
* HealthcheckResource -> Is the implementation of the Health Checker - Eureka call is to know if the service still up and running fine - if it fails eureka will unregistered the instance.
* LoggingInterceptor -> Another interceptor just to log whats going on.
* KaryonJerseyServerApp -> Where you configure your microservice.
* MainRunner -> A simple class to run the application.
* WeatherResource -> REST contract and implementation of a Weather Service - The actual Service :-)
WeatherResource
As you can see is very straight forward - i`m just calling open weather api with your location. This call is blocking and could hang to fix this problem we should use Ribbon and Hystrix witch will be covered in future posts.
KaryonJerseyServerApp (App Configs)
Here is wehere you configure everything. We use annotations todo the job. @ArchaiusBootstrap bootup archaius for us, @KaryonBootstrap we provie the name of the service(this will be used to register into eureka) and what is the heath checker class.
There are other modules like: KaryonEurekaModule to make the app register into eureka automatically and KaryonServoModule to have metrics and KaryonWebAdminModule to have a web admin module exposed.
We need add some property files to configure the APP and make IT able to find eureka - so lets start with the REST config first.
weather-service.properties
Now we need add some configs so Karyon can find eureka.
eureka-client.properties
Them you can run the app running the main runner class. You will see some logging like this.
IF you goto eureka: http://localhost:8080/eureka/v2/apps you will see the weather-service is registered there. This could take some time like 30 seconds.
Now you can open your browser and call the service: http://localhost:6002/weather/now/Sunnyvale
You also can check the web admin if you go: http://localhost:8077/admin/
Servo JMX metrics are also available: http://localhost:8077/admin/#view=jmx&
The FULL code you can get into my github here: https://github.com/diegopacheco/netflixoss-pocs/tree/master/karyon-server-eureka-client
Cheers,
Diego Pacheco
* NetflixOSS The DevOps Stack for Microservices - http://diego-pacheco.blogspot.com/2015/09/netflixoss-devops-stack-for.html
* Microservices with NetflixOSS: Karyon, Ribbon and Eureka part 1 - http://diego-pacheco.blogspot.com/2015/09/microservices-with-netflixoss-karyon.html
* Microservices with NetflixOSS: Building and Running Eureka part 2 - http://diego-pacheco.blogspot.com/2015/09/microservices-with-netflixoss-building.html
It`s time to build a simple REST service using Karyon. Karyon will self-register as an Eureka client and we will do this using java annotations. To do this task we will create 8 classes. Lets see what are the responsibility of each class.
The Classes
* AuthenticationService -> Just a contract to perform authentication on the services.
* AuthenticationServiceImpl -> Authentication implementation it just check for a header and if not present dont let the call pass tought the service.
* AuthInterceptor -> Karyon Interceptor to glue the authentication implementation with the server requests
* HealthcheckResource -> Is the implementation of the Health Checker - Eureka call is to know if the service still up and running fine - if it fails eureka will unregistered the instance.
* LoggingInterceptor -> Another interceptor just to log whats going on.
* KaryonJerseyServerApp -> Where you configure your microservice.
* MainRunner -> A simple class to run the application.
* WeatherResource -> REST contract and implementation of a Weather Service - The actual Service :-)
WeatherResource
As you can see is very straight forward - i`m just calling open weather api with your location. This call is blocking and could hang to fix this problem we should use Ribbon and Hystrix witch will be covered in future posts.
KaryonJerseyServerApp (App Configs)
Here is wehere you configure everything. We use annotations todo the job. @ArchaiusBootstrap bootup archaius for us, @KaryonBootstrap we provie the name of the service(this will be used to register into eureka) and what is the heath checker class.
There are other modules like: KaryonEurekaModule to make the app register into eureka automatically and KaryonServoModule to have metrics and KaryonWebAdminModule to have a web admin module exposed.
We need add some property files to configure the APP and make IT able to find eureka - so lets start with the REST config first.
weather-service.properties
Now we need add some configs so Karyon can find eureka.
eureka-client.properties
Them you can run the app running the main runner class. You will see some logging like this.
IF you goto eureka: http://localhost:8080/eureka/v2/apps you will see the weather-service is registered there. This could take some time like 30 seconds.
You also can check the web admin if you go: http://localhost:8077/admin/
Servo JMX metrics are also available: http://localhost:8077/admin/#view=jmx&
The FULL code you can get into my github here: https://github.com/diegopacheco/netflixoss-pocs/tree/master/karyon-server-eureka-client
Cheers,
Diego Pacheco