Showing posts with label SpringBoot. Show all posts
Showing posts with label SpringBoot. Show all posts

July 21, 2017

Evolving a REST API is a difficult problem – one for which many options are available - Solution for API Versioning

 Situation:
    Evolving a REST API is a difficult problem – one for which many options are available - Solution for API Versioning.

Action
 > Specifying the version number in URI  E.g.: /v2/products/
              The URI in REST should represent the resource only
              This is not suggested unless the entire set of APIs migrate to a new platform
> Passing the version number as a URI Parameter: E.g.: /products?version=2
            • This is used by Amazon, Google and Netflix
            • Parameters are used for filtering resources and should not specify the implementation
            • The APIs have to parse the param string before knowing where to route the request.
> Using the well-known HTTP header: The Accept header is used to make up our own resource types. E.g.: Accept: application/vnd.test.v2 + json

This means that I, as a vendor, can define (and can register) a media type. If we do this it's called a 'vendor' media type. It works and well-known.

The disadvantage with this approach is it is not very intuitive.
According to the Rest API specifications, A REST API should be entered with no prior knowledge beyond the initial URI (bookmark) and set of standardized media types that are appropriate for the intended audience. Failure here implies that out-of-band information is driving interaction instead of hypertext.

So URIs are not part of the contract!
A REST API should spend almost all of its descriptive effort in defining the media type(s) used for representing resources and driving application state, or in defining extended relation names and/or hypertext-enabled mark-up for existing standard media types.

So the Media Type definitions are part of the contract and should be prior knowledge for the client that consumes the API.


Conclusion:
This article tried to provide an overview of the very diverse and difficult problem of evolving a REST Service. We discussed the two common solutions, advantages and disadvantages of each one, and ways to reason about these approaches in the context of REST. The article concludes by making the case for the second solution – versioning the media types, while examining the possible changes to a RESTful API.Finally Considering all the above, using well-known HTTP header, Accept may be a better option.
 
 

@RequestMapping(..., produces = "application/vnd.company.app-[1.0-1.6]+json)
@ResponseBody
public Object method1() {
   // so something
   return object;
}
@RequestMapping(..., produces = "application/vnd.company.app-[1.7-]+json)
@ResponseBody
public Object method2() {
   // so something
   return object;
}
 
Result:
We can get the different version data without any changes in the code level. 

Ref:
http://www.baeldung.com/rest-versioning

Solution implementation at: https://github.com/mspapant/restVersioningExample/


Read more ...

How Internally works Eureka connection pool? How to configure?

We can configure total connections Eureka, updating peerNodeTotalConnections property in the application.yml (default: eurkea peerNodeTotalConnections is 1000) in eureka application:

eureka:
    server:
       waitTimeInMsWhenSyncEmpty: 0
       peerNodeTotalConnections: 1000
       peerNodeTotalConnectionsPerHost: 500

Ref:
https://github.com/spring-cloud/spring-cloud-netflix/blob/master/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaServerConfigBean.java
EurekaServerConfigBean :
private int peerNodeTotalConnections = 1000;
private int peerNodeTotalConnectionsPerHost = 500;

http://blog.abhijitsarkar.org/technical/netflix-eureka/:
For Eureka server config: org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean that implements com.netflix.eureka.EurekaServerConfig. All properties are prefixed by eureka.server.
Further more info:
We can check eureka health and info, we can configure below configuration in Eureka:

instance:
    hostname: ${vcap.application.uris[0]:localhost}
    statusPageUrl: https://${eureka.hostname}/info
    healthCheckUrl: https://${eureka.hostname}/health
    homePageUrl: https://${eureka.hostname}/

For Eureka instance config: org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean that implements (indirectly) com.netflix.appinfo.EurekaInstanceConfig. All properties are prefixed by eureka.instance.

For Eureka client config: org.springframework.cloud.netflix.eureka.EurekaClientConfigBean that implements com.netflix.discovery.EurekaClientConfig. All properties are prefixed by eureka.client.

We can check eureka apps info:
https://github.com/Netflix/eureka/wiki/Eureka-REST-operations
Using ribbon, zuul configurations, we can manage connection pool also.


For more information about eureka connections:
DefaultEurekaServerConfig side maximum limit of Peer Node Total Connections = 1000 and Peer Node Total Connections Per Host = 500. DefaultEurekaClientConfig side it is 200 and 50.
Given our environment, from the short and medium-terms, the above default limits may be fine and these connections are not expensive as database connections
In the case in the long-term, we need to override the defaults, we have options to do so.
https://github.com/Netflix/eureka/wiki/Overriding-Default-Configurations
The life cycle of Eureka client and server are summarized below.
Registration:
Eureka client registers the information about the running instance to the Eureka server
Renewal:
1. Eureka client needs to renew the lease by sending heartbeats every 30 seconds.
2. The renewal informs the Eureka server that the instance is still alive. If the server doesn’t see a renewal for 90 seconds, it removes the instance out of its registry.
Fetching Registry:
1. Eureka clients fetch the registry information from the server and cache it locally. After that, the clients use that information to find other services.
2. This information is updated periodically (every 30 seconds) by getting the delta updates between the last fetch cycle and the current one.
3. After getting the deltas, Eureka client reconciles the information with the server by comparing the instance counts returned by the server and if the information does not match for some reason, the whole registry information is fetched again.
4. Eureka server caches the payload of the deltas, whole registry and also per application
Cancellation:
1. Eureka client sends a cancel request to Eureka server on shutdown.
2. This removes the instance from the server's instance registry thereby effectively taking the instance out of traffic.
Time lag:
1. All operations from Eureka client may take some time to reflect in the Eureka servers and subsequently in other Eureka clients.
2. This is because of the caching of the payload on the Eureka server which is refreshed periodically to reflect new information.

3. Eureka clients also fetch deltas periodically. Hence, it may take up to 2 minutes for changes to propagate to all Eureka clients.
Other ways:
Ribbon Configuration ref:
https://github.com/Netflix/ribbon/blob/3a707ec518a896053be44266dddc4c4f925f4e60/ribbon-core/src/main/java/com/netflix/client/config/DefaultClientConfigImpl.java#L331


More refference links:
Eureka default configuration:
https://github.com/Netflix/eureka/blob/master/eureka-client/src/main/java/com/netflix/discovery/EurekaClientConfig.java

https://github.com/Netflix/eureka/blob/master/eureka-client/src/main/java/com/netflix/discovery/DefaultEurekaClientConfig.java

https://github.com/Netflix/eureka/blob/master/eureka-client/src/main/java/com/netflix/appinfo/MyDataCenterInstanceConfig.java

https://github.com/Netflix/eureka/blob/master/eureka-core/src/main/java/com/netflix/eureka/DefaultEurekaServerConfig.java

https://www.npmjs.com/package/eureka-js-client

http://programtalk.com/java-api-usage-examples/com.netflix.discovery.DefaultEurekaClientConfig/

https://insight.io/github.com/Netflix/eureka/blob/master/eureka-client/src/main/java/com/netflix/appinfo/DataCenterInfo.java

https://spring.io/blog/2015/01/20/microservice-registration-and-discovery-with-spring-cloud-and-netflix-s-eureka


Read more ...

My Favorite Site's List

#update below script more than 500 posts