elixir grpc 试用
备注:
a. 安装 protoc 参考相关文档,比较简单
b. 安装elixir grpc 插件 protoc-gen-elixir 同时配置环境变量
a. 创建项目
mix new appdemo
cd appdemo
touch helloword.proto
syntax = "proto3";
option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
项目结构如下:
├── README.md
├── config
│ └── config.exs
├── helloword.proto
├── lib
│ ├── appdemo.ex
├── mix.exs
└── test
├── appdemo_test.exs
└── test_helper.exs
b. 生成代码
protoc --elixir_out=./lib helloword.proto
protoc -I . --elixir_out=plugins=grpc:./lib/ helloword.proto
结果如下:
├── README.md
├── config
│ └── config.exs
├── helloword.proto
├── lib
│ ├── appdemo.ex
│ └── helloword.pb.ex
├── mix.exs
└── test
├── appdemo_test.exs
└── test_helper.exs
内容:
defmodule Helloworld.HelloRequest do
use Protobuf
@type t :: %__MODULE__{
name: String.t()
}
defstruct [:name]
field :name, 1, optional: true, type: :string
end
defmodule Helloworld.HelloReply do
use Protobuf
@type t :: %__MODULE__{
message: String.t()
}
defstruct [:message]
field :message, 1, optional: true, type: :string
end
defmodule Helloworld.Greeter.Service do
use GRPC.Service, name: "helloworld.Greeter"
rpc :SayHello, Helloworld.HelloRequest, Helloworld.HelloReply
end
defmodule Helloworld.Greeter.Stub do
use GRPC.Stub, service: Helloworld.Greeter.Service
end
a. server端实现代码
lib/server.ex
defmodule Helloworld.Greeter.Server do
use GRPC.Server, service: Helloworld.Greeter.Service
@spec say_hello(Helloworld.HelloRequest.t(), GRPC.Server.Stream.t()) ::
Helloworld.HelloReply.t()
def say_hello(request, _stream) do
Helloworld.HelloReply.new(message: "Hello #{request.name}")
end
end
b. 项目使用opt 进行运行,具体来说是supervisor
lib/helloworld_app.ex
defmodule HelloworldApp do
use Application
def start(_type, _args) do
import Supervisor.Spec
children = [
supervisor(GRPC.Server.Supervisor, [{Helloworld.Greeter.Server, 50051}])
]
opts = [strategy: :one_for_one, name: HelloworldApp]
Supervisor.start_link(children, opts)
end
end
c. mix.exs 启动配置
def application do
[mod: {HelloworldApp, []},
applications: [:logger, :grpc]]
end
defp deps do
[
{:grpc, github: "tony612/grpc-elixir"},
{:dialyxir, "~> 0.5", only: [:dev, :test], runtime: false},
]
end
d. config/config.exs
use Mix.Config
# Start server in OTP
config :grpc, start_server: true
mix deps.get,compile
iex -S mix
参考 https://github.com/rongfengliang/grpc-elixir
https://github.com/rongfengliang/grpc-elixir
https://github.com/rongfengliang/gprc-elixir-server
https://github.com/tony612/grpc-elixir
elixir grpc 试用的更多相关文章
- nginx grpc 试用
1. 编译 wget https://nginx.org/download/nginx-1.13.10.tar.gz tar xvf nginx-1.13.10.tar.gz cd nginx-1.1 ...
- RPC的故事
今天我跟几个RPC框架之间发生了一些事,情节跌宕起伏一波三折,不吐不快,以至于我这个从来不写博客的人也忍不住写下来分享一下. 背景 主系统部署在Windows上(.NET 4.5),子系统(.NET ...
- phoenix elixir 框架简单试用
备注: 官方提供的脚手架工具,我们可以直接使用,生成代码,同时需要nodejs 环境配置(比较简单,参考 相关资料即可) 1. 安装脚手架 mix archive.install https:/ ...
- purescript 基本试用
安装环境 安装预编译文件 https://github.com/purescript/purescript/releases 配置环境变量: export PATH=$PATH:/Users/dalo ...
- 【译】gRPC vs HTTP APIs
本文翻译自 ASP.NET Blog | gRPC vs HTTP APIs,作者 James,译者 Edison Zhou. 写在开头 现在,ASP.NET Core使开发人员可以构建gRPC服务. ...
- 「译」 .NET 6 中 gRPC 的新功能
gRPC是一个现代的.跨平台的.高性能的 RPC 框架.gRPC for .NET 构建在 ASP.NET Core 之上,是我们推荐的在 .NET 中构建 RPC 服务的方法. .NET 6 进一步 ...
- 解决go-micro与其它gRPC框架之间的通信问题
在之前的文章中分别介绍了使用gRPC官方插件和go-micro插件开发gRPC应用程序的方式,都能正常走通.不过当两者混合使用的时候,互相访问就成了问题.比如使用go-micro插件生成的gRPC客户 ...
- gRPC源码分析1-SSL/TLS
引子 前几天看到微信后台团队分享了TLS相关文章,正好gRPC里TLS数据加密是很重要的一块,于是整理出了这篇文章. 在gRPC里,如果仅仅是用来做后端微服务,可以考虑不加密.本文太长,先给个大纲. ...
- gRPC源码分析2-Server的建立
gRPC中,Server.Client共享的Class不是很多,所以我们可以单独的分别讲解Server和Client的源码. 通过第一篇,我们知道对于gRPC来说,建立Server是非常简单的,还记得 ...
随机推荐
- flask学习(五):使用配置文件
1. 新建一个config.py文件 2. 在主app文件中导入这个文件,并且配置到app中,示例代码如下: import config app.config.from_object(config) ...
- 22.线程池之ScheduledThreadPoolExecutor
1. ScheduledThreadPoolExecutor简介 ScheduledThreadPoolExecutor可以用来在给定延时后执行异步任务或者周期性执行任务,相对于任务调度的Timer来 ...
- RabbitMQ消息队列(九)RPC开始应用吧
一 简单应用 RPC——远程过程调用,通过网络调用运行在另一台计算机上的程序的函数\方法,是构建分布式程序的一种方式.RabbitMQ是一个消息队列系统,可以在程序之间收发消息.利用RabbitMQ可 ...
- Java获取系统环境信息
使用System.getProperty()方法获取JVM信息 public class TestSystemGetProperty { public static void main(String[ ...
- 【51nod-1091】线段的重叠(贪心)
所有线段按起点从小到大排序,然后比较出最大的重叠部分.比如第i条线段和第j条线段进行比较找出重叠部分(j>i),当第j条线段的右端点<第i条线段的右端点,此时可以让i继续比较后面的线段:如 ...
- hdu4309
题解: 暴力枚举 然后网络流 代码: #include<iostream> #include<cstdio> #include<cstring> using nam ...
- Linux服务器通过拷贝的方式安装多个tomcat
Tomcat占用资源少.运行速度快.安装配置简单,在个人开发中拥有广泛的使用者.很多人在使用中存在以下的误区:1.Tomcat必须通过eclipse启动2.Tomcat必须通过安装才能使用运行3.一台 ...
- LeetCode OJ:Burst Balloons(击破气球)
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- yii2.0 使用不同语言
1.建立语言目录.文件.项目根目录建立messages文件夹.存放不同语言对应的目录文件. 例如中文和英文 message 下建立两个文件夹 en.zh_CN 里面可以对应着多个翻译文件 2.在mai ...
- asp.net string有多行文字
用如下格式设置