An open API service indexing awesome lists of open source software.

https://github.com/meta-magic/microservice-sample


https://github.com/meta-magic/microservice-sample

Last synced: about 1 month ago
JSON representation

Awesome Lists containing this project

README

          

# microservice-sample

This is a proof-of-concept application, which demonstrates [Microservice Architecture Pattern](http://martinfowler.com/microservices/) using Spring Boot, Spring Cloud, Spring Config.



## Functional services

Decomposed into two core microservices. All of them are independently deployable applications, organized around certain business capability.

#### Login service

Method: POST

Path : /authenticate

Description: Validate userid password.

Curl Command: curl -H "Content-Type: application/json" -X POST -d '{"loginId":"xyz","password":"xyz"}' http://localhost:1112/loginservice/authenticate

Note: Business logic is hard coded, If userid/password are sample its authenticated.


## Edge Server

This is entry point to outside world.

In theory, a client could make requests to each of the microservices directly. But obviously, there are challenges and limitations with this option, like necessity to know all endpoints addresses, perform http request for each peace of information separately, merge the result on a client side. Another problem is non web-friendly protocols, which might be used on the backend.

It can be used for authentication, insights, stress and canary testing, service migration, static response handling, active traffic management.

With Spring Cloud we can enable it with one @EnableZuulProxy annotation

@EnableZuulProxy
public class Config {
}

Route requests to appropriate microservices, defined in application.properties

zuul.prefix=/api

zuul.ignoredServices='*'

zuul.routes.auth-service.path=/auth-service/**

zuul.routes.auth-service.serviceId=AUTH-SERVICE

zuul.ribbon.restclient.enabled=true

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=60000

Note: Detail code for edge server can be found in edge-server project


## Service Discovery

The key part of Service discovery is Registry. I use Netflix Eureka in this project. Eureka is a good example of the client-side discovery pattern, when client is responsible for determining locations of available service instances (using Registry server) and load balancing requests across them.

With Spring Boot, you can easily build Eureka Registry with spring-cloud-starter-eureka-server dependency, @EnableEurekaServer annotation and simple configuration properties.

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}

Every service will get register to Eureka server on its starup and provide all required info like URL, Host etc.

application.properties for eureka server.

spring.application.name=eureka-service

server.port=1111

eureka.instance.client.register-with-eureka=false

eureka.instance.client.fetch-registry=false

logging.level.com.netflix.eureka=OFF

logging.level.com.netflix.discover=OFF

Note: Detail code for eureka server can be found in eureka-server project


##Load balancer, Circuit breaker and Http client

####Ribbon

Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients. Compared to a traditional load balancer, there is no need in additional hop for every over-the-wire invocation - you can contact desired service directly.

Out of the box, it natively integrates with Spring Cloud and Service Discovery. Eureka Client provides a dynamic list of available servers so Ribbon could balance between them.

####Hystrix

Hystrix is the implementation of Circuit Breaker pattern, which gives a control over latency and failure from dependencies accessed over the network. The main idea is to stop cascading failures in a distributed environment with a large number of microservices. That helps to fail fast and recover as soon as possible - important aspects of fault-tolerant systems that self-heal.

Besides circuit breaker control, with Hystrix you can add a fallback method that will be called to obtain a default value in case the main command fails.

####Feign

Feign is a declarative Http client, which seamlessly integrates with Ribbon and Hystrix. Actually, with one spring-cloud-starter-feign dependency and @EnableFeignClients annotation you have a full set of Load balancer, Circuit breaker and Http client with sensible ready-to-go default configuration.

public interface LoginService {

@RequestMapping(value="/authenticate", method=RequestMethod.POST, produces="application/json")

public MessageWrapper authenticate(@RequestBody Object loginBean);

}

@FeignClient(value="login-service", fallback=LoginServiceFallBack.class)

public interface LoginServiceClient extends LoginService

{

}

NOTE: PLEASE LOOK INTO AUTH-CLIENT PROJECT FOR FEIGN


## Notes
Once you download code and its setup in standard ID starts the application in following sequence.

1 - Eureka Server - You might get error at console saying replica server is not configure, we can ignore for development purpose.

2 - Login Service

3 - Auth Client

4 - Edge Server

Use following curl command to test

curl -H "Content-Type: application/json" -X POST -d '{"loginId":"xyz","password":"xyssssz"}' http://localhost:1114/api/auth-service/login/authenticate

curl -H "Content-Type: application/json" -X POST -d '{"loginId":"xyz","password":"xyz"}' http://localhost:1114/api/auth-service/login/authenticate

If you want to check FALLBACK mechanism, stop login/token service and execute above commands again.

Once fallback scenario is tested start the login/token service and execute above commands. Wait for 1 Minute once server are started as hystrix default isolation for thread is set to 1min.