Giter Site home page Giter Site logo

shenyu-client-golang's Introduction

shenyu-client-golang

English | 简体中文

Build and Test codecov.io GoDoc


Shenyu-client-golang

Shenyu-client-golang for Go client allows you to access ShenYu Gateway,it supports registory go service to ShenYu Gateway.


Supported Register Center to ShenYu Gateway

  • Http type Register
  • Nacos type Register
  • Zookeeper type Register
  • Consul type Register
  • Etcd type Register

Requirements

Supported Go version over 1.13

SDK Supported ShenYu version over 2.4.3

Installation

Use go get to install SDK:

$ go get -u github.com/apache/shenyu-client-golang

The Demo location

  • shenyu-client-golang/example/**_client/main.go

Get Start


shenyu-client-golang's People

Contributors

bingtianyiyan avatar dengliming avatar lishuo5263 avatar stulzq avatar tuohai666 avatar yu199195 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

shenyu-client-golang's Issues

Can not register

我在本地运行的 ShenYu Admin & BootstrapApplication,然后运行了 .NET client 可以成功注册

image

我直接使用的 go client 仓库的 http client example

/*
 * 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.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * 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.
 */

package main

import (
	"github.com/apache/incubator-shenyu-client-golang/clients"
	"github.com/apache/incubator-shenyu-client-golang/common/constants"
	"github.com/apache/incubator-shenyu-client-golang/model"
	"github.com/wonderivan/logger"
)

/**
 * The shenyu_http_client example
 **/
func main() {

	//init ShenYuAdminClient
	adminClient := &model.ShenYuAdminClient{
		UserName: "admin",  //user provide
		Password: "123456", //user provide
	}

	adminToken, err := clients.NewShenYuAdminClient(adminClient)
	if err == nil {
		logger.Info("this is ShenYu Admin client token ->", adminToken.AdminTokenData.Token)
	}

	//init MetaDataRegister
	metaData := &model.MetaDataRegister{
		AppName: "test",      //require user provide
		Path:    "/test/aaa", //require user provide
		Enabled: true,        //require user provide
		Host:    "127.0.0.1", //require user provide
		Port:    "5000",      //require user provide
	}
	result, err := clients.RegisterMetaData(adminToken.AdminTokenData, metaData)
	if err != nil {
		logger.Warn("MetaDataRegister has error:", err)
	}
	logger.Info("finish register metadata ,the result is->", result)

	//init urlRegister
	urlRegister := &model.URIRegister{
		Protocol:    "http://",              //require user provide
		AppName:     "test",                 //require user provide
		ContextPath: "/test/aaa",            //require user provide
		RPCType:     constants.RPCTYPE_HTTP, //require user provide
		Host:        "127.0.0.1",            //require user provide
		Port:        "5000",                 //require user provide
	}
	result, err = clients.UrlRegister(adminToken.AdminTokenData, urlRegister)
	if err != nil {
		logger.Warn("UrlRegister has error:", err)
	}
	logger.Info("finish UrlRegister ,the result is->", result)

	//do you logic
}

客户端日志看起来是 ok 的

2022-07-01 12:38:50 [INFO] [/Users/lzq/OpenSource/shenyu/incubator-shenyu-client-golang/clients/admin_client/shenyu_admin_client.go:51] Get ShenYu Admin response, body is -> {200 login dashboard user success {1 admin 1 true 2022-05-25 18:02:52 2022-05-25 18:02:52 eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyTmFtZSI6ImFkbWluIiwiZXhwIjoxNjU2NzM2NzMwfQ.JAL9NDC-sCcrfnaIICosOScRAORXGpIKhNUsyLc07O0}}
2022-07-01 12:38:50 [INFO] [/Users/lzq/OpenSource/shenyu/incubator-shenyu-client-golang/example/http_client/main.go:40] this is ShenYu Admin client token -> eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyTmFtZSI6ImFkbWluIiwiZXhwIjoxNjU2NzM2NzMwfQ.JAL9NDC-sCcrfnaIICosOScRAORXGpIKhNUsyLc07O0
2022-07-01 12:38:50 [INFO] [/Users/lzq/OpenSource/shenyu/incubator-shenyu-client-golang/example/http_client/main.go:55] finish register metadata ,the result is-> true
2022-07-01 12:38:50 [INFO] [/Users/lzq/OpenSource/shenyu/incubator-shenyu-client-golang/example/http_client/main.go:70] finish UrlRegister ,the result is-> true

但是 Admin 无法查询到注册信息,没有成功注册。

并且 admin 一直在报错

