go-micro微服务教程五:Registry服务注册和发现

Micro内置了mDNS组播系统,这是一种零依赖的服务注册发现机制,它不依赖其他组件,可以当做本地开发的服务发现方式。本节课我们使用etcd代替mDNS,替换很简单,启动微服务时追加参数–registry etcd或配置环境变量MICRO_REGISTRY=etcd 即可

安装 etcd

根据官方文档进行安装

script
1
2
3
4
5
6
7
8
9
git clone https://github.com/etcd-io/etcd.git
cd etcd
./build
cd bin
cp etcd etcdctl /usr/local/bin
#或者
brew install etcd
#启动etcd
etcd

go-micro微服务教程三:实现用户信息微服务

本示例的目的是演示一个3层的用户服务架构,获取本节代码

3层服务架构:micro api->api service->backend service

  • 网关 micro api: (localhost:8080) - http访问入口网关
  • API层 api service: (io.github.entere.api.user) - 对外暴露的API服务
  • 服务层 backend service: (io.github.entere.srv.user) - 内网的后台服务
    本节,我们先实现backend service 以下简称 srv

在 定义User原型

生成微服务骨架代码

script
1
2
cd micro-examples/part2
micro new user/srv --type=srv --alias=user --namespace=io.github.entere --gopath=false

修改vi user/srv/proto/user/user.proto中的内容,定义User原型

go-micro微服务教程二:创建微服务

本示例的目的是用go-micro完成一个简单微服务(srv) 获取本节代码

micro 工具包提供代码生成器指令 micro new,它可以新建服务模板代码,把基本所需的目录结构建好,省去大家挖坑的时间。这个指令我们在上一课安装过。

在开发之前,我们需要先理清 micro api, api, srv 三者之间的关系,这也是初学都容易迷惑的地方。

  • micro api 网关层,本质上是一个http协议的网关接口,它会把动态路由到转到后台服务中。默认监听8080端口。命令启动:micro api –handler=xx –namespace=xx
  • api api服务层,会把请求的参数组装成srv想要的形式,如果服务非常简单,可以不要api, 直接micro api -> srv
  • srv 后端微服务层,主要向所有内部服务提供接口

正常用 micro api->api->srv这样的调用
调用流程: 发起http请求到网关, 然后进入api层执行对应的handler, handler里面封装创建 具体微服务的rpc client 并向后端发起rpc请求.

Golang使用go.mod配置加载本地模块

如何在当前项目中加载另一个本地正在开发的模块呢?go 提供了replace命令,当我们遇上有些包无法下载(暂未上传或需要外网访问)时,我们可以在mod文件中用replace命令,让程序去指定的地方找代码,如:

看一下我们的go.mod的配置
go.mod

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17


module github.com/entere/micro-examples

go 1.13

require (
github.com/golang/protobuf v1.3.2
github.com/micro/go-micro v1.15.1
)

replace (
//暂未上传,找本地包代替
github.com/entere/micro-examples => /Users/entere/github/micro-examples
//需要外网访问,找github上的包代替
golang.org/x/crypto => github.com/golang/crypto latest
)

Part 01 Golong 微服务教程:ProtoBuf 与 gRPC 初步使用

预置条件

我们需要下面准备工作:

1、安装protobuf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 源码安装

$ wget https://github.com/protocolbuffers/protobuf/releases/download/v3.6.1/protobuf-all-3.6.1.tar.gz
$ tar zxvf protobuf-all-3.6.1.tar.gz
$ cd protobuf-3.6.1/
$ ./autogen.sh
$ ./configure
$ make
$ make install
$ protoc -h

# 或者
$ git clone https://github.com/google/protobuf
$ cd protobuf
$ ./autogen.sh
$ ./configure
$ make
$ make check
$ sudo make install

高并发之-缓存击穿的解决方案

缓存击穿

学生在周末一起上编程课时,发现MySQL突然压力变大,项目中读的地方都加有Cache,按道理大部分数据都会被挡在Cache层,不会打到数据库,于是怀疑应该缓存击穿问题,查看编程游戏每关通用配置文件代码,果然发现问题。有问题,马上改进是我党一惯作风……

先说一下什么缓存击穿,缓存击穿是指一个key非常热点,在不停的扛着大并发,大并发集中对这一个点进行访问,当这个key在失效的瞬间,持续的大并发就穿破缓存,直接请求数据库,就像在一个屏障上凿开了一个洞。

入坑MySQL group_concat 长度限制默认1024

问题描述

开发过程中发现部分数据量稍大的业务 用group_concat出错,sql 如下

1
2
3
4
5
6
7
8
9

select
challenge_id,tilemap_id,name,intro,chapter_id,
IFNULL(solution_tips,''),fail_again_tips,suggest_function,IFNULL(suggest_solution,''),suggest_line_number,pass_conditions,
is_show_grid,is_show_mirror,is_mouse,goal,reward_code_coin,
reward_experience,reward_experience_unicorn,reward_experience_york,reward_experience_hygeia,reward_items_boy,
reward_items_girl,reward_cert,npcs_in_backpack,npcs_in_bosswar,IFNULL(bosswar_url,''),
IFNULL(video_url,''),IFNULL(background_music,''),IFNULL(gametv,''),IFNULL(audio,''),
(select concat('[',group_concat('{"command_id":',game_commands.command_id,',"name":"',game_commands.name,'","cover_url":"',game_commands.cover_url,'","description":"',game_commands.description,'","example":"',game_commands.example,'","code":"',game_commands.code,'"}'),']') from game_commands where json_contains(game_challenges.command_ids, json_array(game_commands.id))) as command_ids from game_challenges where challenge_id = 47864220

问题解决

在MySQL中 group_concat 长度限制默认1024,如果大于1024会被截断