페이지

2020년 5월 10일 일요일

What is Kubernetes?

In 2017, Docker adoption was up 40%
amongst Datadog's very large customer base
source: "8 Surprising facts About Real Docker Adoption." Datadog, April 2017

The median number of containers running on a single host is aboput 10.
Source: "The 2017 Dockedr Usage Report." Sysdig, April 17, 2017

How do you manage all these containers running on a single host, and across your whole infrastructure?

Orchestrator Features 
* Provision hosts
* Instantiate containers on a host
* Restart failing containers
* Expose containers as services outside the cluster
* Scale the cluster up or down

Kubernetes(K8s)
* Definition: an open-source platform designed to automate deploying, scaling, and operating application containers
* Goal: to foster an ecosystem of components and tools that relieve the burden of running applications in public and private clouds 

Borg was the predecessor to Kubernetes.

Kubernetes is a platform to schedule and run containers on clusters of virtual machines. It runs on bare metal, virtual machines, private datacenter and public cloud.

No golden handcuffs when migrating to the cloud

Kubernetes and Docker
Kubernetes is a container platform

You can use Docker containers to develop and build applicastions, and then use Kubernetes to run these applications on your infrastructure.

Features Kubernetes
"Kubernetes is an open source project that enables software teams of all sizes, from a small startup to a Fortune 100 comapny, to automate deploying, scaling, and managing applications on a group or cluster of server machines..."
"These applications can include everything from internalfacing web applications like a content management system to marquee web properties like Gamil to big data processing."
- Joe Beda

Multi-Host
Container Scheduling
* Done by the kube-scheduler
* Assigns pods to nodes at runtime
* Checks resources, quality of service, policies, and user specifications before scheduling


Scalability and Availability
* K8s master can be deployed in a highly available configuration
* Multi-region deployments available

Scalability (v 1.8)
* Supports 5,000 node clusters
* 150,000 total pods
* Pods can be horizontally scaled via API

Flexibility and Modularization
* Plug-and-play architecture
* Extend architecture when needed
* Add-ons: network dirvers, service3 discovery, container runtime, visualization, and command

Registration
Seamless nodes register themselves with master

Service Discovery
Automatic detection of services and endpoints via DNS or environment variables.

Persistent Storage
* Much requested and important feature when working with containers
* Podc can use persistent volumes to store data
* Data retained across pod restarts and crashes




















 

2020년 5월 9일 토요일

What Is Containerization?

1. History of Containers


Container:
A collection of software processes unified by one namespace, with access to an operating system kernel that ist shares with other containers and little to no access between containers.


Dociker Instance
A runtime instance of a Docker image contains three things:
1. A Docker image
2. An execution environment
3. A standard set of instructions



Virtual Machine (VM)
* One or many applications
* The necessary binaries and libraries
* The entire guest operating system to interact with the applications

Containers
* Include the application and all of its dependencies
* Share the kernel with other containers
* Not tied to infrastructure only needs Docker Engine installed on the host
* Run as isolated processes in user space on the host OS

Container Benefits for Developers
Applicaton are
1. Portable
2. Packaged in a standard way

Deployment is 
1. Easy
2. Repeatable

Container Benefits for Developers
* Automated testing, packaging, and integrations
* Support newer microservice architectures
* Alleviate platform compatibility issues

Container Benefits for DevOps
* Reliable deployments: improve speed and frequency of releases
* Consistent application lifecycle: configure once and run multiple times

Container Benefits for DevOps

Consistent environments
* No more process differences between dev and production environments

Simple scaling
* Fast deployments ease the addition of workers and poermit workeload to grow and shrink for on-demand use cases


The DevOps team can isolate and debug issues at the container level.

Use of Containerized Apps on the Rise

Among 195 organizations surveyed in January 2017, organizations expect that their number of containerized applications will rise by 80% in the next tow years.

Containers: Real Adoption And Use Cases In 2017 (March 2017), Forrester Consulting Thought Leadership Paper Commissioned by Dell EMC, Intel, and RedHat

Containers and Microservices

Allow the Building of Pipelines

* Containers bring agility toi your code
* Help build a continuous integration and deployment pipeline
* Push an IT team to develop, test, and deploy applications faster










 




















2020년 5월 5일 화요일

Spring Cloud

Addressing the issues found in cloud-native applications

Module Outline

1. Spring / Spring IO / Spring Cloud
2. Spring Cloud Netflix
3. Common Concepts

Spring Cloud Origins

1. First, there was the Spring Framework (2004)
  - Alternative to low-level JEE approaches

2. Next, Spring sub-projects emerged (2006 - present)
  - Spring Security, Web Flow, Integration, Batch, Web Services, XD, Social, Data, Boot, Session, etc.
  - Organized under Spring IO umbrella:
 

Spring Could Subproject
1. "Sub-Umbrella" Project within Spring IO Platform


Goal of Spring Cloud

1. Provide libraries to apply common patterns needed in distributed applications
  - Distributed / Versioned / Centralized Configuration Management
  - Service Registration and Discovery
  - Load Balancing
  - Service-to-service Calls
  - Circuit Breakers
  - Routing
  - ....

Where Does NETFLIX Fit Into All of This?

1. Netfliz reinvented itself since early 2007
   - Moved from DVD mailing to video-on-demand
      * Once USPS largest first-class customer
      * Now biggest source of North American Internet traffic in evenings.

2. Became Trailbazers in Cloud Computing
   - All Running on Amazon Web Services

3. Chose to publish many general-=user technologies as Open-Source projects
   - Proprietary video-streaming technologies are still secret.

