XU XUv0.16.0

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

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

counts: 3 functions, 18 methods, 3 types, 0 constants, 0 method sets

Alibaba Nacos Service Registration and Dynamic Configuration Client for Xu Supports OpenAPI v1/v2 service registration, discovery, heartbeat, and config publishing.

github.com/xulang-org/packages · 1.17.2 · Install and use

Examples · API declarations

Examples

These sources come from the official examples repository and can be copied or downloaded. Run them with a local Xu SDK in the complete project; network, database, and desktop examples require the corresponding services or assets.

43_standalone_nacos/main.xu

Required SDK: Xu 0.8.3 · Complete project · Download source

XuLocal SDK example
@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 declarations

struct Response

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

Purpose

Nacos Server response payload containing status, headers, and body.

Public fields

status_code int
field (int)
headers string
field (string)
body string
body

struct Client

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

Purpose

Nacos Client session maintaining connection and operations.

Public fields

host string
host
port int
port
secure bool
field (bool)
namespace_id string
field (string)

new_client

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

Purpose

Construct client.

Parameters

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

Returns

Client — a value of this type

(Client) request

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

Purpose

Execute request on Client. Throws on failure; handle the error or propagate it to the caller.

Parameters

c Client
The Client instance used by this call (value receiver).
method string
method (string)
path string
filesystem path (string)
body string
body (string)

Returns

Response — a value of this type

(Client) publish_config

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

Purpose

Operation on Client: publish config.

Parameters

c Client
The Client instance used by this call (value receiver).
data_id string
data_id (string)
group string
group (string)
content string
content (string)

Returns

bool — true or false

(Client) get_config

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

Purpose

Operation on Client: read config. Throws on failure; handle the error or propagate it to the caller.

Parameters

c Client
The Client instance used by this call (value receiver).
data_id string
data_id (string)
group string
group (string)

Returns

string — result string

(Client) remove_config

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

Purpose

Operation on Client: remove config.

Parameters

c Client
The Client instance used by this call (value receiver).
data_id string
data_id (string)
group string
group (string)

Returns

bool — true or false

(Client) register_instance

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

Purpose

Operation on Client: register instance.

Parameters

c Client
The Client instance used by this call (value receiver).
service string
service (string)
ip string
ip (string)
port int
port (int)
group string
group (string)

Returns

bool — true or false

(Client) get_instances

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

Purpose

Operation on Client: read instances. Throws on failure; handle the error or propagate it to the caller.

Parameters

c Client
The Client instance used by this call (value receiver).
service string
service (string)
group string
group (string)

Returns

string — result string

(Client) deregister_instance

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

Purpose

Operation on Client: deregister instance.

Parameters

c Client
The Client instance used by this call (value receiver).
service string
service (string)
ip string
ip (string)
port int
port (int)
group string
group (string)

Returns

bool — true or false

struct Pool

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

Purpose

Nacos Connection pool manager scheduling reusable socket connections.

Public fields

client Client
field (Client)
state chan pool.State
field (channel; type chan pool.State)
max_idle int
field (int)

new_pool

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

Purpose

Construct pool.

Parameters

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

Returns

Pool — a value of this type

open_pool

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

Purpose

Open pool.

Parameters

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

Returns

Pool — a value of this type

See usage example

(&Pool) dup

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

Purpose

Shares sockets and close state; clones configuration strings.

Parameters

p &Pool
The Pool instance used by this call (borrow).

Returns

Pool — a value of this type

(&Pool) get_conn

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

Purpose

Operation on &Pool: read conn.

Parameters

p &Pool
The Pool instance used by this call (borrow).

Returns

int — a value of this type

(&Pool) put_conn

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

Purpose

Operation on &Pool: put connection.

Parameters

p &Pool
The Pool instance used by this call (borrow).
fd int
file descriptor (int)

Returns

This call does not return a value.

(&Pool) discard_conn

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

Purpose

Operation on &Pool: discard connection.

Parameters

p &Pool
The Pool instance used by this call (borrow).
fd int
file descriptor (int)

Returns

This call does not return a value.

(&Pool) register_instance

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

Purpose

Operation on &Pool: register instance.

Parameters

p &Pool
The Pool instance used by this call (borrow).
service string
service (string)
ip string
ip (string)
port int
port (int)
group string
group (string)

Returns

bool — true or false

(&Pool) get_instances

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

Purpose

Operation on &Pool: read instances. Throws on failure; handle the error or propagate it to the caller.

Parameters

p &Pool
The Pool instance used by this call (borrow).
service string
service (string)
group string
group (string)

Returns

string — result string

(&Pool) deregister_instance

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

Purpose

Operation on &Pool: deregister instance.

Parameters

p &Pool
The Pool instance used by this call (borrow).
service string
service (string)
ip string
ip (string)
port int
port (int)
group string
group (string)

Returns

bool — true or false

(&Pool) publish_config

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

Purpose

Operation on &Pool: publish config.

Parameters

p &Pool
The Pool instance used by this call (borrow).
data_id string
data_id (string)
group string
group (string)
content string
content (string)

Returns

bool — true or false

(&Pool) get_config

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

Purpose

Operation on &Pool: read config. Throws on failure; handle the error or propagate it to the caller.

Parameters

p &Pool
The Pool instance used by this call (borrow).
data_id string
data_id (string)
group string
group (string)

Returns

string — result string

(&Pool) remove_config

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

Purpose

Operation on &Pool: remove config.

Parameters

p &Pool
The Pool instance used by this call (borrow).
data_id string
data_id (string)
group string
group (string)

Returns

bool — true or false

(&Pool) close

XuAPI
pub fn (p &Pool) close()

Purpose

Operation on &Pool: close the resource.

Parameters

p &Pool
The Pool instance used by this call (borrow).

Returns

This call does not return a value.