struct Response
pub struct Response {
pub status_code int
pub headers string
pub body string
}用途
Nacos 服务端响应结果,包含状态码、响应头与正文数据。
公开字段
status_codeint- 字段(int)
headersstring- 字段(string)
bodystring- 正文
#github.com/xulang-org/packages/nacos扩展包 `nacos #github.com/xulang-org/packages/nacos`,由模块 github.com/xulang-org/packages 提供。
github.com/xulang-org/packages · 1.17.2 · 安装与使用
以下源码来自官方 examples 仓库,可复制或下载。请在完整工程中使用本地 Xu SDK 运行;网络、数据库和桌面示例需要对应的服务或资源。
43_standalone_nacos/main.xu@main
nacos #github.com/xulang-org/packages/nacos
#errors
#strings
fn verify_nacos_pool() {
println("--- 1. Testing Nacos Connection Pool ---")
$p := nacos.open_pool("http://127.0.0.1:8848", "public", 5)
// 1. Connection Reuse
fd1 := p.get_conn()
println(" Borrow 1 fd: " + str(fd1))
p.put_conn(fd1)
fd2 := p.get_conn()
println(" Borrow 2 (after return) fd: " + str(fd2))
if fd1 == fd2 {
println(" [PASS] Connection reused successfully (fd " + str(fd1) + " == " + str(fd2) + ")")
} else {
println(" [WARN] Different fd allocated: " + str(fd1) + " vs " + str(fd2))
}
// 2. Multi-connection concurrent borrow
fd3 := p.get_conn()
println(" Borrow 3 (concurrently) fd: " + str(fd3))
if fd2 != fd3 {
println(" [PASS] Concurrently allocated distinct fds (" + str(fd2) + " != " + str(fd3) + ")")
} else {
println(" [FAIL] Expected distinct fds for concurrent connections!")
}
p.put_conn(fd2)
p.put_conn(fd3)
p.close()
println(" [PASS] Nacos Pool closed cleanly.")
}
fn verify_nacos_naming() {
println("\n--- 2. Testing Nacos Naming Service (Registration & Discovery) ---")
$p := nacos.open_pool("http://127.0.0.1:8848", "public", 5)
srv := "xu-payment-service"
ip := "10.0.0.88"
port := 8080
// 1. Register Instance
reg_ok := p.register_instance(srv + "", ip + "", port, "DEFAULT_GROUP")
if reg_ok {
println(" [PASS] Microservice instance registered (" + srv + " -> " + ip + ":" + str(port) + ")")
} else {
println(" [FAIL] Failed to register instance")
}
// 2. Discover Instances
instances := p.get_instances(srv + "", "DEFAULT_GROUP") >>> (err) {
println(" [FAIL] get_instances failed: " + err.msg())
p.close()
<<
}
println(" [PASS] Service discovery instances json length: " + str(instances.len) + " bytes")
// 3. Deregister Instance
dereg_ok := p.deregister_instance(srv + "", ip + "", port, "DEFAULT_GROUP")
if dereg_ok {
println(" [PASS] Microservice instance deregistered.")
} else {
println(" [FAIL] Failed to deregister instance")
}
p.close()
}
fn verify_nacos_config() {
println("\n--- 3. Testing Nacos Config Service (Publish & Read) ---")
$p := nacos.open_pool("http://127.0.0.1:8848", "public", 5)
data_id := "xu-dynamic-config.yaml"
group := "DEFAULT_GROUP"
content := "server.port=9090\nenv=production"
// 1. Publish Config
pub_ok := p.publish_config(data_id + "", group + "", content + "")
if pub_ok {
println(" [PASS] Dynamic config published (" + data_id + ")")
} else {
println(" [FAIL] Failed to publish config")
}
// 2. Get Config
got_cfg := p.get_config(data_id + "", group + "") >>> (err) {
println(" [FAIL] get_config failed: " + err.msg())
p.close()
<<
}
println(" Retrieved config: " + got_cfg)
if got_cfg == content {
println(" [PASS] Retrieved config matched exactly!")
} else {
println(" [FAIL] Config content mismatch!")
}
// 3. Remove Config
rm_ok := p.remove_config(data_id + "", group + "")
if rm_ok {
println(" [PASS] Config removed cleanly.")
} else {
println(" [FAIL] Failed to remove config")
}
p.close()
}
fn main() {
println("=== Xu Nacos Microservice Driver Verification ===\n")
verify_nacos_pool()
verify_nacos_naming()
verify_nacos_config()
println("\n[NACOS VERIFICATION 100% COMPLETED]")
}struct Responsepub struct Response {
pub status_code int
pub headers string
pub body string
}Nacos 服务端响应结果,包含状态码、响应头与正文数据。
status_code intheaders stringbody stringstruct Clientpub struct Client {
pub host string
pub port int
pub secure bool
pub namespace_id string
}Nacos 客户端会话实例,维护与服务端的连接并提供操作接口。
host stringport intsecure boolnamespace_id stringnew_clientpub fn new_client(endpoint string, namespace string) Client构造 client。
endpoint stringnamespace stringClient — 该类型的值
(Client) requestpub fn (c Client) request(method string, path string, body string) ResponseClient 上的操作:请求。失败时抛出错误,调用方需处理或继续传播。
c ClientClient 实例;按值接收。method stringpath stringbody stringResponse — 该类型的值
(Client) publish_configpub fn (c Client) publish_config(data_id string, group string, content string) boolClient 上的操作:执行 publish config 操作。
c ClientClient 实例;按值接收。data_id stringgroup stringcontent stringbool — 是否成立
(Client) get_configpub fn (c Client) get_config(data_id string, group string) stringClient 上的操作:读取 config。失败时抛出错误,调用方需处理或继续传播。
c ClientClient 实例;按值接收。data_id stringgroup stringstring — 结果字符串
(Client) remove_configpub fn (c Client) remove_config(data_id string, group string) boolClient 上的操作:删除配置。
c ClientClient 实例;按值接收。data_id stringgroup stringbool — 是否成立
(Client) register_instancepub fn (c Client) register_instance(service string, ip string, port int, group string) boolClient 上的操作:执行 register instance 操作。
c ClientClient 实例;按值接收。service stringip stringport intgroup stringbool — 是否成立
(Client) get_instancespub fn (c Client) get_instances(service string, group string) stringClient 上的操作:读取 instances。失败时抛出错误,调用方需处理或继续传播。
c ClientClient 实例;按值接收。service stringgroup stringstring — 结果字符串
(Client) deregister_instancepub fn (c Client) deregister_instance(service string, ip string, port int, group string) boolClient 上的操作:执行 deregister instance 操作。
c ClientClient 实例;按值接收。service stringip stringport intgroup stringbool — 是否成立
struct Poolpub struct Pool {
pub client Client
pub state chan pool.State
pub max_idle int
}Nacos 连接池管理器,并发安全地维护和调度复用套接字连接。
client Clientstate chan pool.Statemax_idle intnew_poolpub fn new_pool(endpoint string, namespace string, max_idle int) Pool构造 pool。
endpoint stringnamespace stringmax_idle intPool — 该类型的值
open_poolpub fn open_pool(endpoint string, namespace string, max_idle int) Pool执行 open pool 操作。
endpoint stringnamespace stringmax_idle intPool — 该类型的值
(&Pool) duppub fn (p &Pool) dup() Pool创建并返回持有独立状态或底层的副本对象。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。Pool — 该类型的值
(&Pool) get_connpub fn (p &Pool) get_conn() int&Pool 上的操作:读取 conn。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。int — 该类型的值
(&Pool) put_connpub fn (p &Pool) put_conn(fd int)&Pool 上的操作:执行 put conn 操作。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。fd int不返回值。
(&Pool) discard_connpub fn (p &Pool) discard_conn(fd int)&Pool 上的操作:丢弃连接。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。fd int不返回值。
(&Pool) register_instancepub fn (p &Pool) register_instance(service string, ip string, port int, group string) bool&Pool 上的操作:执行 register instance 操作。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。service stringip stringport intgroup stringbool — 是否成立
(&Pool) get_instancespub fn (p &Pool) get_instances(service string, group string) string&Pool 上的操作:读取 instances。失败时抛出错误,调用方需处理或继续传播。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。service stringgroup stringstring — 结果字符串
(&Pool) deregister_instancepub fn (p &Pool) deregister_instance(service string, ip string, port int, group string) bool&Pool 上的操作:执行 deregister instance 操作。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。service stringip stringport intgroup stringbool — 是否成立
(&Pool) publish_configpub fn (p &Pool) publish_config(data_id string, group string, content string) bool&Pool 上的操作:执行 publish config 操作。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。data_id stringgroup stringcontent stringbool — 是否成立
(&Pool) get_configpub fn (p &Pool) get_config(data_id string, group string) string&Pool 上的操作:读取 config。失败时抛出错误,调用方需处理或继续传播。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。data_id stringgroup stringstring — 结果字符串
(&Pool) remove_configpub fn (p &Pool) remove_config(data_id string, group string) bool&Pool 上的操作:删除配置。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。data_id stringgroup stringbool — 是否成立
(&Pool) closepub fn (p &Pool) close()&Pool 上的操作:关闭资源。
p &PoolPool 实例;以只读借用传入,调用方仍持有实例。不返回值。