Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 58 additions & 17 deletions config_center/zookeeper/go-client/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,71 +40,112 @@ import (
)

func main() {
_ = writeRuleToConfigCenter()
// write configuration to config center
if err := writeRuleToConfigCenter(); err != nil {
logger.Errorf("Failed to write config to config center: %v", err)
panic(err)
}
logger.Info("Successfully wrote config to ZooKeeper")

time.Sleep(time.Second * 10)
// wait for config write to finish
time.Sleep(time.Second * 3)

// configure Dubbo instance
zkOption := config_center.WithZookeeper()
dataIdOption := config_center.WithDataID("dubbo-go-samples-configcenter-zookeeper-client")
addressOption := config_center.WithAddress("127.0.0.1:2181")
groupOption := config_center.WithGroup("dubbogo")

ins, err := dubbo.NewInstance(
dubbo.WithConfigCenter(zkOption, dataIdOption, addressOption, groupOption),
)
if err != nil {
logger.Errorf("Failed to create Dubbo instance: %v", err)
panic(err)
}
// configure the params that only client layer cares

// create client
cli, err := ins.NewClient()
if err != nil {
logger.Errorf("Failed to create Dubbo client: %v", err)
panic(err)
}

// create service proxy
svc, err := greet.NewGreetService(cli)
if err != nil {
logger.Errorf("Failed to create GreetService: %v", err)
panic(err)
}

resp, err := svc.Greet(context.Background(), &greet.GreetRequest{Name: "hello world"})
// call remote service
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

resp, err := svc.Greet(ctx, &greet.GreetRequest{Name: "hello world"})
if err != nil {
logger.Error(err)
logger.Errorf("Failed to call Greet service: %v", err)
return
}
logger.Infof("Greet response: %s", resp)
}