Spring and NETFLIX
1. The Spring Team has always been forward looking
  - Trying to Focus on Applications of Tomorrow

2. Netflix OSS Mature and Battle-Tested; Why Reinvent?

3. Netflix OSS Not Necessarily Easy and Convernient
  - Spring Cloud provides easy interaction
     * Dependencies
     * Annotations


Spring Cloud Setup
1. Spring Cloud Projects are all based on Spring Boot
  - Difficult to employ using only core Spring Framework
  - Dependency management based on Boot
  - ApplicationContext startup process modified


Server vs. Client

1. "Client" and "Server" are relative terms
  - Based on the role in a relationship
  - A Microservice is ofter a client and a server

2. Don't get lost on the terminology!


Required Dependenies

1. Replace Spring Boot Parent
  - Spring Cloud proejct are based on Spring Boot


org.springframework.cloud
spring-cloud-starter-parent
Angel.SR4



org.springframework.cloud
spring-cloud-start-...



2. ... OR Use Dependency Management Section




org.springframework.cloud
spring-boot-starter-parent
Angel.SR4
pom
import





org.springframework.cloud
spring-boot-starter-...


Summary
1. Spring Cloud is a sub-project within Spring IO Umbrella
  - And is itself an umbrella project.

2. Spring Cloud addresses common patterns in distributed computing
3. Spring Cloud enables easy use of Netflix libraries
4. Spring Cloud i based on Spring Boot


Spring Cloud Config

Centralized, versioned configuration management for distributed applications


Objectives

1. At the end of this module, you will be able to
  - Explain what Spring Cloud Config is
  - Build and Run and spring Cloud Config Server
  - Establish a Repository
  - Build, Run, and Configure a Client

Module Outline

1. Configuration Management
   - Challenges
   - Desired Solution

2. Spring Cloud Config
   - Server Side
   - Client Side

3. Repository Organization

What is Application Configuration?

1. Application are more than just code
  - Connections to resources, other applications

2. Usually use external configuration to adjust software behavior
  - Where resources are located
  - How to connect to the DB
  - Etc.

Configuration Options

1. Package configuration files with application
  - Requires rebuild, restart

2. Configuration files in common file system
  - Unavailable in cloud

3. Use environment variables
  - Done differently on different platforms
  - Large # of individual variables to manage / duplicate

4. Use a cloud-vendor specific solution
  - Coupling application to specific environment

Other Challenges

1. Microservices -> large # of dependent services    <--- brittle="" manual="" p="" work="">
2. Dynamic updates
  - Changes to services of environment variables require restage of restart  <-- activities="" deployment="" p="">
3. Version control   <-- p="" traceablity="">

Desired Solution for Configuration

1. Platform / Cloud-Independent solution
  - Language-independent too

2. Centralized
  - Or a few discrete sources of our choosing

3. Dynamic
  - Ability to update settings while an application is running

4. Controllable
  - Same SCM choices we use with software

5. Passive
  - Services (Applications) should do most of the work themselves by self-registering

Solution:
1. Spring Cloud Config
  - Provides centralized, externalized, secured, easy-to-reach source of application configuration

2. Spring Cloud Bus
  - Provides simple way to notify clients to config changes

3. Spring Cloud Netflix Eureka
  - Service Discovery - Allows applications to register themselves as clients

Spring Cloud Config
1. Designates a centralized server to server-up configuration information
  - Configuration itself can be backed by source control

2. Clients connect over HTTP and retrieve their configuration settings
  - In addition to their own, internal sources of configuration






Spring Cloud config Server

1. Source available at GitHub:
https://github.com/spring-cloud-samples/configserver

2. Or, it is reasonably easy to build your own

Spring Cloud Config Server _Building, part 1

1. Include minimal dependencies in your POM(or Gradle)
  - Spring Cloud Starter Parent
  - Spring Cloud Config Server


org.springframework.cloud
spring-cloud-starter-parent
Angel.SR4




org.springframework.cloud
spring-cloud-config-server




Spring Cloud Config Server  _ Building, part 2

1. application.yml - indicates location of configuration repository

---
spring:
   cloud:
      config:
         server:
            git:
              uri: https://github.com/kennyk65/              searchPaths:ConfigData


   - ...or application.properties

Spring Cloud Config Server _ Building, part 3

1. Add @EnableConfigServer

   @SpringBootApplication
   @EnableConfigServer
   public class Applicaton{
   
         public static void main(String[] args){
               SpringApplication.run(Application.class, args);
         }
    }


2. That's it!

The Client Side - Building part 1

1. Use the Spring Cloud Starter parent as a Parent POM:


   org.springframework.cloouod
   spring-cloudstarter-parent
   Angel.SR4


2....OR use a Dependency management section:

 
     
         
           org.springframework.cloouod
           spring-cloud-starter-parent
           Angel.SR4
           pom
           import
       
   
 


The Client Side - Building Part 2

1. Include the Spring Cloud Starter for config:


           org.springframework.cloouod
           spring-cloud-starter-config


2. Configure application anme and server location in bootstrap.properties / yml

   - so it is examined early in the startup process
   # bootstrap.properties:
      spring.application.name: lucky-word
      spring.cloud.config.uri:  http://localhost:8001

3. That's it!
  - Client connects at startup for additional configuration settings.

EnvironmentRepository - Choices

1. Spring Cloud Config Server uses an
  EnvironmentRepository
    - Two implementations available: Git and Native (local files)

2. Implement EnvironmentRepository to use other sources.

Environment Repository - Organization

