Skip to content

pivotal-bill-kable/spring-boot2-student-notes

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 

Repository files navigation

Reference materials

In addition to the standard course contents, we might use some extra reference materials below whenever needed:

Microservices

The 12 Factors App

App Continuum

Auto-configuration starter

Creating our own Spring Boot auto-configuration starter lab

  • Clone the project and follow TODO's (TODO 10-16, 20-26, 30-38)

REST

Spring MVC and REST

RestTemplate

  • Javadoc

  • RestTemplate Customization

  • Tutorial

  • Example of a Spring Cloud Load Balanced Rest Template customized with timeout:

        /**
         * RestTemplate Builder
         *
         * Following example sets connect timeout of 500ms,
         * where if client cannot acquire open connection on a socket,
         * it will time out.
         *
         * Operations that timeout on connect events may be retried without
         * regard to Idempotence.
         *
         * The example also shows how to configure the read timeout to 2
         * seconds.
         * This protects the calling client from long running calls in a
         * downstream server/producer.
         *
         * Retries may only be executed across read timeouts if the down
         * stream operation is idempotent,
         * use with care.
         *
         * @return RestTemplate
         */
        @LoadBalanced
        @Bean
        public RestTemplate restTemplate() {
            return new RestTemplateBuilder()
                    .setConnectTimeout(Duration.ofMillis(500L))
                    .setReadTimeout(Duration.ofMillis(2000L))
                    .build();
        }

How to run HSQLDB Client

  • Add the following configuration class
import org.hsqldb.util.DatabaseManagerSwing;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

@Configuration
public class HsqldbConfig {

    @Autowired
    DataSource dataSource;

    @Bean
    public JdbcTemplate getJdbcTemplate(){
        return new JdbcTemplate(dataSource);
    }

    //default username : sa, password : ''
    @PostConstruct
    public void getDbManager(){
        DatabaseManagerSwing.main(
                new String[] { "--url", "jdbc:hsqldb:mem:testdb", "--user", "sa", "--password", ""});
    }
}
  • Turn off the "java.awt.headless"
	System.setProperty("java.awt.headless", "false");

Actuator

Actuator/Prometheus lab

Spring Boot Testing

OAuth2

Spring Cloud Contract

Tools

Spring related

  • Spring Lifecycle changes with Spring 5.+ and Java 11

    • Java 11 Drops JSR 250 Annotations, including @PostConstruct & @PreDestroy. See Java 11 Migration Guide and Spring Framework docs

    • If using Java 11, you may explicitly import annotations:

      implementation 'javax.annotation:javax.annotation-api:1.3.2'
      <dependency>
         <groupId>javax.annotation</groupId>
         <artifactId>javax.annotation-api</artifactId>
         <version>1.3.2</version>
      </dependency>
      
    • Or, you may use @Bean parameters to specific init and cleanup callbacks, see Spring Lifecycle Callbacks for more examples.

    • You may also implement the InitializingBean or DisposableBean on the class you want to provide the lifecycle callbacks, but this ties your class to Spring.

PCF

Deploy a Spring Boot web application to PCF

  1. Create a free PWS account (if you have not done so yet) from https://run.pivotal.io/

  2. Download and install cf cli from https://docs.cloudfoundry.org/cf-cli/install-go-cli.html

    • If you are using MacOS, you can use Homebrew
    brew tap cloudfoundry/tap
    brew install cf-cli
    
  3. Log in to the PWS

    cf login -a api.run.pivotal.io
    
  4. Creat a fat jar from any of your Spring Boot web application

    <core-spring-labfiles/lab> ./gradlew 38-rest-ws-solution:build
    
  5. Deploy the application to PCF

    <core-spring-labfiles/lab> cf push my-app -p 38-rest-ws-solution/build/libs/38-rest-ws-solution-5.0.c.RELEASE.jar --random-route
    
  6. Display all apps deployed

    <core-spring-labfiles/lab> cf apps
    
  7. Access the application from a browser by copying the url of the app and place it into a browser's url

  8. You can also manage your applications from PWS App Manager UI by going to https://console.run.pivotal.io/

  9. If you enable Actuator in your Spring Boot application, you can visualize and manage your Actuator endpoints in App Manager. See Using Spring Boot Actuators with Apps Manager for more information.

OAuth in action in PCF

  1. Observe that every request has Authorization request header set with [PRIVATE DATA HIDDEN], which represents access token

    <core-spring-labfiles/lab> cf -v app my-app
    
  2. Go to /.cf and observe that there is a file called config.json

  3. Display the contents of `config.json" and observe that it has access token

    cat config.json
    
  4. Copy the access token (starting from the character right bearer under Authorization field and analyze it via https://jwt.io/

    • Observe that there are three sections
    • Observe that there are scopes under payload section

Using MySQL backing servce

Spring Performance Considerations

Spring Boot 1.5 -> 2.0 Migration Considerations

Microservices & Fault Tolerance Patterns

Certification

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published