การรีเฟรชการกำหนดค่าขณะทำงาน
อัปเดตการกำหนดค่าโดยไม่ต้องเริ่มบริการใหม่
การรีเฟรชการกำหนดค่าขณะทำงาน เป็นบทเรียน Spring Boot 4 Microservices & REST APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Spring Boot 4 Microservices & REST APIs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Config changes without restarts
By default clients read config only at startup. To apply a changed value without restarting, Spring Cloud provides runtime refresh.
The Actuator refresh endpoint
Add Spring Boot Actuator and expose the refresh endpoint. Posting to it makes the client re-fetch config from the server.
management:
endpoints:
web:
exposure:
include: refreshTriggering a refresh
A POST to /actuator/refresh reloads the environment and returns the list of changed property keys.
// POST http://localhost:8080/actuator/refresh
// response: ["app.greeting"] <- keys that changed@RefreshScope
Only beans annotated with @RefreshScope are re-created on refresh, picking up new values. Without it, a bean keeps the value it got at startup.
@RefreshScope
@Component
public class GreetingService {
@Value("${app.greeting}")
private String greeting; // updates after /actuator/refresh
}What gets updated
On refresh, @ConfigurationProperties beans are rebound automatically (no @RefreshScope needed), while @Value-based beans need @RefreshScope to see new values.
How @RefreshScope works
@RefreshScope wraps the bean in a proxy. On refresh the underlying instance is discarded and lazily re-created on next use, so it reads fresh config.
Refresh is per-instance
Calling /actuator/refresh updates only that one instance. In a cluster you must hit every instance - which does not scale well.
Spring Cloud Bus
Spring Cloud Bus links instances over a message broker (RabbitMQ/Kafka). A single /actuator/busrefresh then broadcasts the refresh to all instances at once.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>Automating with webhooks
Connect your Git provider's webhook to the Config Server's monitor endpoint so a push automatically triggers a bus refresh across the fleet - no manual call needed.
What cannot be refreshed live
Some settings only take effect at boot - e.g. server port, datasource URL for an already-built pool, or component scanning. Changing those still requires a restart.
Refresh safely
Treat refresh like a deploy: validate config in Git first, roll out gradually, and watch the returned changed-keys list to confirm only intended values changed.
Quick Check
Test your runtime-refresh knowledge.
Recap
You refreshed config at runtime:
- Expose and POST
/actuator/refresh @RefreshScopere-creates@Valuebeans;@ConfigurationPropertiesrebind automatically- Refresh is per-instance; Spring Cloud Bus broadcasts to all
- Some boot-time settings still need a restart
คำถามที่พบบ่อย
บทเรียน “การรีเฟรชการกำหนดค่าขณะทำงาน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การรีเฟรชการกำหนดค่าขณะทำงาน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Boot 4 Microservices & REST APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การรีเฟรชการกำหนดค่าขณะทำงาน”
อัปเดตการกำหนดค่าโดยไม่ต้องเริ่มบริการใหม่ คุณปฏิบัติ Spring Boot 4 Microservices & REST APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Boot 4 Microservices & REST APIs หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Boot 4 Microservices & REST APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การรีเฟรชการกำหนดค่าขณะทำงาน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Spring Boot 4 Microservices & REST APIs นี้ได้ไหม
ได้ บทเรียน Spring Boot 4 Microservices & REST APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เซิร์ฟเวอร์การกำหนดค่า
- การกำหนดค่าที่มี Git รองรับ
- ไคลเอนต์การกำหนดค่า
- การรีเฟรชการกำหนดค่าขณะทำงาน