1. Configuration file naming converntion:
  - -.yml
     * Or .properties (yml takes precedence)

  - spring.application.name = - set by client application's bootstrap.yml(or .properties)
  - Profile - Client's spring.profiles.active
    * (set various ways)

2. Obtain settings from server:
   - http://://
   - String client do this automatically on startup

The Client Side
1. Spring Boot Applications: Include:
   - Spring cloud client dependency
   
       
org.springframework.cloud
        spring-cloud-starter
     

   - Configure application name and server location in bootstrap .properties /yml
      * So it is examined early in the startup process
       # bootstrap.properties:
       spring.application.name:lucky-world
       spring.cloud.config.uri: http://localhost:8888

2. That's It!
   - Client connects at startup for additional configuration settings.

Environment Repository - Organization Example

1. Assume client application named "lucky-world" and profile set to "northamerica"
  - Spring client (automatically) requests
    * /luck-word/northamerica

 lucky-word-default.yml                     <-- ignored="" is="" p="" profile="" set=""> lucky-word.yml                               <-- included="" p="" prededent="" second=""> lucky-word-northamerica.yml             <-- first="" included="" p="" precedent=""> lucky-word-europe.yml                     <-- diffrent="" ignored="" p="" profile="" set=""> lucky-word.properties                      <-- included="" p="" precedent="" third=""> another-app.yml                             <-- app="" diffrent="" ignored="" p="">
.yml vs .properties

1. Settings can be stored in either YAML or standard Java properties files

   - Both have advantages
   - Config server will favor .yml over .properties

# .properties file
spring.config.name=aaa
spring.config.location=bbb
spring.profiles.active=ccc
spring.profiles.include=ddd


# .yml file
-----
spring:
  config:
     name: aaa
     location:  bbb
  profiles:
    active: ccc
    include: ddd

Profiles
 
  1. YAML Format can hold multiple profiles in a single file
   
      # lucky-word-east.proerties
      luck-word: Clover

      # lucky-word-west.properties
      luck-word: Rabbit's Foot

     # luckyword.yml
     ---
     spring:
        profiles: east
     lucky-word: Clover
   
    ---
     spring:
       profiles:west
    lucky-word: Rabbit's Foot


The Client Side

1. How Properties work in Spring Applications
   - Spring apps have an Environment object
   - Environment object contains multiple PropertiySources
      * Typically populated from environment variables, system properties, JNDI, developer-specified property files, etc.
   - Spring Cloud Config Client library simply adds another PropertySource
      * By connecting to server over HTTP
      * http://://

   - Result: Properties descried by server become part of client application's environment

What about non-Java / non-Spring Clients?

1. Spring Cloud Server exposes properties over simple HTTP interpace
    - http://://

2. Reasonably easy to call server from any application
    - Just not as automated as Spring

What if the Config Server is Down?

1. Spring Cloude Config Server should typically run on serveral instances
   - So downitme should be a non-issue

2. Clinet application can control policy of how to handle missing config server
   - spring.cloud.config.failFast=true
   - Deafult is false

3. Config Server settings override local settings
   - Strategy: provide local fallback settings.

Spring Cloud Ribbon
1. Understanding and Using Ribbon, The clinet side load balancer

Objectives
1. At the end of this module, you will be able to
- Understand the purpose of Client-Side Load Balancing
- Use Spring Cloud Ribbon to implement Client-Side Load Balancing

What is a Load Balancer?

1. Traditional load balancers are server-side components
  - Distribute incoming traffic among serveral servers
  - Software (Apache, Nginx, HA Proxy) or Hardware(F5, NSX, BigIP)




Clinet-Side Load Balancer

1. Clinet-Side Load Balancer selects which server to call
   - Based on some criteria
   - Part of client software
   - Server can still employ its own load balancer




Why?

1. Not all servers are the same
  - Some may be unavaliable(faults)
  - Some may be slower than other (performance)
  - Some may be further away than others (regions)




Module Outline
1. Clinet Side Load Balancing
2. Spring Coud Netflix Ribbon

Spring Cloud Netflix Ribbon
1. Ribbon - Another part of the Netflix OSS family
  - Clinet side load balancer
  - Automatically integrates with service discovery (Eureka)
  - Built in failure resiliency (Hystrix)
  - Caching / Batching
  - Multiple protocols (HTTP, TCP, UDP)
2. Spring Cloud provides an easy API Wrapper for using Ribbon.

Key Ribbon Concepts

1. List of Servers
2. Filtered List of Servers
3. Load Balancer
4. Ping

List of Servers
1. Determines what the list of possible servers are (for a given service (client))
   - Static - Populated via configuration
   - Dynamic - Populated via Service Discovery ( Eureka )

2. Spring Cloud default - Use Eureka when present on the classpath.


Filtered List of Servers

1. Criteria by which you wish to limit the total list
2. spring Cloud default - Filter servers in the same zone

ping

1. Used to test if the server is up or down
2. Spring Cloud default - delegate to Eureka to determine if server is up or down

Load Balancer

1. The Load Balancer is the actual component that routes the calls to the servers in the filtered list

2. Serveral strategies available, but they usually defer to a Rule component to make the actual decisions

3. Spring Cloud's Default: ZoneAwareLoadBalancer


Rule

1. The Rule is the single module of intelligence that makes the decision on whether to call or not.
2. Spring Cloud's Default: ZoneAvoidanceRule

Using Ribbon with Spring Cloud - part 1

1. Use the Spring Cloud Starter parent as a Parent POM:

  org.springframework.cloud
  spring-cloud-starter-parent
  Angel.SR4



