首页 > 其他分享 >grpc-web使用

grpc-web使用

时间:2022-10-25 10:45:29浏览次数:156  
标签:Control web grpc Content header add 使用

一,proto协议编译js文件

hello.proto文件

 1 syntax = "proto3";
 2 
 3 package api;
 4 
 5 // 这里可以写服务注释
 6 service HelloWorldService {
 7     // 这里可以写方法注释
 8     rpc SayHello (HelloRequest) returns (HelloResponse) {}
 9 }
10 
11 // 这里可以写请求结构注释
12 message HelloRequest {
13     // 这里可以写参数注释
14     string name = 1;
15 }
16 
17 // 这里可以写响应结构注释
18 message HelloResponse {
19     // 这里可以写参数注释
20     string message = 1;
21 }

环境准备

cd /home/shimon/pkg

# 将下载后的内容移动到bin路径中方便使用
mv protoc-gen-grpc-web-1.4.1-linux-x86_64 /usr/local/bin/protoc-gen-grpc-web
# 增加可执行权限
chmod +x /usr/local/bin/protoc-gen-grpc-web

# 根据hello.proto生成js调用文件
protoc hello.proto  --js_out=import_style=commonjs:./output  --grpc-web_out=import_style=commonjs,mode=grpcweb:./output

 

 

二,前端调用grpc服务

a.将hello_grpc_web_pb.js和hello_pb.js移到前端pb目录下

b.项目安装依赖yarn add google-protobuf grpc-web

c.调用代码:

import { HelloRequest } from './pb/hello_pb';
import { HelloWorldServiceClient } from './pb/hello_grpc_web_pb';

// 注意这个端口是代理服务器的端口,不是grpc的端口
const client = new HelloWorldServiceClient('http://localhost:8199',
    null, null);

const request = new HelloRequest();
request.setName('World');

client.sayHello(request, {}, (err, response) => {
    console.log(response.getMessage());
});

 

三,代理请求

由于浏览器的特性,gRPC-web其实没办法直接向gRPC-server发送HTTP/2请求的,只有通过nginx/envoy代理,将来自gRPC-web的HTTP/1的请求转换为gRPC-server能够接收的HTTP/2请求

nginx配置文件:

server {
  listen 8199;
  server_name _;

  access_log /tmp/grpc.log;
  error_log /tmp/grpc.log debug;

  location ~ \.(html|js)$ {
    root /var/www/html;
  }
  location / {
    # 重点!!需要将Content-Type更改为 application/grpc
    # grpc-web过来的是application/grpc-web+proto || application/grpc-web+text (取决于生成js代码时grpc-web_out 的mode选项,本文用grpcweb 则为application/grpc-web+proto)
    grpc_set_header Content-Type application/grpc;
    grpc_pass localhost:9999;
    # 因浏览器有跨域限制,这里直接在nginx支持跨域
    if ($request_method = 'OPTIONS') {
      add_header 'Access-Control-Allow-Origin' '*';
      add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
      add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Transfer-Encoding,Custom-Header-1,X-Accept-Content-Transfer-Encoding,X-Accept-Response-Streaming,X-User-Agent,X-Grpc-Web';
      add_header 'Access-Control-Max-Age' 1728000;
      add_header 'Content-Type' 'text/plain charset=UTF-8';
      add_header 'Content-Length' 0;
      return 204;
    }

    if ($request_method = 'POST') {
      add_header 'Access-Control-Allow-Origin' '*';
      add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
      add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Transfer-Encoding,Custom-Header-1,X-Accept-Content-Transfer-Encoding,X-Accept-Response-Streaming,X-User-Agent,X-Grpc-Web';
      add_header 'Access-Control-Expose-Headers' 'Content-Transfer-Encoding, grpc-message,grpc-status';
    }
  }
}

 

标签:Control,web,grpc,Content,header,add,使用
From: https://www.cnblogs.com/shi2310/p/16824071.html

相关文章

  • linux如何使用umount命令强制卸载文件系统
    https://www.cnblogs.com/ss-33/p/13884639.htmlumount命令解挂文件系统。umount[-ahnrvV][-t《文件系统类型》][文件系统]umount可卸除目前挂在Linux目录中的文......
  • 关于一个web站点的欢迎页面
    关于一个web站点的欢迎页面什么是一个web站点的欢迎页面?对于一个webapp来说,我们是可以设置它的欢迎页面的。设置欢迎页面之后,当你访问这个webapp的时候,或者访问这......
  • 使用Expression代替反射读取IDataReader或IDataRecord给实体类赋值
    ExpressionMapper代码usingSystem;usingSystem.Collections.Concurrent;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.Data.Common;usingSy......
  • 还在使用@Autowrired注入?不妨试试@RequiredArgsConstructor
    一、前言小编最近在项目里看到有的同事大神用到了Lombok中的一个@RequiredArgsConstructor,带着好奇发现这个东西就是简化了一些@Autowired注解,想想如果一个Service还有几......
  • 1.1 WCF SOA架构和webservice
    1.什么是SOA?SOA全称:面向服务架构(serviceOrientedArchitecture),它是一种组件架构模式。一、定义1.WebService:严格来说是行业标准,不是技术,使用XML扩展标记语言来表示数据......
  • .NET MAUI应用中使用资源文件/图像
    官方文档:https://learn.microsoft.com/zh-cn/aspnet/core/blazor/hybrid/static-files?view=aspnetcore-6.0在.NETMAUI应用中,使用MauiAsset生成操作的原始资源和.NET......
  • 记录-Chrome浏览器翻译无法使用解决
    之前谷歌浏览器无法使用,修改了hosts文件,在hosts文件中配置了:203.208.40.66translate.google.com203.208.40.66translate.googleapis.com但最近又无法使用了......
  • 第二十六章 使用 CSP 进行基于标签的开发
    第二十六章使用CSP进行基于标签的开发CSP允许使用标准HTML文件开发CSP应用程序。CSP编译器将HTML(和XML)文档转换为可以响应HTTP请求的类中的%CSP.Page。CS......
  • Page.map方法的使用
    Page.map方法的使用1、前言日常工作中,我们常常会有这样的场景:分页查询得到了结果,需要对dto的某个单独字段将进行赋值,这时候我们就会用到Page分页对象提供的map方法,用来转......
  • 使用docker部署nginx并配置https
     使用docker部署nginx并配置https 我只有一台服务器,但我想在这台服务器上运行多个项目,怎么办?总不能靠加端口区分吧?百度和Google是个好东西,于是我找到了答案,使用ng......