func writeRuleToConfigCenter() error {
// connect to ZooKeeper
c, _, err := zk.Connect([]string{"127.0.0.1:2181"}, time.Second*10)
if err != nil {
panic(err)
return perrors.Wrap(err, "failed to connect to ZooKeeper")
}
defer c.Close() // ensure resource cleanup

valueBytes := []byte(configCenterZKClientConfig)
path := "/dubbo/config/dubbogo/dubbo-go-samples-configcenter-zookeeper-client"

// ensure path starts with '/'
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
paths := strings.Split(path, "/")
for idx := 2; idx < len(paths); idx++ {
tmpPath := strings.Join(paths[:idx], "/")
_, err = c.Create(tmpPath, []byte{}, 0, zk.WorldACL(zk.PermAll))
if err != nil && err != zk.ErrNodeExists {
panic(err)
}

// create parent paths
if err := createParentPaths(c, path); err != nil {
return perrors.Wrap(err, "failed to create parent paths")
}

// create or update config node
_, err = c.Create(path, valueBytes, 0, zk.WorldACL(zk.PermAll))
if err != nil {
if perrors.Is(err, zk.ErrNodeExists) {
_, stat, _ := c.Get(path)
// node exists, update its content
_, stat, getErr := c.Get(path)
if getErr != nil {
return perrors.Wrap(getErr, "failed to get existing node")
}
_, setErr := c.Set(path, valueBytes, stat.Version)
if setErr != nil {
panic(err)
return perrors.Wrap(setErr, "failed to update existing node")
}
logger.Info("Updated existing config node")
} else {
panic(err)
return perrors.Wrap(err, "failed to create config node")
}
} else {
logger.Info("Created new config node")
}

return nil
}

// helper function to create parent paths
func createParentPaths(c *zk.Conn, path string) error {
paths := strings.Split(path, "/")
for idx := 2; idx < len(paths); idx++ {
tmpPath := strings.Join(paths[:idx], "/")
_, err := c.Create(tmpPath, []byte{}, 0, zk.WorldACL(zk.PermAll))
if err != nil && !perrors.Is(err, zk.ErrNodeExists) {
return perrors.Wrapf(err, "failed to create path: %s", tmpPath)
}
}
return err
return nil
}

const configCenterZKClientConfig = `## set in config center, group is 'dubbogo', dataid is 'dubbo-go-samples-configcenter-zookeeper-client', namespace is default
Expand Down
69 changes: 51 additions & 18 deletions config_center/zookeeper/go-server/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,20 @@ type GreetTripleServer struct {
}

func (srv *GreetTripleServer) Greet(ctx context.Context, req *greet.GreetRequest) (*greet.GreetResponse, error) {
// reference ctx to avoid unused parameter warning
_ = ctx
resp := &greet.GreetResponse{Greeting: req.Name}
return resp, nil
}

func main() {
_ = writeRuleToConfigCenter()
// Write configuration to the config center
if err := writeRuleToConfigCenter(); err != nil {
logger.Errorf("Failed to write config to config center: %v", err)
panic(err)
}

// Wait for the configuration to take effect
time.Sleep(time.Second * 10)

ins, err := dubbo.NewInstance(
Expand All @@ -60,19 +68,24 @@ func main() {
),
)
if err != nil {
logger.Errorf("Failed to create dubbo instance: %v", err)
panic(err)
}

srv, err := ins.NewServer()
if err != nil {
logger.Errorf("Failed to create server: %v", err)
panic(err)
}

if err = greet.RegisterGreetServiceHandler(srv, &GreetTripleServer{}); err != nil {
logger.Errorf("Failed to register service: %v", err)
panic(err)
}

logger.Info("Starting Dubbo-Go server...")
if err = srv.Serve(); err != nil {
logger.Error(err)
logger.Errorf("Server failed to serve: %v", err)
}
}

Expand All @@ -93,42 +106,62 @@ dubbo:
interface: com.apache.dubbo.sample.basic.IGreeter
`

func ensurePath(c *zk.Conn, path string, data []byte, flags int32, acl []zk.ACL) error {
_, err := c.Create(path, data, flags, acl)
return err
}
// ensurePath Ensure the path exists (removed because it is unused)

func writeRuleToConfigCenter() error {
// Connect to Zookeeper
c, _, err := zk.Connect([]string{"127.0.0.1:2181"}, time.Second*10)
if err != nil {
panic(err)
return perrors.Wrap(err, "failed to connect to zookeeper")
}
defer c.Close() // Ensure the connection is closed

valueBytes := []byte(configCenterZKServerConfig)
path := "/dubbo/config/dubbogo/dubbo-go-samples-configcenter-zookeeper-server"

// Ensure the path starts with '/'
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
paths := strings.Split(path, "/")
for idx := 2; idx < len(paths); idx++ {
tmpPath := strings.Join(paths[:idx], "/")
_, err = c.Create(tmpPath, []byte{}, 0, zk.WorldACL(zk.PermAll))
if err != nil && err != zk.ErrNodeExists {
panic(err)
}

// Create parent paths
if err := createParentPaths(c, path); err != nil {
return perrors.Wrap(err, "failed to create parent paths")
}

// Create or update configuration node
_, err = c.Create(path, valueBytes, 0, zk.WorldACL(zk.PermAll))
if err != nil {
if perrors.Is(err, zk.ErrNodeExists) {
_, stat, _ := c.Get(path)
// Node already exists, update configuration
_, stat, getErr := c.Get(path)
if getErr != nil {
return perrors.Wrap(getErr, "failed to get existing node")
}
_, setErr := c.Set(path, valueBytes, stat.Version)
if setErr != nil {
panic(err)
return perrors.Wrap(setErr, "failed to update existing node")
}
logger.Info("Updated existing configuration in config center")
} else {
panic(err)
return perrors.Wrap(err, "failed to create configuration node")
}
} else {
logger.Info("Created new configuration in config center")
}

return nil
}

// createParentPaths Create parent paths
func createParentPaths(c *zk.Conn, path string) error {
paths := strings.Split(path, "/")
for idx := 2; idx < len(paths); idx++ {
tmpPath := strings.Join(paths[:idx], "/")
_, err := c.Create(tmpPath, []byte{}, 0, zk.WorldACL(zk.PermAll))
if err != nil && !perrors.Is(err, zk.ErrNodeExists) {
return perrors.Wrapf(err, "failed to create parent path: %s", tmpPath)
}
}
return err
return nil
}
39 changes: 32 additions & 7 deletions llm/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
Expand All @@ -13,14 +12,40 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# LLM Models Configuration
LLM_MODELS = llava:7b, qwen2.5:7b

OLLAMA_MODELS= qwen2.5:7b, llava:7b
# Provider Configuration
LLM_PROVIDER = ollama
LLM_BASE_URL = http://localhost:11434
# LLM_API_KEY = your-api-key-here

# Legacy Configuration (for backward compatibility)
OLLAMA_URL = http://localhost:11434
TIME_OUT_SECOND = 300

NACOS_URL = localhost:8848
# Server Configuration
MODEL_NAME = llava:7b
SERVER_PORT = 20020
NACOS_URL = nacos://localhost:8848
TIME_OUT_SECOND = 300
MAX_CONTEXT_COUNT = 3
MODEL_NAME = qwen2.5:7b
SERVER_PORT = 20000

# Example configurations:
#
# Ollama (Local):
# LLM_MODELS = llava:7b, qwen2.5:7b
# LLM_PROVIDER = ollama
# LLM_BASE_URL = http://localhost:11434
#
# OpenAI:
# LLM_MODELS = gpt-4, gpt-3.5-turbo
# LLM_PROVIDER = openai
# LLM_BASE_URL = https://api.openai.com/v1
# LLM_API_KEY = your-openai-api-key
#
# Anthropic:
# LLM_MODELS = claude-3-sonnet, claude-3-haiku
# LLM_PROVIDER = anthropic
# LLM_BASE_URL = https://api.anthropic.com/v1
# LLM_API_KEY = your-anthropic-api-key
Loading
Loading