Home Navigation

Wednesday 12 September 2018

no message body reader has been found for class contenttype application/json, Apache CXF rest client

The error means it needs a JSONProvider or a JSON parser in its code

Option 1: Create a JSON parser

WebClient client = WebClient.create(ENDPOINT_URL);
Response r = client.accept("application/json").get();       
MappingJsonFactory factory = new MappingJsonFactory();
JsonParser parser = factory.createJsonParser((InputStream)r.getEntity());
YourModelClass model= parser.readValueAs(YourModelClass.class);

Option 2: Register a JSON provider

List<Object> providers = new ArrayList<>();
providers.add(new JacsksonJsonProvider()); // or you can register your own provider
WebClient client = WebClient.create(ENDPOINT_URL,providers);

Tuesday 11 September 2018

Microservices cheat sheet at a glance

Config server:
Provides centralized, externalized, secured, easy to reach source of application configuration. Designates a centralized server to serve-up configuration information ( configuration read from source control)
Clients connect over http and retrieve their configuration settings, in addition to their own, internal sources of configuration.

    Configuration options:

  • Package configuration files with application, if any changes in configuration, it requires build and restart
  • Configuration file in a common file system, Unavailable in cloud
  • Use environmental variable
                - Done differently on different environment
- If large hard to manage and may duplicate occurances

  • Use a cloud vendor specific solutions

      - Coupling application to a specific environment, ex cloud foundry

Other challenges to consider
- Large number of microservics
- Dynamic updates
- version control

desired configuration server would be:
- platform/cloud independent solutions
- centralized
- dynamic
- controllable (scm)
- passive (self registering)

What if configuration sever is down?
      - Spring cloud config server should typically run on multiple server instances
      - Client application can control policy of how to handle missing config server
- spring.cloud.config.failFast=true, default is false
      - config server override the local config, probably there will be a policy to backup config.

Spring cloud bus:
provides simple way to notify clients to config changes

Service Discovery

  • Microservices architectures result in large numbers of inter-service calls which is very challenging to configure
  • How can one application easily find all of the other runtime dependencies? Manual configuration impractical

Service discovery provides a single lookup service, client register themselves, discover other registrants. Its like mainlining an automatic dynamic list.
Example: Eureka, Consul, Etcd, Zookeeper, SmartStack etc

Typically runs in a multiple server. Client sends heart beat. Multiple profiles.

Client-side load balancer:

Traditional load balancers are server-side components

  • Distribute incoming traffic among serveral servers
  • Software: Apache, Nginx, HA Proxy: hardware F5, NSX, BigIP

Client-side load balancer, client side load balancer select which server to call
                Based on some criteria
                part of client software
                Server can still employ its own load balancer
Why?? some may be unavailable, some slower and some further away

Spring Cloud Netflix Ribbon is a part of Netflix OSS family a client side load balancer
        - Automatically integrates with service discovery (Eureka)
        - Build in failure resiliency (Hystrix)
        - Caching / Batching
        - Multiple protocols (HTTP, TCP, UDP)
        - Spring cloud provides an easy API Wrapper for using Ribbon

Ribbon gets the server list from a static source defined in application.yml or from service discovery
How Ribbon decides

  • Ping: used to test if server is up or down
  • Spring cloud default - delegate to Eureka to determine if server is up or down.
  • Ribbon load balancer itself: The load balancer is the actual component that routes the call to the server in the filtered list
  • Several strategies available, but they usually defer to a rule component to make the actual decisions
  • Spring cloud's default : ZoneAwareLoadBalancer

Feign:
  • Declarative REST client from Netflix
  • Allows you to write calls to REST services with no implementation code
  • Alternative to RestTemplate 
  • Spring cloud provides easy wrapper for using Feign
Cascading Failure:

Hystrix
  • Hystrix part of netflix  oss
  • Detects failure conditions and opens to disallows further calls
  • Identify fallback, fallback can be chained
  • Automatically closes itself after interval
Circuit Breaker: Hystrix
  • Closed when operating normally
  • Open when failure is detected
  • Failure definition flexible , exception thrown or timeout exceeded over time period.
  • Definable "Fallback" options
  • Automatically re-closes itself
Api Gateway:
     - Build for specific client needs ("facade")
     - Reduces remote call
     - Routes calls to specific servers
     - Handles security / SSO
     - Handles caching
     - Protocol Translation
     - Optimizes calls / Link Expansion

     Spring cloud Zuul
           - Zuul JVM-based router and load balancer
           - Can be used for many API Gateway needs
           - Routing - send request to real server ( Reverse proxy)

     Is Zuul an API Gateway?
  - Zuul is a tool for creating an API Gateway
  - Specifically routing

              What parts are missing?
   - Caching, Protocol translation, resource expansion / Link resolution

       Spring caching abstraction:
Annotate method with @cachable
Describe cache and key
Define a cacheManager
Backed by synchronizedMaps, EHCache, Gemfire, etc.

       Some shortcoming
- Cache value might expire or might not exist after some time.

       ETags: shallow etag, intended to save bandwidth not server work, network load
Modern http-based caching better than expires
- client requests resources
- server returns resource with etag
Hash value calculated from content
- Client sends if-none-match header with etag value whenever requesting the same resource
- server calculates new hash
if it matches, return 304
if not, return 200 new content, new Etag
       
         RestTemplate does not have caching built in but HttpClient does!
         Fiegn doesn't not have caching capability, consider creating your own aspect for use with Fiegn