2. ... OR use a Dependency management section:


   
     
         org.springframework.cloud
         spring-cloud-starer-parent
         Angel.SR4
         pom
         import
     
 


...exactly the same options as a spring cloud config client or a spring cloud eureka client.

Using Ribbon with Spring Cloud - part 2

1. Include dependency:
   
     
          org.springframework.cloud
          spring-cloud-starter-ribbon
     
 

Using Ribbon with Spring Cloud - part 3

1. Low-level technique:
  - Access LoadBalancer, use directly:

   public class MyClass{
       @Autowired LoadBalancerClient loadBalancer;

       public void doStuff(){
            ServiceInstance instance = loadBalancer.choose("subject");
            URI subjectUri = URI.create(String.format("http://$s:%s", instance.getHost(), instance.getPort());
            // ... do something with the URI
      }
}

API Reference
1. Previous example used Ribbon API directly
2. Not desirable - couples code to Ribbon
3. Upcoming examples will show declarative approach
  - Feign, Hystrix.

Customizing
1. Previously we escribed the deaults. What if you wnat to change them?
2. Declare a separate config with replacement bean.

@Configuration
@RibbonClient(name="subject", configuration=SubjecConfig.class)
public class MainConfig{
}


@Configuration
public class SubjectConfig{
   @Bean
   public IPing ribbonPing(IClientConfig config){
       return new PingUri();
  }
}

What Customizing Choices are available
1. Quite a Few!
- Recommend looking at the JavaDoc or GitHub Code


Summary
1. Client-Siode Load Balancing augments regular load
balancing by allowing the client to select a server based on some criteria.

2. Spring Cloud Riboon is an easy-to-use implementation of client side load balancing.

Spring Cloud Feign

Declarative REST Client

Objectives
1. At the end of this module, you will be able to
  - Call REST services using the Feign libraries
  - Understand how Feign, Ribbon, and Eureka collaborate

Module Outline
1. What is Feign
2. How to use Feign

Feign
1. What is it?
  - Declarative REST client, from NetFlix
  - Allows you to write calls to REST services with no implementation code
  - Alternative to Rest Template (even easier!)
  - Spring Cloud provides easy wrapper for using Feign

Spring REST Template
1. Spring's Rest Template provides very easy way to call REST services

RestTemplate template = new RestTemplate();
String url = "http://inventoryService/{0}";
Sku sku = template.getForObject(uri, Sku.class, 4724352);

2. Still, this code must be
  1) Written
  2) Unit-tested with mocks / stubs.

Feign Alternative - Declarative Web Service Clients
1. How does it work?
  - Define interfaces for your REST client code
  - Annotate interface with Feign annotation
  - Annotate methods with Spring MVC annotations
    * Other implementations like JAX/RS pluggable

2. Spring Cloud will impleent it at run-time
  - Scans for interfaces
  - Automatically implements code to call REST service and process response


Feign Interface
1. Create an Interface, not a Class:



Note: No extra dependencies are needed for Feign when using Spring Cloud.

Runtime Implementations

1. Spring scans for @FeignClients
   - Provides implementations at runtime



2. That's it!
   - Implementations provided by Spring / Feign!

What does @EnableFeignClients do?

You can @Autowire an InventoryClient wherever one is needed


Ribbon and Eureka  _ Where do they fit in?

1. The previous example - hard-codeedURL
    @FeignClient(url="localhost:8080/warehouse")

2. ...use a Eureka "Client ID" instead:
     @FeignCliuent("warehouse")

3. Ribbon is automatically enabled
    - Eureka gives our application all "Clients" that match the given Client ID
    - Ribbon automatically applies load balancing
    - Feign hanldes the code.

Runtime Dependency

1. Feign starter required at runtime:
    ...but not compile time
     
         
             org.springframework.cloud
             spring-cloud-starter-feign
       
   

Summary
1. Feign provides a very easy way to call RESTful services
2. Feign integrates with Ribbon and Eureka automatically.


Spring Cloud Hystrix

Understanding and Applying Client Side Circuit Breakers

Objectives
1. At the end of this module, you will be able to
   - Understand how software circuit breakers protect against cascade failure
   - Use Spring Cloud Netflix Hystrix annotations within your software to implement circuit breakers
   - Establish simple monitoring of Circuit Breakers using Hystrix Dashboard and Turbine

Module Outline
1. Cascading Failures and the Circuit Breaker Solution
2. Using Spring Cloud Netflix Hystrix
3. Monitoring with the Hystrix Dashboard and Turbine


The Problem: Cascading Failure
1. Having a large number of services as dependencies can lead to a 'cascading failures'
2.Without mitigating this, microservices are a recipe for certain disaster!



Distributed Systems - More Failure Opportunities

1. Distributed systems -> more opportunity for failure.
   - Remember tghe Fallacies of Distributed Computing.

2. The Math: Assume 99.95% Uptime (Amazon EC2 SLA)
  - Single app - 22 minutes down per montjhe
  - 30 interrelated services - 11 hours downtime per month ( bad )
  - 100 interrelated services - 36 hours downtime per month ( ouich! )

The Circuit Breaker Pattern

1. Consider a household circuit breaker
  - It "watches" a circuit
  - When failure occurs ( too much current flow ), it "opens" the circuit (disconnects the circuit )
  - Once problem is resolved, you can manually "close: the breaker by flipping the switch.
  - Prevents cascade failure
   * i.e. - your house burning down.

Hystrix - The Software Circuit Breaker

