微服务框架---搭建 go-micro环境

1.安装micro

需要使用GO1.11以上版本

#linux 下
export GO111MODULE=on
export GOPROXY=https://goproxy.io
# windows下设置如下环境变量
setx GO111MODULE on
setx GOPROXY https://goproxy.io
# 使用如下指令安装
go get -u -v github.com/micro/micro
go get -u -v github.com/micro/go-micro

2.安装 protoc

https://github.com/protocolbuffers/protobuf/releases
//下载,不同的版本文件名称不一样,我们这里选择protoc-3.9.1-win64.zip
protoc-3.9.1-win64.zip
//解压到目标文件架,我们以e:\dev为例
e:\dev\protoc-3.9.1-win64
//添加e:\dev\protoc-3.9.1-win64\bin到环境变量path

3.安装protoc-gen-micro插件

//这个插件主要作用是通过.proto文件生成适用于go-micro的代码
go get -u -v github.com/micro/protoc-gen-micro

4.安装 consul

//下载windows版本
https://www.consul.io/downloads.html
//解压到
e:\dev\consul

添加e:\dev\consul到环境变量path
使用如下指查看是否安装成功,如下所示安装成功

>consul
Usage: consul [--version] [--help] <command> [<args>]
Available commands are:acl            Interact with Consul's ACLsagent          Runs a Consul agentcatalog        Interact with the catalogconfig         Interact with Consul's Centralized Configurationsconnect        Interact with Consul Connectdebug          Records a debugging archive for operators

5.创建微服务 hello world

//使用如下指令创建微服务
>micro new techidea8.com/microapp/hello
Creating service go.micro.srv.hello in E:\winlion\gopath\src\techidea8.com\microapp\hello.
├── main.go
├── plugin.go
├── handler
│   └── hello.go
├── subscriber
│   └── hello.go
├── proto\hello
│   └── hello.proto
├── Dockerfile
├── Makefile
├── README.md
└── go.moddownload protobuf for micro:brew install protobuf
go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
go get -u github.com/micro/protoc-gen-microcompile the proto file hello.proto:cd E:\winlion\gopath\src\techidea8.com\microapp\hello
protoc --proto_path=.:$GOPATH/src --go_out=. --micro_out=. proto/hello/hello.proto

6.生成适配proto的golang代码

#切换到项目目录下
>cd /d E:\winlion\gopath\src\techidea8.com\microapp\hello# 根据proto生成文件
>protoc --proto_path=. --go_out=. --micro_out=. proto/hello/hello.proto# 启动应用
>go run main.go
2019/08/19 13:00:46 Transport [http] Listening on [::]:54689
2019/08/19 13:00:46 Broker [http] Connected to [::]:54690
2019/08/19 13:00:46 Registry [mdns] Registering node: go.micro.srv.hello-4851dce2-ab5d-4e4c-801e-44dae5d93f26
2019/08/19 13:00:46 Subscribing go.micro.srv.hello-4851dce2-ab5d-4e4c-801e-44dae5d93f26 to topic: go.micro.srv.hello
2019/08/19 13:00:46 Subscribing go.micro.srv.hello-4851dce2-ab5d-4e4c-801e-44dae5d93f26 to topic: go.micro.srv.hello# 查看是否启动
>micro list services
go.micro.srv.hello
topic:go.micro.srv.hello

7.启动restful api接口支持支持

注意其中的--namespace参数,我们每一个微服务都属于一个命名空间,通过api暴露出来该命名空间后,满足go.micro.srv.*格式的微服务都可以访问。如go.micro.srv.hello可以通过如下格式访问

# 访问http://127.0.0.1:8080/hello/call>micro api --namespace=go.micro.srv
2019/08/19 13:07:11 Registering API Default Handler at /
2019/08/19 13:07:11 HTTP API Listening on [::]:8080
2019/08/19 13:07:11 Transport [http] Listening on [::]:54934
2019/08/19 13:07:11 Broker [http] Connected to [::]:54935
2019/08/19 13:07:11 Registry [mdns] Registering node: go.micro.api-1753185c-b8e1-49c4-aa0f-617f243a8e2a

8.测试

1493082-20190905104203353-197386462.png

转载于:https://www.cnblogs.com/Paul-watermelon/p/11463128.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391397.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

angular dom_Angular 8 DOM查询:ViewChild和ViewChildren示例

angular domThe ViewChild and ViewChildren decorators in Angular provide a way to access and manipulate DOM elements, directives and components. In this tutorial, well see an Angular 8 example of how to use the two decorators.Angular中的ViewChild和ViewChild…

浪潮之巅——IT产业的三大定律

http://www.cnblogs.com/ysocean/p/7641540.html转载于:https://www.cnblogs.com/czlovezmt/p/8325772.html

DStream算子讲解(一)

先把目录列好&#xff0c;方便有条理的进行整理转载于:https://www.cnblogs.com/leodaxin/p/7507600.html

aws 静态网站_如何使用AWS托管静态网站-入门指南

aws 静态网站When I created my first portfolio last year, I based it on what I had learned from freeCodeCamp (HTML, CSS and a little JavaScript). 去年创建我的第一个投资组合时 &#xff0c;我基于从freeCodeCamp (HTML&#xff0c;CSS和一些JavaScript)中学到的知识…

