Closed wangwanttt closed 6 years ago
please attach your full code
package main
import (
"errors"
"flag"
"fmt"
"log"
"strings"
"time"
gateway "github.com/rpcx-ecosystem/rpcx-gateway"
"github.com/smallnest/rpcx/client"
)
var (
addr = flag.String("addr", ":9981", "http server address")
st = flag.String("st", "http1", "server type: http1 or h2c")
//registry = flag.String("registry", "peer2peer://127.0.0.1:8972", "registry address")
registry = flag.String("registry", "consul://localhost:8500", "registry address")
//------------- 改成了我自己的-------
basePath = flag.String("basepath", "/rpcx_test", "basepath for zookeeper, etcd and consul")
failmode = flag.Int("failmode", int(client.Failover), "failMode, Failover in default")
selectMode = flag.Int("selectmode", int(client.RoundRobin), "selectMode, RoundRobin in default")
)
func main() {
flag.Parse()
d, err := createServiceDiscovery(*registry)
if err != nil {
log.Fatal(err)
}
gw := gateway.NewGateway(*addr, gateway.ServerType(*st), d, client.FailMode(*failmode), client.SelectMode(*selectMode), client.DefaultOption)
fmt.Print("--------网关开始接受请求-----")
gw.Serve()
}
func createServiceDiscovery(regAddr string) (client.ServiceDiscovery, error) {
i := strings.Index(regAddr, "://")
if i < 0 {
return nil, errors.New("wrong format registry address. The right fotmat is [registry_type://address]")
}
regType := regAddr[:i]
regAddr = regAddr[i+3:]
switch regType {
case "peer2peer": //peer2peer://127.0.0.1:8972
return client.NewPeer2PeerDiscovery("tcp@"+regAddr, ""), nil
case "multiple":
var pairs []*client.KVPair
pp := strings.Split(regAddr, ",")
for _, v := range pp {
pairs = append(pairs, &client.KVPair{Key: v})
}
return client.NewMultipleServersDiscovery(pairs), nil
case "zookeeper":
return client.NewZookeeperDiscovery(*basePath, "placeholder", []string{regAddr}, nil), nil
case "etcd":
return client.NewEtcdDiscovery(*basePath, "placeholder", []string{regAddr}, nil), nil
case "consul": // -------- 改成了我自己的 ----
return client.NewConsulDiscovery(*basePath, "Login", []string{regAddr}, nil), nil
case "mdns":
client.NewMDNSDiscovery("placeholder", 10*time.Second, 10*time.Second, "")
default:
return nil, fmt.Errorf("wrong registry type %s. only support peer2peer,multiple, zookeeper, etcd, consul and mdns", regType)
}
return nil, errors.New("wrong registry type. only support peer2peer,multiple, zookeeper, etcd, consul and mdns")
}
我用 go客户端 访问consul 调用 服务器端是可以的,但node,js 调用gateway -->consul --> rpc服务 则不行 会报错 018/01/06 17:34:44 server.go:307: WARN : rpcx: failed to handle request: rpcx: can't find service
app.get('/test', function (req, res) {
request({
url: "http://localhost:9981/",
method: "POST",
headers: {
'Content-Type': 'application/rpcx',
'X-RPCX-MessageID': '12345678',
'X-RPCX-MesssageType': '0', //设置0作为请求 1--响应
'X-RPCX-SerializeType': '1',
'X-RPCX-ServicePath': '',
'X-RPCX-ServiceMethod': 'Login'
},
body: '{"Name":"node.js", "Psw":"调用网关"}'
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}else{
console.log(error.message)
}
});
});
// -------- 改成了我自己的 ----
不要做修改 , placeholder
是个占位符,不要动
'X-RPCX-ServicePath': '',
设置你的servicePath, 比如 Login
, 看例子
case "consul": return client.NewConsulDiscovery(basePath, "placeholder", []string{regAddr}, nil), nil ---------- 改成这样,一启动就报错的--- /rpcx_test/placeholder2018/01/07 12:58:01 consul_discovery.go:60: INFO : cannot get services of from registry: rpcx_test/placeholder%!(EXTRA errors.errorString=Key not found in store) panic: Key not found in store
关于 rpcx-gateway 的配置关键点,能详细说明下吗?针对于我的这个 consul 中的 key,我 客户端和gateway 和 servcie端应该怎么配呢?现在的配置用 rpcx 的client是可以调用的,但换成 gateway 就很多莫名其妙问题,能不能给个QQ号,具体交流下啊,大神
我现在就差gateway整个项目就算完成了,现在卡在这一步了
看rpcx项目的readme,有qq群
那你把placeholder换成你的Login。
首先你得明确你的servicepath和servicemethod是什么,
看起来你的ServierPath是Login
,
ServiceMethod是什么?Login
和 Logout
?
跑一下项目中的example试试
兄弟,谢谢,我先加QQ群,一下说不清楚
Clone method of consul/etcd/zookeeper registry has an issue. Please pull latest rpcx to test
var ( addr = flag.String("addr", ":9981", "http server address") st = flag.String("st", "http1", "server type: http1 or h2c") //registry = flag.String("registry", "peer2peer://127.0.0.1:8972", "registry address") registry = flag.String("registry", "consul://localhost:8500", "registry address") --------- i use consul ------ ...... case "consul": //regAddr is localhost:8500 return client.NewConsulDiscovery(*basePath, "placeholder", []string{regAddr}, nil), nil
compli error:
2018/01/06 12:20:06 consul_discovery.go:59: INFO : cannot get services of from registry: rpcx/placeholder%!(EXTRA *errors.errorString=Key not found in store) panic: Key not found in store