Friday, January 27, 2023

Install JDK

Install JDK

Establishing a JDK development environment is often confusing for those new to Java.
OpenJDK is only the open source code, and many distributions release its binary. When installing JDK on Windows, you must consider three points: JDK version, distribution, and setting environment variables.

JDK Version

Detailed information on each version of the JDK can be found on Wikipedia: Java version history . But usually, we only focus on the LTS (Long-term support) version. The following lists the three main LTS version numbers:

Ver. Date Brief
8 2014-03 Lambdas
Spring 5.3.x & Spring Boot 2.x
11 2018-09 New HTTP Client
Spring 5.3.x & Spring Boot 2.x
17 2021-09 Sealed Classes
Spring Framework 6 & Spring Boot 3

Distributions

Install BellSoft Liberica JDK version 17

In Spring Quickstart Guide official website, it is recommended to use BellSoft Liberica JDK version 17. When entering the BellSoft website to download the SDK, you will find that various versions of JDK are provided. The following examples will use JDK 17 LTS to demonstrate. The downloaded file is in .msi format, so it is easy to follow the steps to complete the installation.

Set windows Environment Variables

If you have not installed other versions of JDK in your Windows environment, the BellSoft installer will automatically add the JAVA_HOME variable and Path to the Windows environment variables. The default information is as follows:

Var. Value
JAVA_HOME C:\Program Files\BellSoft\LibericaJDK-17\
Path C:\Program Files\BellSoft\LibericaJDK-17\bin\

After the installation is complete, open PowerShell and enter the following commands to check the output to make sure whether the installation was successful:

> java --version
openjdk 17.0.6 2023-01-17 LTS
OpenJDK Runtime Environment (build 17.0.6+10-LTS)
OpenJDK 64-Bit Server VM (build 17.0.6+10-LTS, mixed mode, sharing)
> javac --version
javac 17.0.6

Create a “Hello World!” Program

Use spring initializr to create a project quickly. You can set the options through the UI provided by spring initializr and download the project, or directly in the command line of vscode (Ctrl+Shift+P), search for “spring initializr”. Follow the steps to create a project.

In the src\main\java\com\example\demo directory of the project, open DemoApplication.java, and refer to the following example to modify the content:

package  com.example.demo;

import  org.springframework.boot.SpringApplication;
import  org.springframework.boot.autoconfigure.SpringBootApplication;
import  org.springframework.web.bind.annotation.GetMapping;
import  org.springframework.web.bind.annotation.RequestParam;
import  org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public  class  DemoApplication {
	public  static  void  main(String[] args) {
	SpringApplication.run(DemoApplication.class, args);
	}
	
	@GetMapping("/hello")
	public  String  hello(@RequestParam(value = "name", defaultValue = "World") String  name) {
		return  String.format("Hello %s!", name);
	}
}

Using terminal (Ctrl+`) and enter:

./mvnw spring-boot:run

Open http://localhost:8080/hello with a browser you will see the Hello World! webpage.


參考資料:


Create Your Docker Image

Create Your Docker Image

The article is described in the following scenarios:

  1. Get the Nginx Image from Docker Registry (Container Registry)
  2. Execute the Container and add/change files
  3. Return to the background process and check the status
  4. Build the new Image, and
  5. Finally, upload to the Docker Registry

Use Nginx as a Web Server

  1. Get nginx image from Docker Hub

    docker pull nginx
    
  2. Run nginx as a container with the name webserver

    docker run -d -p 8080:80 --name webserver nginx

    Options Description
    -d --detch, Run container in background
    -p Publish all exposed ports to the port,
    format host-port:container-port
    --name Assign a name to the container
    nginx Docker image or ID

    After execution, enter localhost:8080 in your host browser and see the “Welcome to nginx!” web page.

  3. Stop, start and restart the container

    docker stop webserver
    docker start webserver
    docker restart webserver
    
  4. Make sure that the container is in the execution, then type the following command to enter the shell environment

    docker exec -it webserver sh
    
    Options Description
    -i --interactive, Keep STDIN open even if not attached
    -t --tty, Allocate a pseudo-TTY

    The default home page of Nginx is located at /usr/share/nginx/html/index.html; you can use the following command to confirm it.

    # cat /usr/share/nginx/html/index.html

    Enter exit to leave the terminal and return to PowerShell.

  5. Copy the file to replace the default web page with a simple Hello World.

    <!DOCTYPE  html>
    <html>
    <head>
    	<title>Hello World Sample</title>
    </head>
    <body>
    	Hello World!
    </body>
    </html>
    

    Overwrite the container’s homepage with the cp command.

    docker cp ./index.html \
      webserver:/usr/share/nginx/html
    

    Enter localhost:8080 in the browser, and you can see that the homepage has been updated.

  6. Create a new image
    Use docker commit to save the container as a new image.

    docker commit webserver \
      <docker_hub_account> \
      /hello_world_nginx:1.0
    
  7. Stop the container and remove it.

    docker stop webserver
    docker rm webserver
    
  8. Run the newly docker image, and test in the browser.

    docker run -d -p 8080:80 \
      --name webserver \
      <docker_hub_account>\
      /hello_world_nginx:1.0
    
  9. Login to docker hub and upload the new docker image.

    docker login
    
    docker push \
      <docker_hub_account>\
      /hello_world_nginx:1.0
    

    After completion, you can see the new docker image on the docker hub web page.


Reference:
How to Create a Docker Image From a Container


GCP Artifact Registry

With a simple example, step by step, push Docker Image to GCP Artifact Registry.

Artifact Registry improves and extends upon the existing capabilities of Container Registry, such as customer-managed encryption keys, VPC-SC support, Pub/Sub notifications, and more, providing a foundation for major upgrades in security, scalability, and control.

The sample is running in Windows 11 PowerShell 7.3.1.

GCP Push and pull images

Pre-work

  1. Create a GCP project (ex. artifact-registry-microsystem).
  2. Docker installed on the local machine.

Google Cloud CLI Installer

You can refer to the following webpage if you have not installed gcloud CLI: Install the gcloud CLI.

After installation, you will be asked to log in and choose a default project name. Once done, you can enter the gcloud command in PowerShell.

Create Repository

Select the GCP Project you want to join, and add Artifact Registry. The relevant setting options are as follows:

  • Name: quickstart-docker-repo (Repository name, you can set it yourself)
  • Format: Docker
  • Location Type: Region
  • Region: us-central1

Setting Auth

Executing the following commands will modify the contents of the docker configuration:

gcloud auth configure-docker \
  us-central1-docker.pkg.dev

Check the docker configuration, and you will find that the credHelpers block has been added. Execute the following commands:

cat $env:UserProfile\.docker\config.json

The results are as follows:

{
"credsStore": "desktop",
"credHelpers": {
  "us-central1-docker.pkg.dev": "gcloud"
  }
}

Pull Docker Image

Next, you can create your own or get a Docker Image from Docker Hub:

docker pull nginx

Tag Docker Image

Before pushing the docker image, add a tag to the docker image with the hostname and repository name.

The tag format is: <hostname>/<project-name>/<repository-name>/<image-name>:<tag>

docker tag nginx \
  us-central1-docker.pkg.dev \
  /artifact-registry-microsystex \
  /quickstart-docker-repo \
  /nginx-image:tag1
  • us-central1: the region of the repository host
  • docker.pkg.dev: adding this suffix after the region will be the hostname
  • artifact-registry-microsystex: GCP Project ID, this is the project name you can create by yourself
  • nginx-image: Docker image name

Push Docker Image

Finally, Docker can be pushed to a private registry:

docker push 
  us-central1-docker.pkg.dev \
  /artifact-registry-microsystex \
  /quickstart-docker-repo \
  /nginx-image:tag1

References:
Artifact Registry: the next generation of Container Registry


Publish Blogger From StackEdit With Markdown

You can publish your Google Blogger from StackEdit with Markdown Language. It is more convenient than writing an article in the Blogger editor.

Selects Styled HTML for the Template when publishing; otherwise, the content of the Code Block will exceed the screen.

Remember to set those in file properties if you do want a specific date or add tags in this Blogger.

Deploying Vue & .NET with Google OAuth on GCP Cloud Run

Deploying Vue & .NET with Google OAuth on GCP Cloud Run Deploying Vue & .NET with Google OAuth on GCP Cloud Run...