leetcode 27. 移除元素(双指针)

给你一个数组 nums 和一个值 val&#xff0c;你需要 原地 移除所有数值等于 val 的元素&#xff0c;并返回移除后数组的新长度。 不要使用额外的数组空间&#xff0c;你必须仅使用 O(1) 额外空间并 原地 修改输入数组。 元素的顺序可以改变。你不需要考虑数组中超出新长度后面…

使用TVP批量插入数据

TVP&#xff08;全称 :Table-Valued Parameter&#xff09; 叫做表值参数(Table-Valued Parameter)是SQL2008的一个新特性。顾名思义&#xff0c;表值参数表示你可以把一个表类型作为参数传递到函数或存储过程里。 第一步&#xff1a;创建一个Type类型和写入数据的原始表结构相…

python:找出两个列表中相同和不同的元素(使用推导式)

#接口返回值 list1 [张三, 李四, 王五, 老二] #数据库返回值 list2 [张三, 李四, 老二, 王七]a [x for x in list1 if x in list2] #两个列表表都存在 b [y for y in (list1 list2) if y not in a] #两个列表中的不同元素print(a的值为:,a) print(b的值为:,b)c [x for x …

springcloud(六):配置中心git示例

随着线上项目变的日益庞大&#xff0c;每个项目都散落着各种配置文件&#xff0c;如果采用分布式的开发模式&#xff0c;需要的配置文件随着服务增加而不断增多。某一个基础服务信息变更&#xff0c;都会引起一系列的更新和重启&#xff0c;运维苦不堪言也容易出错。配置中心便…

写作工具_4种加快数据科学写作速度的工具

写作工具I’ve been writing about data science on Medium for just over two years. Writing, in particular, technical writing can be time-consuming. Not only do you need to come up with an idea, write well, edit your articles for accuracy and flow, and proofr…

leetcode 91. 解码方法(dp)

解题思路 记忆化搜索&#xff0c;记录已经计算过的子问题 代码 func numDecodings(s string) int {temp:make([]int,len(s),len(s))for i : range temp {temp[i]-1}return de(s,0,temp) } func de(s string,cur int,dp []int) int {if curlen(s){return 1}if dp[cur]!-1{re…

python数据结构与算法

2019独角兽企业重金招聘Python工程师标准>>> http://python.jobbole.com/tag/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/ 转载于:https://my.oschina.net/u/3572879/blog/1611369

test5

test5 转载于:https://www.cnblogs.com/Forever77/p/11468284.html

ux和ui_阅读10个UI / UX设计系统所获得的经验教训

ux和uiAs a way to improve my UI/UX skills I decided to read the guidelines for 10 popular UI/UX design systems. In this article I will give you a concise summary of the most important concepts. 为了提高我的UI / UX技能&#xff0c;我决定阅读10种流行的UI / UX…

大数据(big data)_如何使用Big Query&Data Studio处理和可视化Google Cloud上的财务数据...

大数据(big data)介绍 (Introduction) This article will show you one of the ways you can process stock price data using Google Cloud Platform’s BigQuery, and build a simple dashboard on the processed data using Google Data Studio.本文将向您展示使用Google Cl…

第1次作业:阅读优秀博文谈感想

摘要&#xff1a;本文介绍第1次作业的详细内容&#xff0c;包括评分标准。 注&#xff1a;本次作业提交截止时间为UTC8(北京时间)&#xff0c;2017-9-17 22:00&#xff08;星期日&#xff09;&#xff0c;以博客发表日期为准。 1. 作业内容 阅读一些优秀博文&#xff08;见第二…

ubuntu 16.04常用命令

ip配置&#xff1a; 终端输入vi /etc/network/interfaces命令编辑配置文件,增加如下内容&#xff1a;         auto enp2s0    iface enp2s0 inet static    address 192.168.1.211    netmask 255.255.255.0    gateway 192.168.1.1 重启网卡&#xf…

leetcode 28. 实现 strStr()(kmp)

实现 strStr() 函数。 给你两个字符串 haystack 和 needle &#xff0c;请你在 haystack 字符串中找出 needle 字符串出现的第一个位置&#xff08;下标从 0 开始&#xff09;。如果不存在&#xff0c;则返回 -1 。 说明&#xff1a; 当 needle 是空字符串时&#xff0c;我们…

git 代码推送流程_Git 101:一个让您开始推送代码的Git工作流程

git 代码推送流程Im going to explain Git the way I wish someone had explained to me back when I was first learning. 我将以我希望有人在我第一次学习时向我解释的方式来解释Git。 Ill show how you can get started with just a few commands, and the concepts at wor…

多元时间序列回归模型_多元时间序列分析和预测:将向量自回归(VAR)模型应用于实际的多元数据集...

多元时间序列回归模型Multivariate Time Series Analysis多元时间序列分析 A univariate time series data contains only one single time-dependent variable while a multivariate time series data consists of multiple time-dependent variables. We generally use mult…

字符串基本操作

1.已知‘星期一星期二星期三星期四星期五星期六星期日 ’&#xff0c;输入数字&#xff08;1-7&#xff09;&#xff0c;输出相应的‘星期几 s星期一星期二星期三星期四星期五星期六星期日 d int(input(输入1-7:)) print(s[3*(d-1):3*d]) 2.输入学号&#xff0c;识别年级、专业…