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. .NETFramework:Exception

    ylbtech-System.Exception.cs 1.返回顶部 1. #region 程序集 mscorlib, Version=4.0.0.0, Culture=neutral, Public ...

  2. STM32之glossary

    glossary Word: data/instruction of 32-bit length. Half word: data/instruction of 16-bit length. Byte ...

  3. LightOJ-1138-Trailing Zeroes (III)-二分+求N!末尾0

    You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in d ...

  4. idea激活破解

    下载链接:https://pan.baidu.com/s/1BADk1MCm0YxtB6zoX0DivA 提取码 ze9m将破解补丁放到bin文件中jetbrains-all.jar 在这俩个文件最后 ...

  5. 初识OpenCV-Python - 003:Mouse as a paint-brush

    此次学习了如何在OpenCV中使用鼠标事件.主要使用cv2.setMouseCallback()函数来调用鼠标事件. 首先,鼠标有如下事件: >>> import cv2>&g ...

  6. USACO training course Checker Challenge N皇后 /// oj10125

    ...就是N皇后 输出前三种可能排序 输出所有可能排序的方法数 vis[0][i]为i点是否已用 vis[1][m+i]为i点副对角线是否已用  m+i 为从左至右第 m+i 条副对角线 vis[1] ...

  7. VMware Workstation 10 配置Ubuntu环境

    分享到 一键分享 QQ空间 新浪微博 百度云收藏 人人网 腾讯微博 百度相册 开心网 腾讯朋友 百度贴吧 豆瓣网 搜狐微博 百度新首页 QQ好友 和讯微博 更多... 百度分享 VMware Work ...

  8. java_缓冲流(字符输出输入流)

    /** java.io.BufferedReader extends Reader * * 构造方法: * BufferedReader(Reader in):创建一个使用默认大小输入缓冲区的缓冲字符 ...

  9. 将本地数据库迁移到云端RDS数据库

  10. Python全栈开发:web框架们

    Python的WEB框架 Bottle Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Python的标准库外,其不依赖任何其他模块. 1 2 3 ...