Kubernetes Native Java by Josh Long @ Spring I/O 2022

Kubernetes Native Java by Josh Long @ Spring I/O 2022

Introduction

In this section, Josh Long introduces himself and his role as a Spring developer advocate. He also provides information on how to contact him through various platforms.

About Josh Long

  • Josh Long is a Spring developer advocate, Kotlin Google Developer Expert, Java Champion, and author.
  • He expresses his excitement to be back in person with the audience.
  • Josh encourages the audience to reach out to him with any questions or concerns they may have.

Contact Information

  • You can find Josh on Twitter and LinkedIn.
  • He has a blog and a new book called "Reactive Spring."
  • Josh has a YouTube channel where he posts Spring tips videos.
  • He hosts a podcast where he talks to people who are smarter than him.

Building Applications for Production with Spring

In this section, Josh discusses building applications intended for production using Kubernetes.

Importance of Production

  • Kubernetes is the place to put your applications for most people.
  • Josh emphasizes the importance of production and encourages everyone to go there as early and often as possible.

Building an Application

  • The application being built is called "Customers."
  • Reactive web support, reactive R2DBC, Spring Boot Actuator, and Spring Native for GraalVM support will be used.

Building Reactive Applications

In this section, the speaker discusses the benefits of using reactive programming when building applications intended for production and scale.

Benefits of Reactive Programming

  • The first benefit is ease of resource efficiency. Reactive programming allows for effective use of resources on a computer by reusing threads.
  • The second benefit is that it provides a single type to describe data from different sources, making it easier to write error-free code when connecting systems.
  • The third benefit is that reactive streams can be composed together, allowing for support for robustness, operators, retries, timeouts, rate limiting, etc.

Building a Simple Service

  • To demonstrate how to build a reactive application, the speaker creates a simple customer service with an integer ID and string name.
  • Using the created repository and controller classes, all data can be retrieved through a REST endpoint.

Overall, using reactive programming simplifies writing threaded code while making effective use of resources. It also makes it easier to connect different systems and compose streams together.

Creating Data with Spring Boot

In this section, the speaker creates data using Spring Boot and inserts some people into a customer table.

Inserting People into Customer Table

  • Use SQL command "INSERT INTO customer name values" to insert people into the customer table.
  • The speaker inserts eight names including Stefan, Josh, Madura, Olga, Yushen, Spencer and himself.
  • Run the application to see the data on localhost.

Using Actuator in Spring Boot

In this section, the speaker discusses how to use Actuator in Spring Boot.

Enabling All Endpoints in Actuator

  • Use managed HTTP endpoints provided by Actuator to get information about the application.
  • Expose all endpoints by setting "management.endpoints.web.exposure.include=*".
  • Enable Kubernetes probes by setting "management.endpoint.health.show-details=always" and "management.health.kubernetes.enabled=true".
  • Restart the application to see changes.

Managed Endpoints in Actuator

  • Beans endpoint shows different objects and their relationships.
  • Caches endpoint shows cache managers info.
  • Health endpoint shows health status of the application.
  • Config props endpoint shows properties that Spring Boot will respond to.
  • Environment endpoint shows environment variables.
  • Loggers endpoint shows all loggers used in the application.
  • Heap dump and thread dump endpoints can be used for debugging heaps and threads respectively.
  • Metrics endpoint is powered by Micrometer which is an abstraction for vendor-neutral application metrics facade.

Customizing Info Endpoint

  • Customize info page by setting "info.env.enabled=true" and adding a message like "info.message=Hello Spring IO".

Configuration and Git Commit ID Plugin

In this section, the speaker discusses how to configure an application and use the Git commit ID plugin.

Adding Custom Configuration

  • To add custom configuration, add the following code to your pom.xml file:

<build>

<plugins>

<plugin>

<groupId>pl.project13.maven</groupId>

<artifactId>git-commit-id-plugin</artifactId>

</plugin>

</plugins>

</build>

  • This will automatically capture information about the Git repository.

Using Git Commit ID Plugin

  • Use the Git commit ID plugin to see what commit triggered a change that resulted in the code that is in production.
  • To use the plugin, run git add ., git status, git commit -m "message", then mvn clean package.

Health Endpoint and Probes

In this section, the speaker discusses how to create a useful health endpoint and probes for Kubernetes.

Liveness Probe and Readiness Probe

  • Kubernetes has two specific concepts called liveness probe and readiness probe.
  • The readiness probe tells you whether the service is ready to be put into production. The liveness probe tells you whether the service is still alive and should stay in production.
  • These probes are there for you out of the box, but you can manipulate them or contribute to them if you want to.

Creating a Health Endpoint

  • To create a health endpoint, add the following code to your application:

@Controller

class HealthController

private final ApplicationContext context;

@GetMapping("/down")

void down()

context.publishEvent(new LivenessStateChangedEvent(false));

  • Restart the application and go to localhost:8080/down to test the health endpoint.

Graceful Shutdown

In this section, the speaker talks about how to handle graceful shutdown in Kubernetes.

Configuring Graceful Shutdown

  • Use sig term and sig kill signals to take a pod out of rotation from the load balancer.
  • Use "graceful shutdown" to finish work that is in progress before shutting down.
  • Configure the duration of time for which you want to wait before shutting down completely.
  • Update your YAML file for the pod and deployment to reflect this timeout.

Configuration Sets

In this section, the speaker talks about configuration sets and how they can be used in certain contexts.

Activate on Cloud Platform

  • Use dash triple hash syntax to activate configuration sets that are only activated in certain contexts.
  • Set activate on cloud platform equals kubernetes to activate configuration sets for Kubernetes.

Config Tree Syntax

  • Use config tree syntax when working with a directory structure created by taking a config map in Kubernetes.
  • The directory contains files named after keys in the config map, with contents being values of those keys.

Override Configuration Properties

In this section, the speaker talks about overriding configuration properties and program arguments.

Program Arguments

  • Pass -spring main cloud platform equals kubernetes as a program argument for local testing environment.
  • Restart application after modifying options.

Dynamic Kubernetes Aware

In this section, the speaker explains how to configure an application to be dynamic and Kubernetes aware.

Configuration

  • The speaker changes the configuration so that it doesn't think it's running in Kubernetes.
  • After restarting, the application starts again on port 8080 without "hello kubernetes."

Getting into Production

  • To get into production, a binary is needed.
  • Spring Native is used for building native images with GraalVM.
  • Spring Native is a build-time toolchain that understands all the places of interest to GraalVM.

Understanding GraalVM

  • GraalVM takes code and looks at reachability by analyzing all methods called by other methods.
  • It keeps reachable classes in the native image heap of the resulting binary but throws everything else away.

Spring Native Framework

  • Spring Native provides most of the configuration for hints but requires additional configuration if using unsupported features.
  • Hints are compile-time auto-configurations built on top of Spring Native.

Compiling Application into a Native Image

  • Maven skip tests is used to compile the application tests into native code before compiling the actual application into native code.
  • The reachability algorithm takes time as it analyzes everything on the classpath every time you recompile.

Building and Deploying a Spring Boot Application

In this section, the speaker discusses building and deploying a Spring Boot application. They cover topics such as compiling for production, using build packs to create Docker images, and deploying to production.

Compiling for Production

  • Normally, you would compile your application for production on Linux.
  • You can use build packs to turn your application code into a Docker image.
  • Build packs are convenient because they provide one recipe for building Docker images out of your Spring Boot application.

Deploying to Production

  • Once you have compiled your application for production, you can deploy it by tagging and pushing the Docker image to your container registry of choice.

Building an Edge Service with Spring Cloud Gateway

In this section, the speaker discusses building an edge service with Spring Cloud Gateway. They cover topics such as accommodating different clients and homogenizing requests.

Accommodating Different Clients

  • There will be lots of different clients accessing our microservice (e.g., iPhone devices, Android devices, TVs).
  • We need to accommodate these clients by homogenizing their requests through an edge service before sending them to the downstream microservices.

