XU XUv0.16.0

#github.com/xulang-org/packages/nacos

导入方式: nacos #github.com/xulang-org/packages/nacos

公开 API: 3 函数, 18 方法, 3 类型, 0 常量, 0 方法集

扩展包 `nacos #github.com/xulang-org/packages/nacos`,由模块 github.com/xulang-org/packages 提供。

github.com/xulang-org/packages · 1.17.2 · 安装与使用

示例代码 · API 声明

示例代码

以下源码来自官方 examples 仓库,可复制或下载。请在完整工程中使用本地 Xu SDK 运行;网络、数据库和桌面示例需要对应的服务或资源。

43_standalone_nacos/main.xu

所需 SDK:Xu 0.8.3 · 完整工程 · 下载源码

Xu本地 SDK 示例
@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]")
}

API 声明

struct Response

XuAPI
pub struct Response {
    pub status_code int
    pub headers string
    pub body string
}

用途

Nacos 服务端响应结果,包含状态码、响应头与正文数据。

公开字段

status_code int
字段(int)
headers string
字段(string)
body string
正文

struct Client

XuAPI
pub struct Client {
    pub host string
    pub port int
    pub secure bool
    pub namespace_id string
}

用途

Nacos 客户端会话实例,维护与服务端的连接并提供操作接口。

公开字段

host string
主机
port int
端口
secure bool
字段(bool)
namespace_id string
字段(string)

new_client

XuAPI
pub fn new_client(endpoint string, namespace string) Client

用途

构造 client。

参数

endpoint string
endpoint(string)
namespace string
namespace(string)

返回值

Client — 该类型的值

(Client) request

XuAPI
pub fn (c Client) request(method string, path string, body string) Response

用途

Client 上的操作:请求。失败时抛出错误,调用方需处理或继续传播。

参数

c Client
本次调用使用的 Client 实例;按值接收。
method string
method(string)
path string
文件系统路径(string)
body string
正文(string)

返回值

Response — 该类型的值

(Client) publish_config

XuAPI
pub fn (c Client) publish_config(data_id string, group string, content string) bool

用途

Client 上的操作:执行 publish config 操作。

参数

c Client
本次调用使用的 Client 实例;按值接收。
data_id string
data_id(string)
group string
group(string)
content string
content(string)

返回值

bool — 是否成立

(Client) get_config

XuAPI
pub fn (c Client) get_config(data_id string, group string) string

用途

Client 上的操作:读取 config。失败时抛出错误,调用方需处理或继续传播。

参数

c Client
本次调用使用的 Client 实例;按值接收。
data_id string
data_id(string)
group string
group(string)

返回值

string — 结果字符串

(Client) remove_config

XuAPI
pub fn (c Client) remove_config(data_id string, group string) bool

用途

Client 上的操作:删除配置。

参数

c Client
本次调用使用的 Client 实例;按值接收。
data_id string
data_id(string)
group string
group(string)

返回值

bool — 是否成立

(Client) register_instance

XuAPI
pub fn (c Client) register_instance(service string, ip string, port int, group string) bool

用途

Client 上的操作:执行 register instance 操作。

参数

c Client
本次调用使用的 Client 实例;按值接收。
service string
service(string)
ip string
ip(string)
port int
端口(int)
group string
group(string)

返回值

bool — 是否成立

(Client) get_instances

XuAPI
pub fn (c Client) get_instances(service string, group string) string

用途

Client 上的操作:读取 instances。失败时抛出错误,调用方需处理或继续传播。

参数

c Client
本次调用使用的 Client 实例;按值接收。
service string
service(string)
group string
group(string)

返回值

string — 结果字符串

(Client) deregister_instance

XuAPI
pub fn (c Client) deregister_instance(service string, ip string, port int, group string) bool

用途

Client 上的操作:执行 deregister instance 操作。

参数

c Client
本次调用使用的 Client 实例;按值接收。
service string
service(string)
ip string
ip(string)
port int
端口(int)
group string
group(string)

返回值

bool — 是否成立

struct Pool

XuAPI
pub struct Pool {
    pub client Client
    pub state chan pool.State
    pub max_idle int
}

用途

Nacos 连接池管理器,并发安全地维护和调度复用套接字连接。

公开字段

client Client
字段(Client)
state chan pool.State
字段(通道;类型 chan pool.State)
max_idle int
字段(int)

new_pool

XuAPI
pub fn new_pool(endpoint string, namespace string, max_idle int) Pool

用途

构造 pool。

参数

endpoint string
endpoint(string)
namespace string
namespace(string)
max_idle int
max_idle(int)

返回值

Pool — 该类型的值

open_pool

XuAPI
pub fn open_pool(endpoint string, namespace string, max_idle int) Pool

用途

执行 open pool 操作。

参数

endpoint string
endpoint(string)
namespace string
namespace(string)
max_idle int
max_idle(int)

返回值

Pool — 该类型的值

查看用法示例

(&Pool) dup

XuAPI
pub fn (p &Pool) dup() Pool

用途

创建并返回持有独立状态或底层的副本对象。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

Pool — 该类型的值

(&Pool) get_conn

XuAPI
pub fn (p &Pool) get_conn() int

用途

&Pool 上的操作:读取 conn。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

int — 该类型的值

(&Pool) put_conn

XuAPI
pub fn (p &Pool) put_conn(fd int)

用途

&Pool 上的操作:执行 put conn 操作。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
fd int
文件描述符(int)

返回值

不返回值。

(&Pool) discard_conn

XuAPI
pub fn (p &Pool) discard_conn(fd int)

用途

&Pool 上的操作:丢弃连接。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
fd int
文件描述符(int)

返回值

不返回值。

(&Pool) register_instance

XuAPI
pub fn (p &Pool) register_instance(service string, ip string, port int, group string) bool

用途

&Pool 上的操作:执行 register instance 操作。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
service string
service(string)
ip string
ip(string)
port int
端口(int)
group string
group(string)

返回值

bool — 是否成立

(&Pool) get_instances

XuAPI
pub fn (p &Pool) get_instances(service string, group string) string

用途

&Pool 上的操作:读取 instances。失败时抛出错误,调用方需处理或继续传播。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
service string
service(string)
group string
group(string)

返回值

string — 结果字符串

(&Pool) deregister_instance

XuAPI
pub fn (p &Pool) deregister_instance(service string, ip string, port int, group string) bool

用途

&Pool 上的操作:执行 deregister instance 操作。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
service string
service(string)
ip string
ip(string)
port int
端口(int)
group string
group(string)

返回值

bool — 是否成立

(&Pool) publish_config

XuAPI
pub fn (p &Pool) publish_config(data_id string, group string, content string) bool

用途

&Pool 上的操作:执行 publish config 操作。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
data_id string
data_id(string)
group string
group(string)
content string
content(string)

返回值

bool — 是否成立

(&Pool) get_config

XuAPI
pub fn (p &Pool) get_config(data_id string, group string) string

用途

&Pool 上的操作:读取 config。失败时抛出错误,调用方需处理或继续传播。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
data_id string
data_id(string)
group string
group(string)

返回值

string — 结果字符串

(&Pool) remove_config

XuAPI
pub fn (p &Pool) remove_config(data_id string, group string) bool

用途

&Pool 上的操作:删除配置。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。
data_id string
data_id(string)
group string
group(string)

返回值

bool — 是否成立

(&Pool) close

XuAPI
pub fn (p &Pool) close()

用途

&Pool 上的操作:关闭资源。

参数

p &Pool
本次调用使用的 Pool 实例;以只读借用传入,调用方仍持有实例。

返回值

不返回值。