基于hprose-golang创建RPC微服务
Hprose(High Performance Remote Object Service Engine)
是一款先进的轻量级、跨语言、跨平台、无侵入式、高性能动态远程对象调用引擎库。它不仅简单易用,而且功能强大。
本文将讲解如何使用Hprose go 服务端编写一个微服务,并实现客户端调用。
本文的涉及的项目代码托管在github:https://github.com/52fhy/hprose-sample 。
使用Go实现服务端
初始化
git初始化:
git init
echo "main" >> .gitignore
echo "# hprose-sample" >> README.md
项目使用go mod管理依赖,请确保安装的Go版本支持该命令。先初始化go.mod文件:
go mod init sample
最终项目目录结构一览:
├── config
│ └── rd.ini
├── dao
├── main.go
├── model
└── util
├── config.go
└── state.go
├── service
│ └── sample.go
├── go.mod
├── go.sum
├── client_test.go
├── README.md
├── php
├── logs
golang写微服务的好处就是我们可以按照自己理想的目录结构写代码,而无需关注代码 autoload 问题。
配置项
我们使用go-ini/ini来管理配置文件。
项目地址:https://github.com/go-ini/ini
文档地址:https://ini.unknwon.io/
这个库使用起来很简单,文档完善。有2种用法,一种是直接加载配置文件,一种是将配置映射到结构体,使用面向对象的方法获取配置。这里我们采用第二种方案。
首先在conf/里建个配置文件rd.ini:
ListenAddr = 0.0.0.0:8080
[Mysql]
Host = localhost
Port = 3306
User = root
Password =
Database = sample
[Redis]
Host = localhost
Port = 6379
Auth =
编写util/config.go加载配置:
package util
import "github.com/go-ini/ini"
type MysqlCfg struct{
Host string
Port int32
User string
Password string
Database string
}
type RedisCfg struct{
Host string
Port int32
Auth string
}
type Config struct {
ListenAddr string
Mysql MysqlCfg
Redis RedisCfg
}
//全局变量
var Cfg Config
//加载配置
func InitConfig(ConfigFile string) error {
return ini.MapTo(Cfg, ConfigFile)
}
main.go
这里我们需要实现项目初始化、服务注册到RPC并启动一个TCP server。
package main
import (
"flag"
"fmt"
"github.com/hprose/hprose-golang/rpc"
"sample/service"
"sample/util"
)
func hello(name string) string {
return "Hello " + name + "!"
}
func main() {
//解析命令行参数
configFile := flag.String("c", "config/rd.ini", "config file")
flag.Parse()
err := util.InitConfig(*configFile)
if err != nil {
fmt.Printf("load config file fail, err:%v\n", err)
return
}
fmt.Printf("server is running at %s\n", util.Cfg.ListenAddr)
//tcp,推荐
server := rpc.NewTCPServer("tcp4://" + util.Cfg.ListenAddr + "/")
//注册func
server.AddFunction("hello", hello)
//注册struct,命名空间是Sample
server.AddInstanceMethods(&service.SampleService{}, rpc.Options{NameSpace: "Sample"})
err = server.Start()
if err != nil {
fmt.Printf("start server fail, err:%v\n", err)
return
}
}
我们看到,RPC里注册了一个函数hello,还注册了service.SampleService里的所有方法。
注:这里注册服务的时候使用了
NameSpace选项从而支持命名空间,这个在官方的WIKI里没有示例说明,很容易忽略。
其中SampleService是一个结构体,定义在service/sample.go文件里:
sample.go:
package service
import (
"sample/model"
"sample/util"
)
//定义服务
type SampleService struct {
}
//服务里的方法
func (this *SampleService) GetUserInfo(uid int64) util.State {
var state util.State
if uid <= 0 {
return state.SetErrCode(1001).SetErrMsg("uid不正确").End()
}
var user model.User
user.Id = uid
user.Name = "test"
return state.SetData(user).End()
}
日志
作为一个线上项目,我们需要在业务代码里打印一些日志辅助我们排查问题。日志这里直接使用 beego的日志库logs。
package util
import (
"errors"
"fmt"
"github.com/astaxie/beego/logs"
)
var Logger *logs.BeeLogger
func InitLog() error {
Logger = logs.NewLogger(10)
err := Logger.SetLogger(logs.AdapterMultiFile, fmt.Sprintf(`{"filename":"/work/git/hprose-sample/logs/main.log", "daily":true,"maxdays":7,"rotate":true}`))
if err != nil {
return errors.New("init beego log error:" + err.Error())
}
Logger.Async(1000)
return nil
}
这里定义里全局变量Logger,之后可以在项目任意地方使用。
日志选项里
filename最好是动态配置,这里为了演示,直接写的固定值。
使用示例:
if uid <= 0 {
util.Logger.Debug("uid error. uid:%d", uid)
}
Go测试用例
每个项目都应该写测试用例。下面的用例里,我们将测试上面注册的服务是否正常。
package main
import (
"github.com/hprose/hprose-golang/rpc"
"sample/util"
"testing"
)
//stub:申明服务里拥有的方法
type clientStub struct {
Hello func(string) string
GetUserInfo func(uid int64) util.State
}
//获取一个客户端
func GetClient() *rpc.TCPClient {
return rpc.NewTCPClient("tcp4://127.0.0.1:8050")
}
//测试服务里的方法
func TestSampleService_GetUserInfo(t *testing.T) {
client := GetClient()
defer client.Close()
var stub clientStub
client.UseService(&stub, "Sample") //使用命名空间
rep := stub.GetUserInfo(10001)
if rep.ErrCode > 0 {
t.Error(rep.ErrMsg)
} else {
t.Log(rep.Data)
}
}
//测试普通方法
func TestHello(t *testing.T) {
client := GetClient()
defer client.Close()
var stub clientStub
client.UseService(&stub)
rep := stub.Hello("func")
if rep == "" {
t.Error(rep)
} else {
t.Log(rep)
}
}
运行:
$ go test -v
=== RUN TestSampleService_GetUserInfo
--- PASS: TestSampleService_GetUserInfo (0.00s)
client_test.go:31: map[name:test id:10001]
=== RUN TestHello
--- PASS: TestHello (0.00s)
client_test.go:47: Hello func!
PASS
ok sample 0.016s
PHP调用
php-client
需要先下载hprose/hprose:
composer config repo.packagist composer https://packagist.phpcomposer.com
composer require "hprose/hprose:^2.0"
client.php
<?php
include "vendor/autoload.php";
try{
$TcpServerAddr = "tcp://127.0.0.1:8050";
$client = \Hprose\Socket\Client::create($TcpServerAddr, false);
$service = $client->useService('', 'Sample');
$rep = $service->GetUserInfo(10);
print_r($rep);
} catch (Exception $e){
echo $e->getMessage();
}
运行:
$ php php/client.php
stdClass Object
(
[errCode] => 0
[errMsg] =>
[data] => stdClass Object
(
[id] => 10
[name] => test
)
)
实际使用时最好对该处调用的代码做进一步的封装,例如实现异常捕获、返回码转换、日志打印等等。
编写codetips
本节不是必须的,但是在多人合作的项目上,可以提高沟通效率。
hprose 不支持一键生成各语言的客户端代码(没有IDL支持),在写代码的时候PHP编译器没法提示。我们可以写一个类或者多个类,主要是Model类和Service类:
- Model类定义字段属性,当传参或者读取返回对象里内容的是,可以使用
Get/Set方法; - Service类类似于抽象类,仅仅是把go服务端里的方法用PHP定义一个空方法,包括参数类型、返回值类型,这个类并不会真正引入,只是给IDE作为代码提示用的。
示例:
class SampleService
{
/**
* 获取用户信息
* @param int $uid
* @return State
*/
public function GetUserInfo(int $uid): State
{
}
}
调用的地方(请使用phpStorm查看提示效果):
/**
* @return SampleService
* @throws Exception
*/
function getClient()
{
$TcpServerAddr = "tcp://127.0.0.1:8050";
$client = \Hprose\Socket\Client::create($TcpServerAddr, false);
$service = $client->useService('', 'Sample');
return $service;
}
try {
$client = getClient();
$rep = $client->GetUserInfo(10);
echo $rep->errCode . PHP_EOL;
print_r($rep);
} catch (Exception $e) {
echo $e->getMessage();
}
方法getClient返回的注释里加了@return SampleService,下面调用的$rep->errCode就会有代码提示了。详见:https://github.com/52fhy/hprose-sample/tree/master/php 。
部署
线上微服务需要后台长期稳定运行,可以使用supervisord工具。
如果还没有安装,请餐参考:Supervisor使用教程。
新增一个常驻任务,需要新建配置。
以上述sample为例,新建配置:go_hprose_sample.ini:
[program:go_hprose_sample]
command=/usr/local/bin/go /work/git/hprose-sample/main
priority=999 ; the relative start priority (default 999)
autostart=true ; start at supervisord start (default: true)
autorestart=true ; retstart at unexpected quit (default: true)
startsecs=10 ; number of secs prog must stay running (def. 10)
startretries=3 ; max # of serial start failures (default 3)
exitcodes=0,2 ; 'expected' exit codes for process (default 0,2)
stopsignal=QUIT ; signal used to kill process (default TERM)
stopwaitsecs=10 ; max num secs to wait before SIGKILL (default 10)
user=root ; setuid to this UNIX account to run the program
log_stdout=true
log_stderr=true ; if true, log program stderr (def false)
logfile=/work/git/hprose-sample/logs/supervisor/go_hprose_sample.log
logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
logfile_backups=10 ; # of logfile backups (default 10)
stdout_logfile_maxbytes=20MB ; stdout 日志文件大小,默认 50MB
stdout_logfile_backups=20 ; stdout 日志文件备份数
stdout_logfile=/work/git/hprose-sample/logs/supervisor/go_hprose_sample.stdout.log
注:上述配置仅供参考,请务必理解配置的含义。
然后启动任务:
supervisorctl reread
supervisorctl update
supervisorctl start go_hprose_sample
线上部署最少要2台机器,组成负载均衡。这样当升级的时候,可以一台一台的上线,避免服务暂停。
Hprose 总结
优点:
- 轻量级、跨语言、跨平台
- 更少的网络传输量,使用二进制传输协议
- 简单,跟着官方提供的例子很快就能掌握基本的使用
- 文档完善
缺点:
- 不支持IDL(接口描述语言),所以无法一键生成客户端调用代码,需要手动维护
参考
1、Supervisor使用教程 - 飞鸿影 - 博客园
https://www.cnblogs.com/52fhy/p/10161253.html
2、Home · hprose/hprose-golang Wiki
https://github.com/hprose/hprose-golang/wiki
3、go-ini/ini: 超赞的 Go 语言 INI 文件操作
https://ini.unknwon.io/
4、golang中os/exec包用法
https://www.cnblogs.com/vijayfly/p/6102470.html
基于hprose-golang创建RPC微服务的更多相关文章
- QCon技术干货:个推基于Docker和Kubernetes的微服务实践
2016年伊始,Docker无比兴盛,如今Kubernetes万人瞩目.在这个无比需要创新与速度的时代,由容器.微服务.DevOps构成的云原生席卷整个IT界.在近期举办的QCon全球软件开发大会上, ...
- SpringCloud系列二:Restful 基础架构(搭建项目环境、创建 Dept 微服务、客户端调用微服务)
1.概念:Restful 基础架构 2.具体内容 对于 Rest 基础架构实现处理是 SpringCloud 核心所在,其基本操作形式在 SpringBoot 之中已经有了明确的讲解,那么本次为 了清 ...
- 【GoLang】go 微服务框架 && Web框架学习资料
参考资料: 通过beego快速创建一个Restful风格API项目及API文档自动化: http://www.cnblogs.com/huligong1234/p/4707282.html Go 语 ...
- 在阿里云容器服务上开发基于Docker的Spring Cloud微服务应用
本文为阿里云容器服务Spring Cloud应用开发系列文章的第一篇. 一.在阿里云容器服务上开发Spring Cloud微服务应用(本文) 二.部署Spring Cloud应用示例 三.服务发现 四 ...
- 基于Apollo实现.NET Core微服务统一配置(测试环境-单机)
一.前言 注:此篇只是为测试环境下的快速入门.后续会给大家带来生产环境下得实战开发. 具体的大家可以去看官方推荐.非常的简单明了.以下介绍引用官方内容: Apollo(阿波罗)是携程框架部门研发的分布 ...
- 基于 Apache APISIX 的下一代微服务架构
2019 年 12 月 14 日,又拍云联合 Apache APISIX 社区举办 API 网关与高性能服务最佳实践丨Open Talk 广州站活动,Apache APISIX PPMC 温铭做了题为 ...
- 场景实践:基于 IntelliJ IDEA 插件部署微服务应用
体验简介 阿里云云起实验室提供相关实验资源,点击前往 本场景指导您把微服务应用部署到 SAE 平台: 登陆 SAE 控制台,基于 jar 包创建应用 基于 IntelliJ IDEA 插件更新 SAE ...
- NodeJS 基于 Dapr 构建云原生微服务应用,从 0 到 1 快速上手指南
Dapr 是一个可移植的.事件驱动的运行时,它使任何开发人员能够轻松构建出弹性的.无状态和有状态的应用程序,并可运行在云平台或边缘计算中,它同时也支持多种编程语言和开发框架.Dapr 确保开发人员专注 ...
- 基于 Spring Cloud 完整的微服务架构实战
本项目是一个基于 Spring Boot.Spring Cloud.Spring Oauth2 和 Spring Cloud Netflix 等框架构建的微服务项目. @作者:Sheldon地址:ht ...
随机推荐
- Codeforces Gym101097I:Sticks (思维)
http://codeforces.com/gym/101097/attachments 题意:现在有k种颜色的木棍,每种颜色有ni根木棍,每根木棍有一个长度,问是否有三根木棍可以组成三角形,并且这三 ...
- 自定义docker镜像
1.拉去centos当作基础镜像 2.重要!!!必须使用超级权限启动: docker run --privileged -ti --name latestCenos centos /usr/sbin ...
- c++书籍推荐《C++编码规范》下载
百度云及其他网盘下载地址:点我 编辑推荐 <C++编程规范:101条规则.准则与 实践>:良好的编程规范可以改善软件质量,缩短上市时间,提升团队效率,简化维护工作.在<C++编程规范 ...
- Java核心技术中的程序片段
import java.io.*; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import ...
- 【题解】危险的工作-C++
Description 给出一个数字N,N<=11.代表有N个人分担N个危险的工作. 每个人对应每个工作,有个危险值 每个人担任其中一项,问每个人危险值相加,最小值是多少. Input 第一行给 ...
- Linux下程序下载
每个开发板/PC机都有硬盘(ROM,read only memory,只读存储器)和运行内存(RAM,random access memory,随机存取存储器).其中Nand/Nor flash相当于 ...
- Excel导出功能超时解决方案 -- 异步处理
背景 有运营同学反馈,最近导出excel会出现超时的情况,初步判断是数据增长太快,数据量太大,请求时间超过了设置的超时时间 尝试 有考虑直接更改该请求的超时时长,可是治标不治本 网上搜索发现,有很多人 ...
- pymysql指南
1 引言 mysql应该说是如今使用最为普遍的数据库了,没有之一,而Python作为最为流行的语言之一,自然少不了与mysql打交道,pymysql就是使用最多的工具库了. 2 创建库.表 我们先从创 ...
- 十、SQL中EXISTS的用法 十三、sql server not exists
十.SQL中EXISTS的用法 EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值True或False EXISTS 指定一个子查询,检测 行 的存在. 语法 ...
- Java 常见面试题整理
操作系统 说一下线程和进程,它们的区别 同步和异步的区别 阻塞和非阻塞的区别 操作系统中死锁的四个必要条件 mmap和普通文件读写的区别,mmap的注意点 CPU密集型和IO密集型的区别 Linux ...