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. SpringBoot_03_SpringBoot对其他技术的整合

    1.SpringBoot整合Mybatis 1.2 添加Mybatis的起步依赖 <!--mybatis起步依赖--> <dependency> <groupId> ...

  2. 10 行 Python 代码实现模糊查询/智能提示

    10 行 Python 代码实现模糊查询/智能提示   1.导语: 模糊匹配可以算是现代编辑器(如 Eclipse 等各种 IDE)的一个必备特性了,它所做的就是根据用户输入的部分内容,猜测用户想要的 ...

  3. 没有找到mfc100.dll

    转自VC错误:http://www.vcerror.com/?p=86 问题描述: 生成的exe文件在编译的时候会提示"没有找到mfc100.dll",这个时候需要更改配置为静态编 ...

  4. HDU-2095-find your present (2)-位或/STL(set)

    In the new year party, everybody will get a "special present".Now it's your turn to get yo ...

  5. pickle,shelve,json,configparser 的模块使用

    主要内容1. 什么是序列化2. pickle3. shelve4. json5. configparser模块 一. 什么是序列化在我们存储数据或者网络传输数据的时候. 需要对我们的对象进行处理. 把 ...

  6. Invalid bound statement (not found)之idea打包maven项目问题

    开发的一个maven项目,之前在Eclipse中,maven打包部署完后一切正常,后来转到idea中开发,再用maven打包部署后, 一直报 Invalid bound statement (not ...

  7. 第九篇:Spring的applicationContext.xml配置总结

    在前面的一篇日志中,记录了web.xml配置启动的顺序,web启动到监听器ContextLoaderListener时,开始加载spring的配置文件applicationContext.xml(通常 ...

  8. 图解 5 种 Join 连接及实战案例!(inner/ left/ right/ full/ cross)

    Join 连接在日常开发用得比较多,但大家都搞清楚了它们的使用区别吗??一文带你上车~~ 内连接 inner join 内连接是基于连接谓词将俩张表(如A和B)的列组合到一起产生新的结果表,在表中存在 ...

  9. P1417 烹调方案 /// DP(假设 简化公式 排序)

    题目大意: https://www.luogu.org/problemnew/show/P1417 题解 看第一份方法的公式 排序后01背包 #include <bits/stdc++.h> ...

  10. CSS三大特性之继承性

    1.并不是所有的属性都可以继承,只有以color/font/text/line开头的属性 才可以继承. 2.在CSS的继承中,不仅仅是儿子可以继承,只要是后代都可以继承. 3.继承中的特殊性 3.1  ...