1. Hystrix - Part of Netflix OSS
2. Light, easy-to-use wrapper provided by Spring Cloud.
3. Detects failure conditions and "opens" to disallows further calls
   - Hystrix Default - 20 failures in 5 seconds
4. Identify "fallback" - what to do in case of a service dependency failure
  - Think: catch block, but more sophisticated
  - Fallbacks can be chained
5. Automatically "closes" itself after interval
  - Hystrix Default - 5 seconds.


Comparison with Physical Circuit Breaker




Hystrix  ( Spring Cloud ) Setup

1. Add the Dependency:
 
    org.springframework.cloud
     spring-cloud-starter-hystrix
 

2. Enable Hystrix within a configuration class:
@SpringBootApplication
@EnableHystrix
public class Application {
}

























 








   
   













2020년 4월 2일 목요일

Spring Boot





  • What is Spring Boot
  1. Radically faster getting started experience
  2. "Opinionated" approach to configuration / defaults
       - Intelligent defaults
       - Gets out of the way quickly


    3. What does it involve?
       - Easier dependency management
       - Automatic configuration / reasonable defaults
       - Different build / deployment options.


  • What Spring Boot is NOT
  1. Plugins for IDEs
       - Use Boot with any IDE(or none at all)

    2. Code generation

___________________________________________________________________________
Demonstration - Spring Boot

Create a new, bare-bones Spring application

eclipse
Help -> eclipse marketplace -> spring ide search -> Spring Tools 4 (aka Spring Tool suite 4) 4.6.0. RELEASE -> install -> restart

Project Explorer right click -> new -> other -> Spring Boot -> Spring Starter Project

name: microserviceBoot, java Version 8 -> next -> next


"Tomcat version 8.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 and 6 WEb modules"
project foloer .settings>org.eclipse.wst.common.project.facet.core.xm

          

 
 
 
 
 
  4.0
"/>  => 2.5
 


____________________________________________________________________________

  • Spring Boot - What just Happened?
  1. Boilerplate project structure created
      - Mostly folder structure
      - "Application" class + test
      - Maven POM (or Gradle if desired)

    2. Dependency Management


  • Running Spring Boot-What Just Happened?
  1. SpringApplication
        - Created Spring Application Context

    2. @SpringBootApplication
       - Combination of @Configuration
           i) Marks a configuration file
           ii) Java equivalent of file
       
       - ...And @ComponentScan
           i) Looks for @Componenrts (none at the moment)

       - ...And @EnableAutoConfiguration
           i) Master runtime switch for Spring Boot
           ii) Examines ApplicationContext & classpath
           iii) Creates missing beans based on intelligent defaults


____________________________________________________________________________________
Demonstration - Adding Web Capability

- Adding spring-boot-starter-web dependency
- Adding HelloController

pom.xml
spring-boot-starter => spring-boot-starter-web

com.example.demo package
create classes => WhateverIWant

package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class WhateverIWant {

@RequestMapping("/hi")
public @ResponseBody String hiThere()
{
return "hello world!";
}

}


Run As -> Spring Boot App

____________________________________________________________________________________

  • Adding Web - What Just Happened?
  1. spring-boot-starter-web Dependency
       - Adds spring-web, spring-mvc jars
       - Adds embedded Tomcat jars

    2. When application starts...
       - Your beans are created
       - @EnableAutoConfiguration looks for  'missing' beans
          i) Based on your beans + classpath
          ii) Notices @Controller / Spring MVC jars
     
       - Automatically creates MVC beans
          i) DispatcherServlet, HandlerMappings, Adapters, ViewResolvers

       - Launches embedded Tomcat instance.



  • But wait, I want a WAR...
  1. To Convert from JAR to WAR:
      - Change POM packaging
      - Extend SpringBootServletInitializer

 


   2. Deploy to app server
      - URL becomes http://localhost:8080//

_______________________________________________________________________________________

Demonstration - WAR Deployment

-WAR Packaging
-SpringBootServletInitializer


pom.xml

element 
 jar -> war
 or add war


MicroservicesBootApplication.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class MicroservicesBootApplication extends SpringBootServletInitializer{


/**
* Used when run as a JAR
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(MicroservicesBootApplication.class, args);
}

/**
* Used when run as a WAR
*/
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {

return builder.sources(MicroservicesBootApplication.class);
}



}

________________________________________________________________________________________

  • What about Web Pages?
  1. Spring MVC supports a wide range of view options
  2. Easy to use JSP, Freemarker, Velocity, Thymeleaf
  3. Boot automatically establishes defaults
      - InternalResourceViewResolver for JSPs
      - ThymeleafViewResolver
         i) If Thymeleaf is on the classpath
    
     4. spring-boot-starter-thymeleaf

___________________________________________________________________________________________

Demonstration - Thymeleaf web pages

- spring-boot-starter-thymeleaf
- /templates folder
- Controller adjustments
- Web page


pom.xml

add


org.springframework.boot
spring-boot-starter-thymeleaf

search google  => spring boot reference

spring Boot Reference Guide

spring-boot-starter-thymeleaf 

resource add Folder
name: templates

add hello.html




Hello name-goes-heae from a Thymeleaf page





WhateverIWant.java

@RequestMapping("/hi/{name}")
public  String hiThere(Map model, @PathVariable String name)
{
model.put("name", name);
return "hello";
}