2022-07-01 12:41:00 [shenyu-shared_wheel_timer-3] WARN  org.apache.shenyu.common.timer.AbstractRetryTask - Failed to execute task:/test/aaa:divide,retried:26 ,total retries:-1,cause:doRegister Failed to execute,wait to retry.

.NET client 注册并不会触发 admin 报错,Go client 只要运行了注册就会有这个。

如何才能成功注册呢

使用Nacos注册,提示异常.Nacos部署的服务版本是2.0.3

代码:
package shenyu

import (
"encoding/json"
"github.com/apache/shenyu-client-golang/clients/nacos_client"
"github.com/apache/shenyu-client-golang/common/constants"
"github.com/apache/shenyu-client-golang/common/shenyu_sdk_client"
"github.com/apache/shenyu-client-golang/model"
"github.com/nacos-group/nacos-sdk-go/clients/naming_client"
"github.com/nacos-group/nacos-sdk-go/vo"

"net"
"pki-go-server/src/com/ifyou/pki/conf"

)

func RegisterShenYu() {
//设置nacos环境配置
ncp := &nacos_client.NacosClientParam{
IpAddr: "10.246.162.117",
Port: 18848,
NamespaceId: "d1bd133f-f415-404b-8e2c-60847ce426d4",
}

//元数据是必要的参数,这将注册到shenyu网关使用
metaData := &model.URIRegister{
	Protocol:    "http",                 //需要用户提供
	AppName:     "pki-go-server",        //需要用户提供
	ContextPath: "/godemo/api",          //需要用户提供
	RPCType:     constants.RPCTYPE_HTTP, //需要用户提供
	Host:        getLocalIP(),           //需要用户提供
	Port:        "8080",                 //需要用户提供
}
metaDataStringJson, _ := json.Marshal(metaData)

//初始化Nacos注册实例信息
nacosRegisterInstance := vo.RegisterInstanceParam{
	Ip:          "10.246.162.117", //需要用户提供
	Port:        18848,            //需要用户提供
	ServiceName: "pki-go-server",  //需要用户提供
	Weight:      10,               //需要用户提供
	Enable:      true,             //需要用户提供
	Healthy:     true,             //需要用户提供
	Ephemeral:   true,             //需要用户提供
	Metadata:    map[string]string{"contextPath": "contextPath", "uriMetadata": string(metaDataStringJson)},
}

sdkClient := shenyu_sdk_client.GetFactoryClient(constants.NACOS_CLIENT)
result, createResult, err := sdkClient.NewClient(ncp)
if !createResult && err != nil {
	conf.LogEntry.Fatalf("Create nacos client error : %v", err)
}

nc := &nacos_client.ShenYuNacosClient{
	NacosClient: result.(*naming_client.NamingClient),
}

registerResult, err := nc.RegisterServiceInstance(nacosRegisterInstance)
if !registerResult && err != nil {
	conf.LogEntry.Fatalf("Register nacos Instance error : %v", err)
} else {
	conf.LogEntry.Infof("Register nacos Instance success : %v", registerResult)
}

//do your logic

}

func getLocalIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return "unknow"
}

for _, address := range addrs {
	if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
		if ipnet.IP.To4() != nil {
			gInnerIP := ipnet.IP.String()
			return gInnerIP
		}
	}
}
return "unknow"

}

2024-04-07T15:03:16.118+0800 ERROR nacos_server/nacos_server.go:230 api</v1/ns/instance>,method:, params:<{"app":"","clusterName":"","enable":"true","ephemeral":"true","groupName":"DEFAULT_GROUP","healthy":"true","ip":"10.246.162.117","metadata":"{"contextPath":"contextPath","uriMetadata":"{\"protocol\":\"http\",\"appName\":\"pki-go-server\",\"contextPath\":\"/godemo/api\",\"rpcType\":\"http\",\"host\":\"192.168.137.59\",\"port\":\"8080\"}"}","namespaceId":"d1bd133f-f415-404b-8e2c-60847ce426d4","port":"18848","serviceName":"DEFAULT_GROUP@@pki-go-server","weight":"10"}>, call domain error:<request return error code 403> , result:<{"timestamp":"2024-04-07T15:03:11.808+08:00","status":403,"error":"Forbidden","message":"unknown user!","path":"/nacos/v1/ns/instance"}>
FATA[0004] RegisterServiceInstance failure! ,error is :retry3times request failed,err=request return error code 403

可能的原始是否为,Nacos需要用户名密码登陆. shenyu-sdk使用的是Nacos 1V的包,导致无法注册 抛出403?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.