我用Python编写了一个测试gRPC服务器,并在ReactJS(ES6)上编写了客户端。这是一个简单的pythongrpc服务器,它为客户机提供身份验证方法。还配置了用于将HTTP请求传输到HTTP2的EnvoyProxy。当我从客户机调用gRPC方法时,我得到{"code": 12, "message": "Method not found"}
。在
{a1}未在此方法中实现:
但我确信这个方法已经实现了!在
下面是我的一些Python服务代码:
class AuthenticationServicer(glyphs_pb2_grpc.AuthenticationServicer):
def SignIn(self, request, context):
# do authentication method stuff...
return glyphs_pb2.TokenResponse(
success=True,
response=glyphs_pb2.Token(token=refresh_token.token, expired_at=str(refresh_token.expired_at)),
)
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
glyphs_pb2_grpc.add_AuthenticationServicer_to_server(AuthenticationServicer(), server)
print('Starting server. Listening on port 50051.')
server.add_insecure_port('localhost:50051')
server.start()
我尝试从python客户机调用gRPC方法,它按预期工作:
^{pr2}$~$ python ./client.py
Refresh Token:
success: true
response {
token: "7ae52622cf556632de0a0fe115e1fc0c5adaea9c"
expired_at: "2019-11-13 13:22:56.870937"
}
我的特使YAML配置:
admin:
access_log_path: /tmp/admin_access.log
address:
socket_address: { address: 0.0.0.0, port_value: 9901 }
static_resources:
listeners:
- name: listener_0
address:
socket_address: { address: 0.0.0.0, port_value: 9090 }
filter_chains:
- filters:
- name: envoy.http_connection_manager
config:
codec_type: auto
stat_prefix: ingress_http
route_config:
name: local_route
virtual_hosts:
- name: local_service
domains: ["*"]
routes:
- match: { prefix: "/" }
route:
cluster: authentication_service
max_grpc_timeout: 0s
cors:
allow_origin:
- "*"
allow_methods: GET, PUT, DELETE, POST, OPTIONS
allow_headers: keep-alive,user-agent,cache-control,content-type,content-transfer-encoding,x-accept-content-transfer-encoding,x-accept-response-streaming,x-user-agent,x-grpc-web,grpc-timeout
max_age: "1728000"
expose_headers: grpc-status,grpc-message
http_filters:
- name: envoy.grpc_web
- name: envoy.cors
- name: envoy.router
clusters:
- name: authentication_service
connect_timeout: 0.25s
type: logical_dns
http2_protocol_options: {}
lb_policy: round_robin
hosts: [{ socket_address: { address: host.docker.internal, port_value: 50051 }}]
ReactJS客户端代码:
const { AuthenticationClient } = require('./glyphs_grpc_web_pb');
const { SignInForm } = require('./glyphs_pb');
let client = new AuthenticationClient('http://localhost:9090/', null, null);
let form = new SignInForm();
form.setEmail('admin@localhost');
form.setPassword('123456a');
client.signIn(form, {}, (err, res) => {
if (res) {
console.log(res);
} else {
console.log(err);
}
});
我期望输出是我的令牌响应,但实际输出是:
在包.json公司名称:
{
"name": "glyphs"
"dependencies": {
"google-protobuf": "^3.10.0",
"grpc-web": "^1.0.6",
"react": "^16.10.2",
"react-dom": "^16.10.2",
"react-scripts": "3.2.0"
}
}
我遇到了一个类似的
method not found
错误,通过将客户机创建更改为let client=new AuthenticationClient('http://localhost:9090',null,null)
注意:去掉末尾的
/
。在这样,调用
client.signIn(...)
将被定向到http://127.0.0.1:9090/[package-if-defined-in-proto].AuthenticationServicer/SignIn
但是如果
let client=new AuthenticationClient('http://localhost:9090/',null,null)
然后相同的调用
client.signIn(...)
将被定向到http://127.0.0.1:9090//[package-if-defined-in-proto].AuthenticationServicer/SignIn
并且双
//
导致了找不到方法的问题。我想grpcweb会自动强制地向主机名添加一个/
。在相关问题 更多 >
编程相关推荐