____________________________________________________________________________________________

  • What Just Happended?
  1. spring-boot-starter-theymeleaf
       - Brought in required jars
       - Automatically configured ThymeleafViewResolver

    2. Controller returned a 'logical view name'
   
    3. View Resolver found a matching template

    4. Render



  • But wait, I want JSPs...

  1. Thymeleaf and other templating approaches are way too advanced for my organization!
       - Besides, we have lots of existing JSPs


    2. No Problem!
  
    3. Just as easy to use JSPs!
       - Place JSPs in desired web-relative location
       - Set spring.mvc,view.prefix / spring.mvc.view.suffix as needed. 
       - (remove thymeleaf starter pom)

______________________________________________________________________________________________

Demonstration - JSP Web Pages

- Place JSP in desired folder
- Set spring.mvc.view.prefix / spring.mvc.view.suffix
- Exclude spring-boot-starter-tomcat

Project Explorer

src -> main -> webapp -> create folder -> WEB-INF -> views -> hello.jsp





Hello ${name} from a JSP page




src/main/resources/appcliaction.properties


spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp



_______________________________________________________________________________________________

  • What Just Happened?
  1. No ThymeleafViewRresolver configured
  2. Controller returned a 'logical view name'
  3. InternalResourceViewResolver forwarded to JSP
  4. Render



  • Spring & REST
  1. REST capability is built in to Spring MVC
      - Simply use domain objects as parameters / return values.
      - Mark with @RequestBody / @ResponseBody
      - Spring MVC automatically handles XML/JSON conversion
           *Based on converters available in classpath.


___________________________________________________________________________________________

DemonStration - REST Controllers in Spring MVC
 
- Additional domain objects
- Automatic HTTP Message Conversion


package com.example.demo.domain;

public class Player {

String name;
String position;



public Player() {
super();

}


public Player(String name, String position) {
this();
this.name = name;
this.position = position;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}



}



package com.example.demo.domain;

import java.util.Set;

public class Team {
String name;
String location;
String mascotte;
Set players; 
public Team() {
super();
}

public Team(String location, String name, Set players) {
this();
this.location = location;
this.name = name;
this.players = players;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getMascotte() {
return mascotte;
}
public void setMascotte(String mascotte) {
this.mascotte = mascotte;
}
public Set getPlayers() {
return players;
}
public void setPlayers(Set players) {
this.players = players;
}

}



package com.example.demo;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.domain.Player;
import com.example.demo.domain.Team;

@Controller
public class WhateverIWant {
private Team team;
@PostConstruct
public void init()
{
Set players = new HashSet<>();
players.add(new Player("Charlien Brown", "pitcher"));
players.add(new Player("Snoopy", "shortstop"));
team = new Team("California", "Peanuts", players);
}
@RequestMapping("/hi")
public @ResponseBody Team hiThere()
{
return team;
}

}


____________________________________________________________________________________________


What Just Happened?

1. Controller returned a domain object
   - Not a logical view name (page)

2. Spring MVC noticed @ResponseBody
   - Or @RestController

3. Invoked correct HttpMessageConverter
  - Based on
     * Requested format
     * JARS on classpath


What if I want XML?

1. No Problem!

2. Annotate domain classes with JAXB annotations
  - JAXB already part of java SE

3. When App Starts
   - Spring creates HttpMessageConverter fo JAXB
      * Based on classpath contents

4. XML or JSON returned
   - based on requested format



@XmlRootElement   => xml out
Accept: application/xml

Accept: application/json


Adding JPA Capability

1. Adding the spring-boot-starter-data-jps Dependency
   - Adds Spring JDBC/ Transaction Management
   - Adds Spring ORM
   - Adds Hibernate / entity manager
   - Adds Spring Data JPA subproject
     * (explained later)


2. Does NOT add a Database Driver
   - Add one manually (HSQL)






Spring Data JPA

1. Typical web application architecture

2. REST Controllers provide CRUD interface to clients

3. DAO provide CRUD interface to DB




Spring Data - Instant Repositories

1. Spring Data provides dynamic repositories

2. You provide the interface, Spring Data dynamically implements.
- JPA, MongoDB, GemFire, etc.

3. Service Layer / Controllers have almost no logic.


_____________________________________________________________________________________________

Demonstraion - Adding Spring Data JPA

- spring-boot-starter-data-jpa
- org.hsqldb / hsqldb
- Annotate domain objects with JPA
- Extend CrudRepository


pom.xml


org.springframework.boot
spring-boot-starter-data-jpa


org.hsqldb
hsqldb



package com.example.demo.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Player {

@Id @GeneratedValue
Long id;
String name;
String position;



public Player() {
super();

}


public Player(String name, String position) {
this();
this.name = name;
this.position = position;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}



}



package com.example.demo.domain;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@Entity
public class Team {
@Id @GeneratedValue
Long id;
String name;
String location;
String mascotte;
@OneToMany(cascade=CascadeType.ALL) 
@JoinColumn(name="teamId")
Set players; 
public Team() {
super();
}

public Team(String location, String name, Set players) {
this();
this.location = location;
this.name = name;
this.players = players;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getMascotte() {
return mascotte;
}
public void setMascotte(String mascotte) {
this.mascotte = mascotte;
}
public Set getPlayers() {
return players;
}
public void setPlayers(Set players) {
this.players = players;
}

}

package com.example.demo.dao;



import java.util.List;

import org.springframework.data.repository.CrudRepository;

import com.example.demo.domain.Team;

public interface TeamDao extends CrudRepository {
List findAll();
Team findByName(String name);

}


package com.example.demo;

import java.util.HashSet;
import java.util.Set;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

import com.example.demo.dao.TeamDao;
import com.example.demo.domain.Player;
import com.example.demo.domain.Team;

@SpringBootApplication
public class MicroservicesBootApplication extends SpringBootServletInitializer{

/**
* Used when run as a JAR
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(MicroservicesBootApplication.class, args);
}

/**
* Used when run as a WAR
*/
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(MicroservicesBootApplication.class);
}
@PostConstruct
public void init()
{
Set players = new HashSet<>();
players.add(new Player("Charlien Brown", "pitcher"));
players.add(new Player("Snoopy", "shortstop"));
Team team = new Team("California", "Peanuts", players);
teamDao.save(team);
}
@Autowired TeamDao teamDao;

}


package com.example.demo;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.dao.TeamDao;
import com.example.demo.domain.Player;
import com.example.demo.domain.Team;

@RestController
public class WhateverIWant {
@Autowired TeamDao teamDao;

@RequestMapping("/teams/{name}")
public  Team hiThere(@PathVariable String name)
{
return teamDao.findByName(name);
}

}

_____________________________________________________________________________________________

Adding Spring Data JPA - What Just Happened?

1. What I did:
  - Added dependencies for spring-boot-starter-data-jpa and hsqldb
  - Annotated Domain objects with plain JPA annotations
  - Added an interface for Spring Data JPA
  - Dependency injected info controller


2. When application starts...
   - Spring Data dynamically implements repositories
     * find*(), delete(), save() methods implemented.
   - DataSource, Transaction Management, all handled.


Spring Data - REST

1. Often, applicatons simply expose DAO methods as REST resources

2. String Data REST handles this automatically...

Adding Spring Data REST

1. Plugs into dynamic repositories

2. Generates RRESTful interface
 - GET, PUT, POST, DELETE

3. Code needed only to override defaults.


__________________________________________________________________________________________

__________________________________________________________________________________________

Adding Spring Data REST - What Just Happened?

1. when applicaton starts...
   - @RestResource annotations interpreted
   - @Controllers beans created
   - @RequestMappings created


Adding HATEOAS

1. Spring Data Rest simply returns RESTful resources
- Conversion handled by Jackson, or JAXB

2. Underlying Data Relationships used to build Links
- If matching repositories exist

3. Consider the Team -> Player relationship

4. Player Repository needed to force link creation


__________________________________________________________________________________________

Demonstration - Adding HATEOAS Links
-  Creating a Player DAO


package com.example.demo.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Player {

@Id @GeneratedValue
Long id;
String name;
String position;



public Player() {
super();

}


public Player(String name, String position) {
this();
this.name = name;
this.position = position;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}



}

_______________________________________________________________________________________________

HATEOAS - What Just Happened?

1. Spring Data REST noticed two repositories
  - The relationship between entities is know via JPA annotations.

2. Spring automatically represents the children as links
  - @RestResource determines names of links


Summary
1. Spring Boot makes it easy to start projects
  - And easy to add featrure sets to projects
  - Opinionated apporach
  - Run as JAR or WAR
  - Web Applications (JSP, Thymeleaf, others)

2. REST
  - Automatic resource conversion

3. Spring Data JPA
  - Automatic repository implementation
 
4. Spring Data REST
  - Automatic REST controllers

2020년 1월 7일 화요일

What are Microservices