Homogenizing Requests

  • The edge service allows us to homogenize all requests from different clients before sending them downstream.
  • This is important because each client has its own bandwidth constraints and power constraints.

Using Spring Cloud Gateway to Handle Service Orchestration and Composition

In this section, the speaker discusses how to handle service orchestration and composition using Spring Cloud Gateway.

Using a Proxy to Handle Service Orchestration

  • To handle service orchestration at the edge, one can use a proxy like Spring Cloud Gateway.
  • The RouteLocatorBuilder is used to build routes for incoming requests.
  • Filters are used to modify headers, add retries, do request rate limiting, path rewriting, etc.

Tanzu's Kubernetes Version of Spring Cloud Gateway

  • Tanzu has a Kubernetes version of Spring Cloud Gateway that can be used to declaratively define rules and filters at the cluster level.

Introduction to GraphQL

In this section, the speaker introduces GraphQL and its integration with Java.

Benefits of GraphQL

  • GraphQL is a technology that allows for efficient querying of APIs.
  • The integration between GraphQL and Java is provided by the graphql-java project.
  • Twitter replaced their Scala-based GraphQL engine with graphql-java and saw improved performance and efficiency.

Example API: SpaceX

  • The SpaceX API (api.spacex.land) is an interesting example of an API that uses GraphQL.

GraphQL Basics

In this section, the speaker introduces GraphQL and explains how it works. He highlights the benefits of using GraphQL over HTTP.

Introduction to GraphQL

  • GraphQL is a query language for APIs that allows you to ask for only the data you need.
  • It has an introspectible endpoint that allows you to query different types of data and commands.
  • Subscriptions in GraphQL are used for long-lived feeds such as stock ticker feeds and sensor data.
  • GraphQL has three main things: queries, mutations, and subscriptions.

Benefits of Using GraphQL

  • GraphQL solves all the problems associated with HTTP by providing a protocol that is easy to learn.
  • HTTP can be confusing when trying to make changes such as POST or PUT requests. However, with GraphQL, these issues are solved.
  • GraphQL is not restful but provides a feature that solves problems related to reading, updating, and working with data.

Building a GraphQL Endpoint

  • To build a GraphQL endpoint, create a new folder called "graphql" and add a new file called "schema.graphql".
  • Create a controller class called "CrmGraphqlController" with methods that return all customers or profiles.
  • Use web client http at the constructor arguments to retrieve body flux customer.class from localhost 8080/customers.

Resolving Data from Multiple Microservices

  • To resolve data from multiple microservices in your schema mapping, use lower-level schema mapping for types such as customer or profile.
  • Inject the root customer to resolve data from a separate microservice for profiles.

Introduction to Querying APIs with Spring Boot and GraphQL

In this section, the speaker introduces the topic of querying APIs with Spring Boot and GraphQL. He explains how to use GraphQL to query data from multiple endpoints in a more efficient way.

Using GraphQL for Efficient Data Retrieval

  • The speaker demonstrates how to use GraphQL to query data from multiple endpoints in a more efficient way.
  • He explains that using batch mapping can help avoid n+1 queries and make network calls more efficient.
  • The speaker shows how reactive types can be used to make asynchronous network calls, which can further improve efficiency.
  • He emphasizes that using GraphQL makes it easy to compose different services seamlessly and efficiently.

Production Considerations

  • The speaker reminds the audience that when building services for production, it's important to consider scalability, packaging, and reliability.
  • He notes that while the talk focused on technical aspects of building services, it's important not to forget about business logic.

Conclusion

  • The speaker thanks the audience for attending and encourages them to attend his upcoming talk on writing operators. He also reminds everyone to wear masks as needed.

Turn any video into a summary like this

YouTube links, meetings, lectures. With transcripts, search, and chat.

Video description

Spring I/O 2022 - Barcelona, 26-27 May Spring is all about helping developers get to production quickly and safely. These days, “production” is all but guaranteed to mean Kubernetes, and Spring has you covered. Join me, Spring Developer Advocate Josh Long (@starbuxman), and we’ll look at how Spring Boot makes writing blisteringly fast, cloud-native, and scalable services more effortless than ever.