首页 > 其他分享 >go 和 elixir 的 grpc 通信

go 和 elixir 的 grpc 通信

时间:2022-10-19 10:58:55浏览次数:85  
标签:do end mix grpc start init elixir go

步骤

准备

安装 protoc-gen-elixir

mix escript.install hex protobuf

具体步骤

创建项目

mix new appdemo

编写 pb 文件

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

生成代码

protoc --elixir_out=./lib helloword.proto
protoc -I . --elixir_out=plugins=grpc:./lib/ helloword.proto

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

lib/helloworld_sup.ex

defmodule HelloworldSup do
  use Supervisor

  def start_link(init_arg1, init_arg2) do
    Supervisor.start_link(__MODULE__, [init_arg1, init_arg2], name: __MODULE__)
  end

  @impl true
  def init([_init_arg1, _init_arg2]) do
    children = [
      %{
        id: GRPC.Server.Supervisor,
        start: {GRPC.Server.Supervisor, :start_link, [{Helloworld.Greeter.Server, 50051}]}
      }
    ]

    Supervisor.init(children, strategy: :one_for_one)
  end
end

helloworld_app.ex

defmodule HelloworldApp do
  use Application

  def start(_type, _args) do
    HelloworldSup.start_link("aaa", "bbb")
  end
end

config/config.exs

import Config

# Start server in OTP
config :grpc, start_server: true

mix.exs

def application do
  [
    mod: {HelloworldApp, []},
    extra_applications: [:logger]
  ]
end

# Run "mix help deps" to learn about dependencies.
defp deps do
  [
    {:grpc, "~> 0.5.0"},
    {:protobuf, "~> 0.10"}
  ]
end

启动

mix deps.get
iex -S mix

go的例子

这里

标签:do,end,mix,grpc,start,init,elixir,go
From: https://www.cnblogs.com/ziyouchutuwenwu/p/16805446.html

相关文章

  • Django之form表单的数据验证
     1.先导入forms模块fromdjangoimportforms 2.创建模板的类classloginform(forms.Form):#2、模板中的元素name=forms.CharField(min_length=6,e......
  • 解决Mongo时间自动减8小时
    publicclassChangeDateTimeKind{publicstaticasyncTaskToUtc<T>(Tmodel){TypeNewType=typeof(T);forea......
  • 我的Vue之旅 07 Axios + Golang + Sqlite3 实现简单评论机制
    第三期·使用Vue3.1+Axios+Golang+Sqlite3实现简单评论机制效果图CommentArea.vue我们需要借助js的Data对象把毫秒时间戳转化成UTCString()。并在模板......
  • 03-Go的执行原理及Go的常用命令
    go的源码文件分为三类:命令源码文件,库源码文件,测试源码文件命令源码文件:后缀.go的文件,一个目录下,只能有一个main的入口,否则build或install会报错。库源码文件:普通的源码......
  • Go 语言入门很简单:Go 语言的错误处理
    本文将介绍Go中的错误处理,以及为什么我们需要错误处理。什么是错误处理异常处理是任何语言都不能绕不开的话题。Go语言没有提供传统的​​try...catch​​语句来处理异......
  • Go素数筛选分析
    Go素数筛选分析1.素数筛选介绍学习Go语言的过程中,遇到素数筛选的问题。这是一个经典的并发编程问题,是某大佬的代码,短短几行代码就实现了素数筛选。但是自己看完原理和代......
  • Go struct字段添加指针与不添加指针的区别
    packagemainimport("fmt")typeNstruct{Namestring`json:"name"`Ageint`json:"age"`B*BBB`json:"b"`}typeBBBstruct{yystringbbbyte......
  • 【C++】GoogleTest进阶之gMock
    gMock是什么当我们去写测试时,有些测试对象很单纯简单,例如一个函数完全不依赖于其他的对象,那么就只需要验证其输入输出是否符合预期即可。但是如果测试对象很复杂或者依赖......
  • Golang编辑器 - GoLand安装
    GoLand官网https://www.jetbrains.com.cn/go/下载GoLand通过访问其官网进行安装包下载下载完成之后双击运行安装程序点击Next选择安装的路径下一步选择创建......
  • K8s client-go watch pod
    一.前言我们在使用kubectl操作k8s时,可以在命令中加入-w来观察资源变化,比如kubectlgetpod-w观察pod状态变化。出了使用控制台,还可以编写代码和k8s交互来获取......