Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nacos-config
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Euan游根明
nacos-config
Commits
9c49628b
Commit
9c49628b
authored
Nov 30, 2023
by
Euan游根明
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
perf:优化拉取配置
parent
42f2c9a6
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
102 additions
and
58 deletions
+102
-58
nacos-client
nacos-client
+1
-1
Nacos.php
src/Nacos/Nacos.php
+101
-57
No files found.
nacos-client
View file @
9c49628b
...
@@ -15,7 +15,7 @@ if (isset($GLOBALS['_composer_autoload_path'])) {
...
@@ -15,7 +15,7 @@ if (isset($GLOBALS['_composer_autoload_path'])) {
unset
(
$file
);
unset
(
$file
);
}
}
$options
=
getopt
(
''
,
array
(
'prepend:'
)
);
$options
=
getopt
(
''
,
[
'base_path:'
,
'env.nacos:'
,
'env:'
]
);
require
NACOS_COMPOSER_INSTALL
;
require
NACOS_COMPOSER_INSTALL
;
...
...
src/Nacos/Nacos.php
View file @
9c49628b
...
@@ -5,54 +5,56 @@ namespace Galaxy\NacosClient\Nacos;
...
@@ -5,54 +5,56 @@ namespace Galaxy\NacosClient\Nacos;
class
Nacos
class
Nacos
{
{
public
static
array
$configs
=
[];
public
static
$configs
=
[];
public
static
$basePath
=
''
;
public
static
function
run
(
$options
)
public
static
function
run
(
$options
)
{
{
var_dump
(
$options
);
$res
=
self
::
configure
(
$options
);
die
;
if
(
empty
(
$res
)){
self
::
configure
();
return
;
$this
->
info
(
'开始拉取配置文件'
);
}
self
::
info
(
'开始拉取配置文件'
);
$url
=
self
::
$configs
[
'NACOS_URL'
]
??
''
;
$url
=
self
::
$configs
[
'NACOS_URL'
]
??
''
;
$group
=
self
::
$configs
[
'GROUP'
]
??
''
;
$group
=
self
::
$configs
[
'GROUP'
]
??
''
;
if
(
empty
(
$url
)){
if
(
empty
(
$url
)){
$this
->
info
(
'无nacos链接'
);
self
::
info
(
'无nacos链接'
);
return
;
return
;
}
}
$appName
=
env
(
'APP_NAME'
);
$modelPath
=
self
::
$basePath
.
'.env'
;
$appEnv
=
env
(
'APP_ENV'
);
$envFile
=
file_get_contents
(
$modelPath
);
if
(
$appName
!=
$group
)
{
$configGroup
=
'APP_NAME='
.
$group
;
$this
->
info
(
$group
.
'应用不匹配'
);
if
(
!
preg_match
(
"/
$configGroup
/"
,
$envFile
))
{
self
::
info
(
$group
.
'应用不匹配'
);
return
;
return
;
}
}
$env
=
'.env'
;
$modelPath
=
base_path
()
.
'/'
.
$env
;
$key
=
$group
.
'-env-'
.
self
::
$configs
[
'APP_ENV'
];
$key
=
$group
.
'-env-'
.
$appEnv
;
$res
=
self
::
getConfig
(
$key
,
$group
);
$res
=
$this
->
getConfig
(
$key
,
$group
);
if
(
empty
(
$res
))
{
if
(
empty
(
$res
))
{
$log
=
$key
.
'配置不存在'
;
$log
=
$key
.
'配置不存在'
;
$this
->
info
(
$log
);
self
::
info
(
$log
);
return
;
return
;
}
}
try
{
try
{
$cacheKey
=
'fileMd5'
.
$key
;
$cacheKey
=
'fileMd5'
.
$key
;
$md5File
=
md5
(
$res
);
$md5File
=
md5
(
$res
);
$oldMd5
=
Nacos
::
Nacos
g
et
(
$cacheKey
);
$oldMd5
=
Nacos
::
Nacos
G
et
(
$cacheKey
);
if
(
!
$oldMd5
)
{
if
(
!
$oldMd5
)
{
$this
->
write
(
$modelPath
,
$res
);
self
::
write
(
$modelPath
,
$res
);
Nacos
::
Nacos
p
ut
(
$cacheKey
,
$md5File
);
Nacos
::
Nacos
P
ut
(
$cacheKey
,
$md5File
);
$logs
=
'首次获取配置'
.
$key
;
$logs
=
'首次获取配置'
.
$key
;
$this
->
info
(
$logs
);
self
::
info
(
$logs
);
return
;
return
;
}
}
$logs
=
$key
.
'文件未发生变更'
.
PHP_EOL
;
$logs
=
$key
.
'文件未发生变更'
.
PHP_EOL
;
if
(
$oldMd5
!=
$md5File
)
{
if
(
$oldMd5
!=
$md5File
)
{
$logs
=
$key
.
'文件发生变更,修改配置项'
.
PHP_EOL
;
$logs
=
$key
.
'文件发生变更,修改配置项'
.
PHP_EOL
;
Nacos
::
Nacos
p
ut
(
$cacheKey
,
$md5File
);
Nacos
::
Nacos
P
ut
(
$cacheKey
,
$md5File
);
$this
->
write
(
$modelPath
,
$res
);
self
::
write
(
$modelPath
,
$res
);
}
}
$this
->
info
(
$logs
);
self
::
info
(
$logs
);
}
catch
(
\Exception
$e
)
{
}
catch
(
\Exception
$e
)
{
$this
->
info
(
'获取配置失败'
.
$key
.
': '
.
$e
);
self
::
info
(
'获取配置失败'
.
$key
.
': '
);
}
}
}
}
...
@@ -63,7 +65,7 @@ class Nacos
...
@@ -63,7 +65,7 @@ class Nacos
* @return void
* @return void
* @throws Exception
* @throws Exception
*/
*/
public
function
write
(
$path
,
$res
){
public
static
function
write
(
$path
,
$res
){
$save
=
file_put_contents
(
$path
,
$res
);
$save
=
file_put_contents
(
$path
,
$res
);
if
(
$save
===
false
){
if
(
$save
===
false
){
throw
new
Exception
(
'保存配置失败'
.
$path
);
throw
new
Exception
(
'保存配置失败'
.
$path
);
...
@@ -77,17 +79,17 @@ class Nacos
...
@@ -77,17 +79,17 @@ class Nacos
* @param $isSync
* @param $isSync
* @return false|\GuzzleHttp\Promise\PromiseInterface|mixed|string
* @return false|\GuzzleHttp\Promise\PromiseInterface|mixed|string
*/
*/
public
function
getConfig
(
$dataId
,
$group
=
'DEFAULT_GROUP'
)
public
static
function
getConfig
(
$dataId
,
$group
=
'DEFAULT_GROUP'
)
{
{
try
{
try
{
return
$this
->
requery
(
$dataId
,
$group
);
return
self
::
requery
(
$dataId
,
$group
);
}
catch
(
\Exception
$e
)
{
}
catch
(
\Exception
$e
)
{
$code
=
$e
->
getCode
();
$code
=
$e
->
getCode
();
if
(
$code
==
404
)
{
if
(
$code
==
403
)
{
return
false
;
self
::
getToken
(
true
);
return
self
::
requery
(
$dataId
,
$group
);
}
}
$this
->
getToken
(
true
);
return
false
;
return
$this
->
requery
(
$dataId
,
$group
);
}
}
}
}
...
@@ -99,10 +101,10 @@ class Nacos
...
@@ -99,10 +101,10 @@ class Nacos
* @return \GuzzleHttp\Promise\PromiseInterface|mixed|string
* @return \GuzzleHttp\Promise\PromiseInterface|mixed|string
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
*/
public
function
requery
(
$dataId
,
$group
)
public
static
function
requery
(
$dataId
,
$group
)
{
{
$url
=
self
::
$configs
[
'NACOS_URL'
]
.
'/nacos/v2/cs/config?accessToken='
.
$this
->
getToken
()
.
'&dataId='
.
$dataId
.
'&group='
.
$group
;
$url
=
self
::
$configs
[
'NACOS_URL'
]
.
'/nacos/v2/cs/config?accessToken='
.
self
::
getToken
()
.
'&dataId='
.
$dataId
.
'&group='
.
$group
;
return
$this
->
getConfigRequest
(
$url
);
return
self
::
getConfigRequest
(
$url
);
}
}
/**
/**
...
@@ -111,18 +113,18 @@ class Nacos
...
@@ -111,18 +113,18 @@ class Nacos
* @return mixed|string|null
* @return mixed|string|null
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
*/
private
function
getToken
(
$new
=
false
)
private
static
function
getToken
(
$new
=
false
)
{
{
$key
=
'nacos:sync:get:token'
;
$key
=
'nacos:sync:get:token'
;
$token
=
Nacos
::
Nacos
g
et
(
$key
);;
$token
=
Nacos
::
Nacos
G
et
(
$key
);;
if
(
empty
(
$token
)
||
$new
)
{
if
(
empty
(
$token
)
||
$new
)
{
$user
=
self
::
$configs
[
'ACCOUNT'
];
$user
=
self
::
$configs
[
'ACCOUNT'
];
$pass
=
self
::
$configs
[
'NACOS_PWD'
];
$pass
=
self
::
$configs
[
'NACOS_PWD'
];
$url
=
self
::
$configs
[
'NACOS_URL'
]
.
'/nacos/v1/auth/login'
;
$url
=
self
::
$configs
[
'NACOS_URL'
]
.
'/nacos/v1/auth/login'
;
$data
=
[
'username'
=>
$user
,
'password'
=>
$pass
];
$data
=
[
'username'
=>
$user
,
'password'
=>
$pass
];
$token
=
$this
->
login
(
$data
,
$url
);
$token
=
self
::
login
(
$data
,
$url
);
$logs
=
'获取token: '
.
$token
;
$logs
=
'获取token: '
.
$token
;
$this
->
info
(
$logs
);
self
::
info
(
$logs
);
}
}
Nacos
::
NacosPut
(
$key
,
$token
);
Nacos
::
NacosPut
(
$key
,
$token
);
return
$token
;
return
$token
;
...
@@ -133,7 +135,7 @@ class Nacos
...
@@ -133,7 +135,7 @@ class Nacos
* @param $url
* @param $url
* @return mixed|string
* @return mixed|string
*/
*/
public
function
getConfigRequest
(
$url
){
public
static
function
getConfigRequest
(
$url
){
// 创建一个cURL资源
// 创建一个cURL资源
$curl
=
curl_init
();
$curl
=
curl_init
();
curl_setopt_array
(
$curl
,
array
(
curl_setopt_array
(
$curl
,
array
(
...
@@ -152,10 +154,17 @@ class Nacos
...
@@ -152,10 +154,17 @@ class Nacos
$response
=
curl_exec
(
$curl
);
$response
=
curl_exec
(
$curl
);
$err
=
curl_error
(
$curl
);
$err
=
curl_error
(
$curl
);
if
(
$err
){
if
(
$err
){
$this
->
info
(
$err
);
self
::
info
(
$err
);
}
}
curl_close
(
$curl
);
curl_close
(
$curl
);
$data
=
json_decode
(
$response
,
true
);
$data
=
json_decode
(
$response
,
true
);
if
(
isset
(
$data
[
'status'
])
&&
$data
[
'status'
]
==
403
){
throw
new
Exception
(
'token过期'
,
403
);
}
if
(
isset
(
$data
[
'code'
])
&&
$data
[
'code'
]
!==
0
){
self
::
info
(
'获取配置返回结果:'
.
$response
);
return
''
;
}
return
$data
[
'data'
]
??
''
;
return
$data
[
'data'
]
??
''
;
}
}
...
@@ -165,7 +174,7 @@ class Nacos
...
@@ -165,7 +174,7 @@ class Nacos
* @return mixed|string|void
* @return mixed|string|void
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
*/
private
function
login
(
$data
,
$url
)
private
static
function
login
(
$data
,
$url
)
{
{
try
{
try
{
$curl
=
curl_init
();
$curl
=
curl_init
();
...
@@ -185,14 +194,14 @@ class Nacos
...
@@ -185,14 +194,14 @@ class Nacos
$response
=
curl_exec
(
$curl
);
$response
=
curl_exec
(
$curl
);
$err
=
curl_error
(
$curl
);
$err
=
curl_error
(
$curl
);
if
(
$err
){
if
(
$err
){
$this
->
info
(
$err
);
self
::
info
(
$err
);
}
}
curl_close
(
$curl
);
curl_close
(
$curl
);
// 处理响应
// 处理响应
$loginDataArr
=
json_decode
(
$response
,
true
);
$loginDataArr
=
json_decode
(
$response
,
true
);
return
$loginDataArr
[
'accessToken'
]
??
''
;
return
$loginDataArr
[
'accessToken'
]
??
''
;
}
catch
(
\Exception
$e
)
{
}
catch
(
\Exception
$e
)
{
$this
->
info
(
$e
->
getMessage
());
self
::
info
(
$e
->
getMessage
());
}
}
}
}
...
@@ -202,7 +211,7 @@ class Nacos
...
@@ -202,7 +211,7 @@ class Nacos
* @return mixed|string
* @return mixed|string
*/
*/
public
static
function
NacosGet
(
$key
){
public
static
function
NacosGet
(
$key
){
$path
=
Nacos
::
getPath
(
'/.nacos.cache'
);
$path
=
self
::
getPath
(
sys_get_temp_dir
(),
'/nacos.cache-'
.
md5
(
self
::
$configs
[
'GROUP'
])
);
$data
=
json_decode
(
$data
=
json_decode
(
file_get_contents
(
$path
),
file_get_contents
(
$path
),
true
,
true
,
...
@@ -220,7 +229,7 @@ class Nacos
...
@@ -220,7 +229,7 @@ class Nacos
* @return void
* @return void
*/
*/
public
static
function
NacosPut
(
$key
,
$value
){
public
static
function
NacosPut
(
$key
,
$value
){
$path
=
Nacos
::
getPath
(
'/.nacos.cache'
);
$path
=
self
::
getPath
(
sys_get_temp_dir
(),
'/nacos.cache-'
.
md5
(
self
::
$configs
[
'GROUP'
])
);
$data
=
json_decode
(
$data
=
json_decode
(
file_get_contents
(
$path
),
file_get_contents
(
$path
),
true
,
true
,
...
@@ -237,9 +246,12 @@ class Nacos
...
@@ -237,9 +246,12 @@ class Nacos
* @param $msg
* @param $msg
* @return void
* @return void
*/
*/
private
function
info
(
$msg
)
private
static
function
info
(
$msg
)
{
{
Log
::
info
(
$msg
);
self
::
writeLog
(
$msg
);
if
(
!
is_string
(
$msg
)){
$msg
=
$msg
[
'msg'
];
}
echo
$msg
.
"
\n
"
;
echo
$msg
.
"
\n
"
;
}
}
...
@@ -248,23 +260,55 @@ class Nacos
...
@@ -248,23 +260,55 @@ class Nacos
* @param $fileName
* @param $fileName
* @return string
* @return string
*/
*/
public
static
function
getPath
(
$fileName
){
public
static
function
getPath
(
$path
,
$fileName
){
return
base_path
()
.
$fileName
;
if
(
!
file_exists
(
$path
))
{
mkdir
(
$path
,
0775
,
true
);
touch
(
$fileName
);
}
if
(
!
file_exists
(
$path
.
$fileName
))
{
touch
(
$fileName
);
}
return
$path
.
$fileName
;
}
}
/**
/**
* 获取nacos
* 获取nacos
* @return
void
* @return
bool
*/
*/
public
static
function
configure
()
public
static
function
configure
(
$options
)
{
{
$path
=
Nacos
::
getPath
(
'/.env.nacos'
);
$envNacos
=
$options
[
'env.nacos'
];
try
{
self
::
$basePath
=
$options
[
'base_path'
];
$data
=
parse_ini_file
(
$path
);
$file
=
self
::
$basePath
.
$envNacos
;
}
catch
(
\Exception
$ex
){
$data
=
parse_ini_file
(
$file
);
\Log
::
info
(
$ex
->
getMessage
());
if
(
empty
(
$data
))
{
$data
=
[];
self
::
info
(
'输入参数有误:'
.
$file
);
return
false
;
}
}
Nacos
::
$configs
=
$data
;
Nacos
::
$configs
=
$data
;
return
true
;
}
/**
* 写入日志
* @return void
*/
public
static
function
writeLog
(
$logData
){
$logFile
=
'/nacos-'
.
date
(
'Y-m-d'
)
.
'.log'
;
$path
=
self
::
getPath
(
sys_get_temp_dir
(),
$logFile
);
$data
=
json_decode
(
file_get_contents
(
$path
),
true
,
);
if
(
!
is_string
(
$logData
)){
$logData
=
json_encode
(
$logData
,);
}
$data
[]
=
$logData
;
file_put_contents
(
$path
,
json_encode
(
$data
),
);
}
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment