https://github.com/grpc-ecosystem/grpc-gateway

在grpc之上加一层代理并转发,转变成protobuf格式来访问grpc服务

安装

go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
go get -u github.com/golang/protobuf/protoc-gen-go

Prod.proto

syntax="proto3";
package services;
import "google/api/annotations.proto"; message ProdRequest {
int32 prod_id =; //传入的商品ID
}
message ProdResponse{
int32 prod_stock=;//商品库存
} service ProdService {
rpc GetProdStock (ProdRequest) returns (ProdResponse){
option (google.api.http) = {
get: "/v1/prod/{prod_id}"
}; }
}

生成两个文件

首先cd 进入pbfiles

这会生成Prod.pb.go
protoc --go_out=plugins=grpc:../services Prod.proto 这会生成Prod.pb.gw.go
protoc --grpc-gateway_out=logtostderr=true:../services Prod.proto

httpserver.go

package main

import (
"context"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"google.golang.org/grpc"
"grpcpro/services"
"log"
"net/http"
) func main() {
gwmux:=runtime.NewServeMux()
opt := []grpc.DialOption{grpc.WithInsecure()}
err:=services.RegisterProdServiceHandlerFromEndpoint(context.Background(),
gwmux,"localhost:8081",opt)
if err != nil {
log.Fatal(err)
}
httpServer:=&http.Server{
Addr:":8080",
Handler:gwmux,
}
httpServer.ListenAndServe() }

server.go

package main

import (
"google.golang.org/grpc"
"grpcpro/services"
"net"
) func main() {
rpcServer:=grpc.NewServer()
services.RegisterProdServiceServer(rpcServer,new(services.ProdService)) lis,_:=net.Listen("tcp",":8081") rpcServer.Serve(lis) }

源码地址:

https://github.com/sunlongv520/grpc-learn

https://github.com/sunlongv520/grpc-doc

使用gRPC-Gateway快速构建微服务-双向认证下rpc-gateway使用(同时提供rpc和http接口)的更多相关文章

  1. 通过GeneXus如何快速构建微服务架构

    概览 “微服务”是一个非常广泛的话题,在过去几年里,市面上存在着各种不同的定义. 虽然对这种架构方式没有一个非常精确的定义,但仍然有一些概念具有代表性. 微服务有着许多围绕业务能力.自动化部署.终端智 ...

  2. SpringBoot 快速构建微服务体系 知识点总结

    可以通过http://start.spring.io/构建一个SpringBoot的脚手架项目 一.微服务 1.SpringBoot是一个可使用Java构建微服务的微框架. 2.微服务就是要倡导大家尽 ...

  3. Springboot揭秘-快速构建微服务体系-王福强-2016年5月第一次印刷

    JavaConfig项目: spring IOC有一个非常核心的概念——Bean.由Spring容器来负责对Bean的实例化,装配和管理.XML是用来描述Bean最为流行的配置方式.Spring可以从 ...

  4. SpringBoot揭秘:快速构建微服务体系

    chapter 2: 饮水思源:回顾与探索Spring框架本质 IoC其实有两种方式,一种是DI(dependency Injection),一种是DL(dependency Lookup 依赖查找, ...

  5. 《SpringBoot揭秘 快速构建微服务体系》读后感(一)

    SpringIOC IOC有两种方式:一种是DI,另一种是DL,即Dependency Lookup(依赖查找).前者是当前软件实体被动接受其依赖的其他组件被IoC容器注入,而后者则是当前软件实体主动 ...

  6. 《SpringBoot揭秘 快速构建微服务体系》读后感(五)

    应用日志和spring-boot-starter-logging 快速web应用开发与spring-boot-starter-web 1.项目结构层面的约定

  7. 《SpringBoot揭秘 快速构建微服务体系》读后感(四)

    再谈自动配置 基于条件的自动配置 调整自动配置的顺序

  8. 《SpringBoot揭秘 快速构建微服务体系》读后感(三)

    SpringApplication:SpringBoot程序启动的一站式解决方案 深入探索SpringApplication执行流程 因为书上的版本是1.2的,比较老,这里参考http://blog. ...

  9. 《SpringBoot揭秘 快速构建微服务体系》读后感(二)

    最简单的springBoot应用 package com.louis.test; import org.springframework.boot.SpringApplication; import o ...

随机推荐

  1. 云-腾讯云-云点播:云点播(VOD)

    ylbtech-云-腾讯云-云点播:云点播(VOD) 提供端到端的一站式VpaaS音视频点播解决方案 1.返回顶部 1. 云点播(Video on Demand,VOD)基于腾讯多年技术积累与基础设施 ...

  2. System.Byte.cs

    ylbtech-System.Byte.cs 1.程序集 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934 ...

  3. Docker系列(六):Docker网络机制(下)

    Linux Namespace详解 namespace:是一个空间,空间里可以放进程,文件系统,账号,网络等,某个资源被放到namespace之后 别人就看不到他了. 可以看到有两个namespace ...

  4. AndroidStudio 添加翻译插件

    添加方式 第一步 在AndroidStudio的菜单栏里找到 File > Settings > 点击 . 第二步 点击Plugins > 在点击Marketplace 等待插件列表 ...

  5. [转]Git 常用命令详解

    史上最浅显易懂的Git教程 http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/ ht ...

  6. Intel RealSense Depth Camera D435安装ROS 驱动——Ubuntu16.04

    官方教程 软件包下载链接 https://github.com/IntelRealSense/realsense-ros Download/Clone librealsense github repo ...

  7. 第七章 Odoo 12开发之记录集 - 使用模型数据

    在上一篇文章中,我们概览了模型创建以及如何从模型中载入和导出数据.现在我们已有数据模型和相关数据,是时候学习如何编程与其进行交互 了.模型的 ORM(Object-Relational Mapping ...

  8. css---switch开关

    html: <label><input class="mui-switch" type="checkbox"> 默认未选中</la ...

  9. Git从fork分支开始的过程整理

    文章适用于团队合作的时候多个人向一个repo贡献,整理了Git从fork分支开始的过程. 1. Fork 在github上你要贡献的repo(eg.http://github/remote/test. ...

  10. 安装office2019

    win10系统安装office2019 安装文件下载 https://pan.baidu.com/s/1VnqJ-hNwysPKBhdzE3FSww#list/path=%2F&parentP ...