ZComposer 镜像 + COS 初体验

初步简单配置使用一下,其他的功能有空再看看。

安装

这边我使用的测试服务器是4核16GB,CentOS 7.6 系统。

1
2
3
4
5
6
7
8
9
10
# 安装 beanstalkd
$ yum install -y epel-release
$ yum install -y beanstalkd --enablerepo=epel
$ /usr/bin/beanstalkd -l 127.0.0.1 -p 11300 &

# 下载项目源码和安装依赖
$ git clone https://github.com/zencodex/composer-mirror.git
$ cd composer-mirror
$ composer install
$ composer require hongfs/flysystem-cos tencentcloud/tencentcloud-sdk-php

配置

1
$ cp config.default.php config.php

config.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php

return (object) [
...

/**
* packagistUrl:官方采集源
* 一般设置为官方源,尽量不要设置为其他非官方源,避免增加他们压力。
*/
'packagistUrl' => 'https://packagist.org',

/**
* 镜像包发布站点, packages.json 入口根域名
* 一般设置为自己的 CDN 域名
*/
'mirrorUrl' => 'https://composer.hongfs.cn',

/**
* .json 中 dist 分发 zip 包的CDN域名
* 一般设置为自己的 CDN 域名
*/
'distUrl' => 'https://composer.hongfs.cn/',

'cloudDisk' => (object) [
'adapter' => 'Hongfs\\Cos\\CosAdapter',
'config' => [
'secret_id' => '<secret_id>',
'secret_key' => '<secret_key>',
'region' => '<region>',
],
'bucketMap' => [
'json' => '<bucket-appid>',
'zip' => '<bucket-appid>',
]
],

/**
* CDN 配置
* https://cloud.tencent.com/product/cdn
*/
'cdn' => [
'secret_id' => '<secret_id>',
'secret_key' => '<secret_key>',
],

...
];

src/Cloud.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Cdn\V20180606\CdnClient;
use TencentCloud\Cdn\V20180606\Models\PurgeUrlsCacheRequest;

...

public function refreshRemoteFile($remoteUrl)
{
try {
$cred = new Credential($this->config->cdn['secret_id'], $this->config->cdn['secret_key']);
$httpProfile = new HttpProfile();
$clientProfile = new ClientProfile();
$clientProfile->setHttpProfile($httpProfile);
$client = new CdnClient($cred, "", $clientProfile);

$req = new PurgeUrlsCacheRequest();

$params = '{"Urls":["' . $remoteUrl . '"]}';
$req->fromJsonString($params);

$client->PurgeUrlsCache($req);
Log::debug("refreshCdnCache => $remoteUrl \n");
} catch (\TencentCloudSDKException $e) {
Log::error('refreshCdnCache => '. $e->getMessage());
}
}

运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 执行抓取任务
$ php ./bin/console app:crawler

# 后台多进程模型同步 COS
$ php ./bin/console app:daemon

# 清理过期垃圾文件
$ php ./bin/console app:clear --expired=json

# 扫描并校验所有json和zip文件的hash256
$ php ./bin/console app:scan

# 遍历云储存的文件,并缓存在本地,用于对比差异
# 还没实现
$ php ./bin/console app:rainbow

composer 配置

1
$ composer config -g repo.packagist composer https://composer.hongfs.cn/

其他

文档中专门提到了 ClientHandlerPlugin,里面 refreshRemoteFile 是用来刷新文件,这边我们需要通过腾讯云的SDK来调用 CDN 相关接口进行处理;prefetchDistFile 这个是提供 URL 然后直接下载到对象存储的接口,不过 COS 这边没有提供所以跳过不管。

往上