Giter Club home page Giter Club logo

Comments (11)

smallnest avatar smallnest commented on May 26, 2024

please attach your full code

from rpcx-gateway.

wangwanttt avatar wangwanttt commented on May 26, 2024
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")
}

from rpcx-gateway.

wangwanttt avatar wangwanttt commented on May 26, 2024

我用 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)
    }
  });
});

from rpcx-gateway.

smallnest avatar smallnest commented on May 26, 2024
  1. gateway中
// -------- 改成了我自己的 ----

不要做修改 , placeholder是个占位符,不要动

from rpcx-gateway.

smallnest avatar smallnest commented on May 26, 2024
  1. client中,使用你正确的servicepath和servicemethod,你设置servicepath为空肯定不正确
 'X-RPCX-ServicePath': '',

设置你的servicePath, 比如 Login, 看例子

from rpcx-gateway.

wangwanttt avatar wangwanttt commented on May 26, 2024

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号,具体交流下啊,大神
key

from rpcx-gateway.

wangwanttt avatar wangwanttt commented on May 26, 2024

我现在就差gateway整个项目就算完成了,现在卡在这一步了

from rpcx-gateway.

smallnest avatar smallnest commented on May 26, 2024

看rpcx项目的readme,有qq群

from rpcx-gateway.

smallnest avatar smallnest commented on May 26, 2024

那你把placeholder换成你的Login。

首先你得明确你的servicepath和servicemethod是什么,

看起来你的ServierPath是Login,
ServiceMethod是什么?LoginLogout?

跑一下项目中的example试试

from rpcx-gateway.

wangwanttt avatar wangwanttt commented on May 26, 2024

兄弟,谢谢,我先加QQ群,一下说不清楚

from rpcx-gateway.

smallnest avatar smallnest commented on May 26, 2024

Clone method of consul/etcd/zookeeper registry has an issue. Please pull latest rpcx to test

from rpcx-gateway.

Related Issues (20)

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.