课程进度 92% · 第21/22章第21/22章 · 标签 1/6
— 1 —
云原生基础
云原生是一种构建和运行应用程序的方法,充分利用容器化、微服务架构和自动化管理来构建弹性、可扩展的应用。
- 云原生概念:面向云环境设计的应用架构
- 容器化技术:Docker容器封装应用及其依赖
- 微服务架构:将应用拆分为独立部署的小服务
```bash # .env配置 APP_ENV=production APP_DEBUG=false DB_CONNECTION=mysql DB_HOST=mysql DB_PORT=3306 DB_DATABASE=app DB_USERNAME=root DB_PASSWORD=password ```
bash
1
# .env配置
2
APP_ENV=production
3
APP_DEBUG=false
4
DB_CONNECTION=mysql
5
DB_HOST=mysql
6
DB_PORT=3306
7
DB_DATABASE=app
8
DB_USERNAME=root
9
DB_PASSWORD=password
— 2 —
健康检查与配置管理
```php
<?php
// 健康检查控制器
class HealthCheckController extends Controller
{
public function check()
{
return response()->json([
"status" => "healthy",
"timestamp" => time(),
"services" => [
"database" => $this->checkDatabase(),
"cache" => $this->checkCache(),
"storage" => $this->checkStorage()
]
]);
}
private function checkDatabase()
{
try {
DB::connection()->getPdo();
return "healthy";
} catch (\Exception $e) {
return "unhealthy";
}
}
private function checkCache()
{
try {
Cache::put("health_check", "ok", 1);
return Cache::get("health_check") === "ok" ? "healthy" : "unhealthy";
} catch (\Exception $e) {
return "unhealthy";
}
}
private function checkStorage()
{
try {
Storage::disk("local")->put("health_check.txt", "ok");
return Storage::disk("local")->get("health_check.txt") === "ok" ? "healthy" : "unhealthy";
} catch (\Exception $e) {
return "unhealthy";
}
}
}
?>
```
php
1
2
// 健康检查控制器
3
class HealthCheckController extends Controller
4
{
5
public function check()
6
{
7
return response()->json([
8
"status" => "healthy",
9
"timestamp" => time(),
10
"services" => [
11
"database" => $this->checkDatabase(),
12
"cache" => $this->checkCache(),
13
"storage" => $this->checkStorage()
14
]
15
]);
16
}
17
18
private function checkDatabase()
19
{
20
try {
21
DB::connection()->getPdo();
22
return "healthy";
23
} catch (\Exception $e) {
24
return "unhealthy";
25
}
26
}
27
28
private function checkCache()
29
{
30
try {
31
Cache::put("health_check", "ok", 1);
32
return Cache::get("health_check") === "ok" ? "healthy" : "unhealthy";
33
} catch (\Exception $e) {
34
return "unhealthy";
35
}
36
}
37
38
private function checkStorage()
39
{
40
try {
41
Storage::disk("local")->put("health_check.txt", "ok");
42
return Storage::disk("local")->get("health_check.txt") === "ok" ? "healthy" : "unhealthy";
43
} catch (\Exception $e) {
44
return "unhealthy";
45
}
46
}
47
}
48