Friday, October 5, 2018

Reflection in Java

Reflection in Java


In computer science, a Reflection is the ability of a computer program to examine, introspect, and modify its own structure and behavior at runtime:
https://en.wikipedia.org/wiki/Reflection_(computer_programming)#Java

The following snippet is an example of a Reflection in Java:

// Without a Reflection
Book eBook = new Book();
eBook.readLoudly();

// With the Reflection
import java.lang.reflect.*;
(...)
Object eBook = Class.forName("eu.microwebservices.awesomeappproject.model.Book").newInstance();

// Alternatively: Object eBook = Book.class.newInstance();
import java.lang.reflect.*;
(...)
Method meth = eBook.getClass().getDeclaredMethod("readLoudly", new Class[0]);
meth.invoke(eBook);

Class book = Class.forName("eu.microwebservices.awesomeappproject.model.Book")

int bookMods = book.getModifiers();

assertTrue(Modifier.isAbstract(bookMods));
assertTrue(Modifier.isPublic(bookMods));

Important Principles with creating microservices

Important Principles with creating microservices


Important Principles with creating microservices...


As we can read at
https://dzone.com/storage/assets/3259596-dzonerefcardz-gettingstartedmicroservices.pdf
both architectures: a monolith one and microservices should be designed following good software architecture principles. So before our BIG EXCHANGE from old architecture to microservices we must consider following principles:

1. Separate your concerns. 
Use MVC and well-defined APIs for high cohesion and low coupling.

2. Use Convention over Configuration (CoC). 
Keep configuration to a minimum by using or establishing conventions.

3. Follow the Law of Demeter. Each unit should have only limited knowledge about other units: only units "closely" related to the current unit. Each unit should only talk to its friends.
Do not talk to strangers! Only talk to your immediate friends.

4. Use Domain-Driven Design. 
Keep objects related to a domain/component together.

5. Focus on Automation. 
Automate testing and fully automated deployment pipeline.

6. Design to Interfaces / APIs.
Classes shouldn’t call other classes directly just because they happen to be in the same archive. Such group of friendship could be later one microservice...

7. Group code by functionality, not by layer. 
Monoliths usually have a layered architecture, and it’s common to group classes by layer. Instead you should break up your code into packages separated by functionalities to allow easier splitting into future microservices. It also makes easier to see the dependencies between your future microservices.

8. Make your code stateless and externalize your application’s state. 
One cannot rely on static variables or global variables as the source of your application’s state. Use external data sources such as key-values stores or databases to maintain state.

Known problems:

  1. Sharing
  2. Asynchronicity
  3. Security
  4. Simplicity
  5. Evolution
  6. Health

How can we create a docker image?
Fast... 
from dockerfile.

Where can we create a docker image from dockerfile?
On AWS.

Ready, steady, go!

The docker container with 3 main "beasts": Java, Tomcat and Alpine/Centos Linux... - very useful information we can read at:

Discovery service for microservices

Discovery service for microservices

.

"The hell" of accounts, permissions, IAM roles and grants on AWS

"The hell" of accounts, permissions, IAM roles and grants on AWS

"The hell" of accounts, permissions, IAM roles and grants on AWS...

1.
IAM stands for Identity & Access Management. 

2.
How do I add users to AWS?
Sign in to the AWS Management Console and open the IAM console at https://console.aws.amazon.com/iam/.
In the navigation pane, choose Users and then choose Add user.
Type the user name for the new user. ...
Select the type of access this set of users will have. ...
Choose Next: Permissions.

3.
Simply speaking:

We may have many Users on AWS Account.
A User may have policies.
A policy uses IAM service role.
We can add IAM permissions to a User and grant access in order to allow to run actions on AWS services...

See more:
4.


Amazon Elastic Container Service (ECS) is a highly scalable, high performance container management service that supports Docker containers and allows you to easily run applications on a managed cluster of Amazon EC2 instances. Amazon ECS eliminates the need for you to install, operate, and scale your own cluster management infrastructure. With simple API calls, you can launch and stop container-enabled applications, query the complete state of your cluster, and access many familiar features like security groups, Elastic Load Balancing, EBS volumes and IAM roles. You can use Amazon ECS to schedule the placement of containers across your cluster based on your resource needs and availability requirements. You can also integrate your own scheduler or third-party schedulers to meet business or application specific requirements.

Kubernetes vs. Docker

Kubernetes vs. Docker



only root has permitions:
/usr/bin/kubectl

/usr/bin/dockerd

--

docker pull gerardorosiles/amazonlinux-tomcat85

from:
https://hub.docker.com/r/gerardorosiles/amazonlinux-tomcat85
integrates tomcat 8.5.24 to amazonlinux with jdk8_152

(a modification of https://hub.docker.com/r/bpatterson/centos7-tomcat85/ )


docker pull gerardorosiles/amazonlinux-tomcat85

docker images

docker run gerardorosiles/amazonlinux-tomcat85

internal:
docker run -it gerardorosiles/amazonlinux-tomcat85

Kubernetes orchestrations

Kubernetes orchestrations

.

Microservices are awesome

Microservices are awesome

Microservice architecture (MSA) is a logical structure for the design of a software program involving loosely-coupled modular components known as microservices.

Microservices (https://en.wikipedia.org/wiki/Microservices) are a software development technique - a variant of the service-oriented architecture (SOA) architectural style that structures an application as a collection of loosely coupled services

In a microservices architecture (MSA), services are fine-grained and the protocols (based on HTTP methods) are lightweight. The benefit of decomposing an application into different smaller services is that it improves modularity

This makes the application easier to understand, develop, test, and become more resilient to architecture erosion than it is possible ...with Monolithic applications.
It parallelizes development by enabling small autonomous teams to develop, deploy and scale their respective services independently.

Microservices have a lot of advantages with a large "statistical scale", but...
there is also a criticism of the microservices approach for a number of issues e.g.:
  • Testing and deployment are more complicated.
  • The whole complexity (from monolithic one) is moved to the architectural organization, a larger amount of interfaces and network traffic.
  • Microservices require a lot of additional software to control and react with failures and errors, because of distributed business goals and autonomous (loosely coupled) services.
  • Information barriers occurs in processes.
  • Inter-service calls over a network have a higher cost in terms of network latency and message processing time than in-process calls within a monolithic service process.


See more: