เกี่ยวกับ Codelab นี้
1 ภาพรวม
การติดตามที่เผยแพร่มีความสําคัญในการรับข้อมูลเชิงลึกและความสามารถในการสังเกตเกี่ยวกับสถาปัตยกรรม Microservice แบบหลายชั้น เมื่อคุณใช้เชนบริการกับการเรียกใช้บริการ ตั้งแต่บริการ A ไปจนถึงบริการ B ไปจนถึงบริการ C ก็ควรทําความเข้าใจด้วยว่าการโทรสําเร็จและเวลาในการตอบสนองทุกขั้นตอน
เมื่อใช้ Spring Boot คุณสามารถใช้ Spring Cloud Sleuth เพื่อเพิ่มเครื่องมือการติดตามแบบกระจายไปยังแอปพลิเคชันได้อย่างราบรื่น โดยค่าเริ่มต้น เครื่องมือจะส่งต่อข้อมูลการติดตามไปยัง Zipkin ได้
Google Cloud Platform มี Stackdriver Trace ซึ่งเป็นบริการที่มีการจัดการที่ช่วยให้คุณจัดเก็บข้อมูลการติดตามได้โดยไม่ต้องจัดการอินสแตนซ์หรือพื้นที่เก็บข้อมูล Zipkin ของคุณเอง ส่วน Stackdriver Trace ยังสามารถสร้างรายงานการกระจายเวลาในการตอบสนองและตรวจหาการถดถอยประสิทธิภาพได้โดยอัตโนมัติ
มี 2 ตัวเลือกในการใช้ Stackdriver Trace จากแอปพลิเคชัน Spring Boot ได้แก่
- ใช้ Stackdriver Trace Zipkin Proxy และกําหนดค่า Spring Cloud Sleuth ให้ใช้พร็อกซีนี้เป็นปลายทาง Zipkin
- หรือใช้ Spring Cloud GCP Trace ซึ่งผสานรวมเข้ากับ Spring Cloud Sleuth ได้อย่างราบรื่นและส่งต่อข้อมูลการติดตามไปยัง Stackdriver Trace โดยตรง
ใน Codelab นี้ คุณจะได้เรียนรู้วิธีสร้างแอปพลิเคชัน Spring Boot ใหม่และใช้ Spring Cloud GCP Trace สําหรับการติดตามผู้กระจาย
สิ่งที่จะได้เรียนรู้
- วิธีสร้างแอปพลิเคชัน Spring Boot สําหรับ Java และกําหนดค่า Stackdriver Trace
สิ่งที่ต้องมี
- โครงการ Google Cloud Platform
- เบราว์เซอร์ เช่น Chrome หรือ Firefox
- ทําความคุ้นเคยกับเครื่องมือแก้ไขข้อความ Linux มาตรฐาน เช่น Vim, EMAC หรือ Nano
คุณจะใช้บทแนะนํานี้อย่างไร
คุณจะให้คะแนนประสบการณ์ในการสร้างเว็บแอป HTML/CSS อย่างไร
คุณจะให้คะแนนประสบการณ์ในการใช้บริการของ Google Cloud Platform อย่างไร
2 การตั้งค่าและข้อกําหนด
การตั้งค่าสภาพแวดล้อมด้วยตนเอง
หากยังไม่มีบัญชี Google (Gmail หรือ Google Apps) คุณต้องสร้างบัญชี ลงชื่อเข้าใช้คอนโซล Google Cloud Platform (console.cloud.google.com) และสร้างโปรเจ็กต์ใหม่ ดังนี้
โปรดทราบว่ารหัสโปรเจ็กต์ ซึ่งเป็นชื่อที่ไม่ซ้ํากันสําหรับโปรเจ็กต์ Google Cloud ทั้งหมด (ชื่อข้างต้นมีผู้อื่นนําไปใช้แล้ว ขออภัยในความไม่สะดวก) และจะเรียกใน Codelab นี้ว่า PROJECT_ID
ในภายหลัง
จากนั้นคุณจะต้องเปิดใช้การเรียกเก็บเงินใน Cloud Console เพื่อใช้ทรัพยากรของ Google Cloud
การเรียกใช้ Codelab นี้ไม่ควรมีค่าใช้จ่ายเกิน 2-3 ดอลลาร์ แต่อาจมากกว่านั้นหากคุณตัดสินใจใช้ทรัพยากรเพิ่มเติมหรือปล่อยให้ทรัพยากรทํางาน (ดู "cleanup" ในตอนท้ายของเอกสารนี้)
ผู้ใช้ใหม่ของ Google Cloud Platform มีสิทธิ์รับช่วงทดลองใช้ฟรี$300
Google Cloud Shell
แม้ว่า Google Cloud และ Kubernetes จะทํางานจากแล็ปท็อปได้จากระยะไกล แต่ใน Codelab นี้เราจะใช้ Google Cloud Shell ซึ่งเป็นสภาพแวดล้อมบรรทัดคําสั่งที่ทํางานในระบบคลาวด์
เปิดใช้งาน Google Cloud Shell
จากคอนโซล GCP ให้คลิกไอคอน Cloud Shell บนแถบเครื่องมือด้านขวาบน ดังนี้
จากนั้นคลิก "เริ่ม Cloud Shell"
ระบบจะใช้เวลาเพียงครู่เดียวในการจัดสรรและเชื่อมต่อกับสภาพแวดล้อม
เครื่องเสมือนนี้โหลดด้วยเครื่องมือการพัฒนาทั้งหมดที่คุณต้องการ โดยมีไดเรกทอรีหน้าแรกขนาด 5 GB ถาวรและทํางานอยู่ใน Google Cloud ซึ่งช่วยปรับปรุงประสิทธิภาพและการตรวจสอบสิทธิ์ของเครือข่ายได้อย่างมาก ไม่เพียงเท่านั้น งานของคุณในห้องทดลองนี้ก็ทําได้โดยใช้เพียงเบราว์เซอร์หรือ Google Chromebook
เมื่อเชื่อมต่อกับ Cloud Shell แล้ว ระบบจะตรวจสอบว่าคุณตรวจสอบสิทธิ์แล้ว และมีการตั้งค่าโปรเจ็กต์เป็น PROJECT_ID แล้ว
เรียกใช้คําสั่งต่อไปนี้ใน Cloud Shell เพื่อยืนยันว่าคุณได้รับการตรวจสอบสิทธิ์
gcloud auth list
เอาต์พุตจากคําสั่ง
Credentialed accounts: - <myaccount>@<mydomain>.com (active)
gcloud config list project
เอาต์พุตจากคําสั่ง
[core] project = <PROJECT_ID>
หากไม่ใช่ คุณจะตั้งค่าได้โดยใช้คําสั่งนี้
gcloud config set project <PROJECT_ID>
เอาต์พุตจากคําสั่ง
Updated property [core/project].
3 สร้างบริการ Spring Boot REST ใหม่
หลังจากเปิดตัว Cloud Shell คุณจะใช้บรรทัดคําสั่งเพื่อสร้างแอปพลิเคชัน Spring Boot ใหม่ด้วย Spring Initializr ได้ดังนี้
$ curl https://start.spring.io/starter.tgz -d packaging=jar \
-d dependencies=web,lombok,cloud-gcp,cloud-starter-sleuth \
-d baseDir=trace-service-one | tar -xzvf - \
&& cd trace-service-one
สร้างตัวควบคุม REST ใหม่โดยเพิ่มคลาสใหม่ ดังนี้
src/main/java/com/example/demo/WorkController.java
package com.example.demo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Random;
@RestController
@Slf4j
public class WorkController {
Random r = new Random();
public void meeting() {
try {
log.info("meeting...");
// Delay for random number of milliseconds.
Thread.sleep(r.nextInt(500));
} catch (InterruptedException e) {
}
}
@GetMapping("/")
public String work() {
// What is work? Meetings!
// When you hit this URL, it'll call meetings() 5 times.
// Each time will have a random delay.
log.info("starting to work");
for (int i = 0; i < 5; i++) {
this.meeting();
}
log.info("finished!");
return "finished work!";
}
}
คุณเริ่มต้นแอปพลิเคชัน Spring Boot ได้ตามปกติด้วยปลั๊กอิน Spring Boot มาข้ามการทดสอบสําหรับห้องทดลองนี้กัน
$ ./mvnw -DskipTests spring-boot:run
เมื่อแอปพลิเคชันเริ่มทํางานแล้ว ให้คลิกไอคอนแสดงตัวอย่างเว็บในแถบเครื่องมือ Cloud Shell และเลือกแสดงตัวอย่างบนพอร์ต 8080
คุณจะเห็นผลลัพธ์หลังจากผ่านไปสักครู่
นอกจากนี้ คุณควรเห็นข้อความในบันทึกที่มีรหัสการติดตามและ span รหัสใน Cloud Shell:
4 การใช้ Stackdriver Trace
เปิดใช้ Stackdriver Trace API
คุณต้องเปิดใช้ Stackdriver Trace API ก่อนเพื่อใช้ Stackdriver Trace เพื่อเก็บข้อมูลการติดตาม หากต้องการเปิดใช้ API ให้ไปที่บริการ API → ไลบรารี
ค้นหา Stackdriver Trace
คลิก Stackdriver Trace API แล้วคลิกเปิดใช้หากยังไม่ได้เปิดใช้
ตั้งค่าข้อมูลเข้าสู่ระบบเริ่มต้นของแอปพลิเคชัน
สําหรับห้องทดลองนี้ คุณจะต้องกําหนดค่าข้อมูลรับรองแอปพลิเคชันเริ่มต้น ระบบจะใช้ Spring Cloud GCP Starter เริ่มต้นข้อมูลเข้าสู่ระบบนี้โดยอัตโนมัติ
ก่อนอื่น ให้เข้าสู่ระบบ:
$ gcloud auth application-default login
You are running on a Google Compute Engine virtual machine.
The service credentials associated with this virtual machine
will automatically be used by Application Default
Credentials, so it is not necessary to use this command.
If you decide to proceed anyway, your user credentials may be visible
to others with access to this virtual machine. Are you sure you want
to authenticate with your personal account?
Do you want to continue (Y/n)? Y
Go to the following link in your browser:
https://accounts.google.com/o/oauth2/auth...
Enter verification code: ...
คลิกลิงก์เพื่อเปิดแท็บเบราว์เซอร์ใหม่ แล้วคลิกอนุญาต
จากนั้นคัดลอกและวางรหัสยืนยันกลับไปยัง Cloud Shell แล้วกด Enter คุณควรเห็นสิ่งต่อไปนี้
Credentials saved to file: [/tmp/tmp.jm9bnQ4R9Q/application_default_credentials.json]
These credentials will be used by any library that requests
Application Default Credentials.
เพิ่มการติดตาม GCP ของ Spring Cloud
ในบริการนี้ เราใช้ Spring Cloud Sleuth ติดตามแล้ว มาเพิ่มเงื่อนไขเริ่มต้นของ Spring Cloud GCP Trace เพื่อส่งต่อข้อมูลไปยัง Stackdriver Trace
เพิ่มทรัพยากร Dependency ของ Spring Cloud GCP
pom.xml
<project>
...
<dependencies>
...
<!-- Add Stackdriver Trace Starter -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-trace</artifactId>
</dependency>
</dependencies>
...
</project>
โดยค่าเริ่มต้น Spring Cloud Sleuth จะไม่สุ่มตัวอย่างคําขอทั้งหมด เพื่อให้การทดสอบง่ายขึ้นเล็กน้อย ให้เพิ่มอัตราตัวอย่างเป็น 100% ใน application.properties
เพื่อให้แน่ใจว่าเราจะเห็นข้อมูลการติดตาม รวมถึงไม่สนใจ URL บางรายการที่เราไม่สนใจ ดังนี้
$ echo "
spring.sleuth.sampler.probability=1.0
spring.sleuth.web.skipPattern=(^cleanup.*|.+favicon.*)
" > src/main/resources/application.properties
เรียกใช้แอปพลิเคชันอีกครั้งและใช้ Cloud Shell Web Preview เพื่อดูแอปพลิเคชัน
$ export GOOGLE_CLOUD_PROJECT=`gcloud config list --format 'value(core.project)'`
$ ./mvnw -DskipTests spring-boot:run
โดยค่าเริ่มต้น Spring Cloud GCP Trace จะจัดกลุ่มข้อมูลติดตามและส่งออกทุกๆ 10 วินาที หรือเมื่อได้รับข้อมูลการติดตามตามจํานวนขั้นต่ํา ซึ่งจะกําหนดค่านี้ได้ในเอกสารอ้างอิงของ Spring Cloud GCP Trace
ส่งคําขอไปยังบริการ
$ curl localhost:8080
ใน Cloud Console ให้ไปที่ Stackdriver → การติดตาม → รายการการติดตาม
ที่ด้านบน ให้จํากัดช่วงเวลาให้แคบลงเป็น 1 ชั่วโมง โดยค่าเริ่มต้น การโหลดซ้ําอัตโนมัติจะเปิดอยู่ ดังนั้นเมื่อข้อมูลการติดตามมาถึง ข้อมูลจะปรากฏในคอนโซล
ข้อมูลการติดตามควรปรากฏภายในประมาณ 30 วินาที
คลิกจุดสีน้ําเงินเพื่อดูรายละเอียดการติดตาม
ง่ายมากเลยใช่ไหม
5 สร้างเว็บแอปพลิเคชันสําหรับ Spring Boot ครั้งที่ 2
เปิดเซสชัน Cloud Shell ใหม่โดยคลิกไอคอน + ดังนี้
ในเซสชันใหม่ ให้สร้างแอปพลิเคชัน Spring Boot ตัวที่ 2 ดังนี้
$ curl https://start.spring.io/starter.tgz -d packaging=jar \
-d dependencies=web,lombok,cloud-gcp,cloud-starter-sleuth \
-d baseDir=trace-service-two | tar -xzvf - \
&& cd trace-service-two
สร้างตัวควบคุม REST ใหม่โดยเพิ่มคลาสใหม่ ดังนี้
src/main/java/com/example/demo/MeetingController.java
package com.example.demo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Random;
@RestController
@Slf4j
public class MeetingController {
Random r = new Random();
@GetMapping("/meet")
public String meeting() {
try {
log.info("meeting...");
Thread.sleep(r.nextInt(500 - 20 + 1) + 20);
} catch (InterruptedException e) {
}
return "finished meeting";
}
}
เพิ่มการติดตาม GCP ของ Spring Cloud ไปยัง pom.xml
pom.xml
<project>
...
<dependencies>
...
<!-- Add Stackdriver Trace starter -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-trace</artifactId>
</dependency>
</dependencies>
...
</project>
กําหนดค่า Sleuth ให้ตัวอย่างคําขอ 100% ดังนี้
src/main/resources/application.properties
$ echo "
spring.sleuth.sampler.probability=1.0
spring.sleuth.web.skipPattern=(^cleanup.*|.+favicon.*)
" > src/main/resources/application.properties
สุดท้าย คุณสามารถเริ่มต้นแอปพลิเคชัน Spring Boot ในพอร์ต 8081 ได้ด้วยปลั๊กอิน Spring Boot ดังนี้
$ export GOOGLE_CLOUD_PROJECT=`gcloud config list --format 'value(core.project)'`
$ ./mvnw -DskipTests spring-boot:run -Dserver.port=8081
6 อัปเดตบริการแรกเพื่อใช้บริการที่สอง
ขณะที่ทํางานอยู่ trace-service-two
ให้กลับไปที่หน้าต่างเซสชันของ Cloud Shell เป็นครั้งแรก แล้วทําการแก้ไขของ trace-service-one
ก่อนอื่นให้เริ่มต้น RestTemplate
ถั่ว
src/main/java/com/example/demo/DemoApplication.java
package com.example.demo;
...
import org.springframework.web.client.RestTemplate;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class DemoApplication {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
ใน WorkController.meeting()
โปรดโทรออกไปยังบริการประชุม
src/main/java/com/example/demo/WorkController.java
package com.example.demo;
...
import org.springframework.web.client.RestTemplate;
import org.springframework.beans.factory.annotation.Autowired;
@RestController
@Slf4j
public class WorkController {
@Autowired
RestTemplate restTemplate;
public void meeting() {
String result = restTemplate.getForObject("http://localhost:8081/meet", String.class);
log.info(result);
}
...
}
เปิดบริการอีกครั้งและเรียกปลายทางจาก Web Preview โดยทําดังนี้
$ export GOOGLE_CLOUD_PROJECT=`gcloud config list --format 'value(core.project)'`
$ ./mvnw -DskipTests spring-boot:run
คุณควรจะเห็นบันทึกข้อความในหน้าต่างเซสชันทั้ง 2 รหัส โดยใช้รหัสการติดตามที่เผยแพร่จากบริการหนึ่งไปยังอีกบริการหนึ่ง
ในรายการการติดตามของ Stackdriver Trace' คุณจะเห็นการติดตามรายการที่สอง:
คุณสามารถคลิกจุดสีน้ําเงินใหม่และดูรายละเอียดการติดตามได้โดยทําดังนี้
คุณยังคลิกระยะเวลาใดก็ได้ในแผนภาพนี้เพื่อดูรายละเอียดของระยะเวลา
7 การกระจายเวลาในการตอบสนองและรายงานประสิทธิภาพ
เมื่อคุณใช้ Stackdriver Trace เป็นพื้นที่เก็บข้อมูลการติดตาม Stackdriver Trace จะใช้ข้อมูลเพื่อสร้างรายงานการกระจายเวลาในการตอบสนอง คุณต้องมีการติดตามมากกว่า 100 รายการเพื่อสร้างรายงานในลักษณะนี้
นอกจากนี้ Stackdriver Trace ยังตรวจหาการถดถอยประสิทธิภาพของบริการเดียวกันโดยอัตโนมัติใน 2 ช่วงเวลาที่ต่างกันในส่วนรายงานการวิเคราะห์
8 สรุป
ในห้องทดลองนี้คุณได้สร้างบริการที่เรียบง่าย 2 รายการและเพิ่มการติดตามการเผยแพร่ด้วย Spring Cloud Sleuth และใช้ Spring Cloud GCP เพื่อส่งต่อข้อมูลการติดตามไปยัง Stackdriver Trace..
9 ยินดีด้วย
คุณได้เรียนรู้วิธีเขียนเว็บแอปพลิเคชัน App Engine แรกแล้ว
ดูข้อมูลเพิ่มเติม
- การติดตาม Stackdriver: https://cloud.google.com/trace/
- Spring ในโปรเจ็กต์ GCP: http://cloud.spring.io/spring-cloud-gcp/
- ฤดูใบไม้ผลิบนที่เก็บ GitHub ของ GCP: https://github.com/spring-cloud/spring-cloud-gcp
- Java ใน Google Cloud Platform: https://cloud.google.com/java/
ใบอนุญาต
ผลงานนี้ได้รับอนุญาตภายใต้สัญญาอนุญาตทั่วไปของ Creative Commons Attribution 2.0