  • What are Microservices
  1. Presently a lot of hype!

  1. Best described as:
  2. An architectural style
  3. An alternative to more traditional 'monolithic' applications
  4. Decomposition of single system into a suite of small services, each running as independent proceses and intercommunicating via open protocols
       - With all the benefits / risks this implies.
  • Definitions from the Experts
  1. Developing a single application as a suite of small services, each running in its own process and communicating with lightweight mechanisms, often an HTTP resource API.                 
      - Martin Fowler    
    
     2. Fine-grained SOA
      - Adrian Cockcroft - Netflix



  • Microservices - Working Definition:
  1. Composing a single application using a suite of small services
       - (rather than a single, monolithic application)

    2. ... each running as independent processes
       - (not merely modules / components within a single executable)

    3. ... intercommunicationg via open protocols
       - ( Like HTTP/REST, or messaging)

    4.  Separately written, deployed, scaled and maintained
       - ( potentially in different languages)

    5.  Services encapsulate business capability
      - ( rather than language constructs (classes, packages) as primary way to encapsulate.

    6. Services encapsulate business capability
      - ( rather than language constructs (classes, packages) as primary way to encapsulate.

    7. Services are independently replaceable and upgradable

  • Microservices are not:
    1. The same as SOA
       - SOA is about integrating various enterprise applications.
         Microservices are mainly about decomposing single applications

    2. A silber bullet
       - The microservices approach involves drawbacks and risks

    3. New! You may be using microservices now and not konw it!


  • Current Trends
    1. Twitter moved from Ruby/Rails monolith to Micorservices.

    2. Facebook moved from PHP monolith to Microservices.

    3. Netflix moved from Java monolith to Microservices.


  • Microservices Example
    1. Consider a monolithic shopping cart application:
      - Web / mobile interfaces
      - Functions for:
          i) searching for products
          ii) Product catalog
          iii) Inventory management
          iv) Shopping cart
          v) Checkout
          vi) Fufillment
       - How would this look with microservices?

  • Monlolithic Application Example
    1. Monolithic shopping cart application:



    2. Understanding the Monolithic Architecture

 
   3. New types of client applications




   4. New type of persistence / services


  • Monolithic Challenges
    1. Single Codebase, Deployment, Versioning, Team Size
 



           
     2. Using Teams / Language Constructs


  • Understanding the monolithic implementation
       1. Single application executable
          - Easy to comprehend, but not to digest.
          - Must be written in a single language.
       2. Modularity based on Program Language
          - Using the constructs available in that language (packages, classes, functions,                namespaces, frameworks)
         - Various storage / service technologies used
           => REBMS, Messaging, eMail, etc.


  • Monolithic Advantages
  1. Easy to comprehend(but not digest)
  2. Easy to test as a single unit (up to a size limit)
  3. Easy to deploy as a single unit.
  4. Easy to manage (up to a size limit)
  5. Easy to manage changes (up to a point)
  6. Easy to scale (when care is taken)
  7. Complexity managed by language constructs.
  • Monolithic Drawbacks
   1. Language / Framework Lock
      - Entire app written with single technology stack. Cannot experiment / take                    advantage of emerging technologies
   2. Digestion
      - Single developer cannot digest a large codebase
      - Single team cannot manage a single large application   
        => Amazon's "2 Pizza" rule
   3. Deployment as single unit
     - Cannot independently deploy single change to single component.
     - Changes are "held-hostage" by other changes     


  • Enter Microservices architecture







  • Compoentization via Services
  1. NOT language constructs.
  2. Where services are smaill, independently deployabl applications
  3. Forces the design of clear interfaces
  4. Changes scoped to their affected service



  • Microserices: Composed using suite of small services
  1. Services are small, independently deployable applications
      - Not a single codebase
      - Not (necessarily) a single language / framework
      - Modularization not based on language / framework constructs



  • Microservices: Communication based on lightweight protocols
  1. HTTP, TCP, UDP, Messaging, etc.
      - Payloads: JSON, BSON, XML, Protocol Buffers, etc.
    2. Forces the design of clear interfaces
    3. Netflix's Cloud Native Architecture - Communicate via APIs
      - NOT Common Database



  • Microservices: Services encapsulate business capabilities
  1. Not based on technology stack
  2. Vertical slices by business function (i.e. cart, catalog, checkout)
  3. ...Though technology chunk also practical (email service)
  4. Suitable for cross-functional teams


  • Micoroservices: Services easily managed
  1. Easy to comprehend, alter, test, version, deploy, manage, overhaul, replace
       - By small, cross-functional teams (or even individuals)




  • Decentralized Governance
  1. Use the right tool ( language, framework) for the job.
  2. Services evolve at different speeds, deployed and managed according to different needs.
  3. Make services be "Tolerant Readers"
  4. Consumer-0Driven Contracts
  5. Antithesis of ESB
      - Services are not Orchestrated, but Choreographed



  • Polyglot Persistence
  1. Freedom to use the best technology for the job
      - Don't assume single RDBMS is always best
      - Very controversial! Many DBAs will not like this!
         i) No pan-enterprise data model!
         ii) No transactions!



  • Microservice Advantages
  1. Easy to digest each service (difficult to comprehend whole)
  2. VERY easy to test, deploy, manage, version, and scale single services
  3. Change cycle decoupled
  4. Easier to scale staff
  5. No Language / Framework lock


  • Challenges with Microservices
  1. Complexity has moved out of the application, but into the operations layer
       - Fallacies of Distributed Computing

    2. Services may be unavailable
       - Never needed to worry about this in a monolith!
       - Design for failure, circuit breakers
        i) "Everything fails all the time" - Werner Vogels, CTO Amazon
       - Much more monitoring needed
 
     3. Remote calls more expensive than in-process calls

     4. Transactions: Must rely on eventual consistency over ACID

     5. Features span multiple services

     6. Change management becomes a different challenge
        - Need to consider the interaction of services
        - Dependency management / versions
 
      7. Refactoring Module Boundaries


  • Fallacies of Distributed Computing
  1. The network is reliable.
  2. Latency is zero.
  3. Bandwidth is infinite.
  4. The network is secure.
  5. Topology doesn't change.
  6. Therre is noen administrator.
  7. Transport cost is zero.
  8. The network is homogeneous.
  • How Do You Break a Monolith into Microservices?
  1. Primary consideration: business functionality:
       - Noun-based (catalog, cart, customer)
       - Verb-based (search, checkout, shipping)
       - Single Responsibility Principle
         => http://programmer.97things.oreilly.com/wiki/index.php/The_Single_Responsibility_Principle
       - Bounded Context
         => http://martinfowler.com/kliki/BoundedContext.html


  • How Micro is Micro?
  1. Size  is not the compelling factor
      - Smaill enough for an individual developer to digest
      - Small enough to be built and managed by small team
        i) Amazon's two pizza raule
      - Documentation small enough to read and understand
        i) Social Security Act of 1935 - 63 pages
        ii) Affordable Care Act of 2010 - 906 pages

       - Dozens of secrets, not hundreds.
       - Predictable. Easy to experiment with



  • Differences with SOA
  1. SOA addresses integration between systems.
       - Microservices address individual applications
    2. SOA relies on orchestration.
       - Microservices rely on choreography

    3. SOA relies on smart integration technology, dumb services
       - Microservices rely on smart services, dum integration technology
       - Consider: Linux commands, pipes and filters

 

  • Are Monoliths Always Bad?
  1. Consider etsy.com
       - As of Rebruary 2013: 1.49 billion page views, 4,215,169 items sold, $94.7 million             of goods sold, 22+ million members
       - 150 developers deploy single WAR 60 tiems a day
       - Practices: Cl; push button deployment; good monitoring; developers deploy to             the site on the first day; VMs per developer; GitHub; Chef; IRC to controll                     releases; dashboards; no source control branches.