请问,怎么判断rpc call 是否掉线 重连?
就是怎么rpc掉线后重连
第 1 条附言 ·
这是我写的重连,请指教
<pre>
<code class="language-go">
package main
var bBClient *rpc.Client
var conn net.Conn
func GetInstance() *rpc.Client {
if conn != nil {
err := conn.SetReadDeadline(time.Now().Add(time.Second * 1))
if err != nil {
log.Fatal("setread-----", err)
bBClient = nil
}
} else {
logs.Debug("conn2", conn)
}
if bBClient == nil {
return instance()
}
return bBClient
}
func instance() *rpc.Client {
conn, err := net.Dial("tcp", beego.AppConfig.String("BBRpc"))
if err != nil {
log.Fatal("dialing:", err)
}
return jsonrpc.NewClient(conn)
}
func init() {
instance()
}
</code></pre>
更多评论
我的php 调用GO标准库jsonRPC也是经常掉线,不知道原因
### ip2city jsonRPC server代码
```go
package main
import (
"fmt"
"github.com/oschwald/geoip2-golang"
"log"
"net"
"net/rpc"
"net/rpc/jsonrpc"
"os"
)
type Response struct {
Country string
Province string
City string
ISP string
Latitude float64
Longitude float64
TimeZone string
}
type Ip2addr struct {
db *geoip2.Reader
}
type Agrs struct {
IpString string
}
func (t *Ip2addr) Address(agr *Agrs, res *Response) error {
netIp := net.ParseIP(agr.IpString)
record, err := t.db.City(netIp)
res.City = record.City.Names["zh-CN"]
res.Province = record.Subdivisions[0].Names["zh-CN"]
res.Country = record.Country.Names["zh-CN"]
res.Latitude = record.Location.Latitude
res.Longitude = record.Location.Longitude
res.TimeZone = record.Location.TimeZone
return err
}
func main() {
db, err := geoip2.Open("./GeoLite2-City.mmdb")
if err != nil {
log.Fatal(err)
}
ip2addr := &Ip2addr{db}
rpc.Register(ip2addr)
address := ":3344"
tcpAddr, err := net.ResolveTCPAddr("tcp", address)
checkError(err)
listener, err := net.ListenTCP("tcp", tcpAddr)
checkError(err)
log.Println("json rpc is listening", tcpAddr)
for {
conn, err := listener.Accept()
if err != nil {
continue
}
jsonrpc.ServeConn(conn)
}
}
func checkError(err error) {
if err != nil {
fmt.Println("Fatal error ", err.Error())
os.Exit(1)
}
}
```
### PHP JsonRPC客户端代码
```php
class JsonRPC
{
public $conn;
function __construct($host, $port)
{
$this->conn = fsockopen($host, $port, $errno, $errstr, 3);
if (!$this->conn) {
return false;
}
}
public function Call($method, $params)
{
$obj = new stdClass();
$obj->code = 0;
if (!$this->conn) {
$obj->info = "jsonRPC-socket-tcp连接失败!";
return $obj;
}
$err = fwrite($this->conn, json_encode(array(
'method' => $method,
'params' => array($params),
'id' => 0,
)) . "\n");
if ($err === false) {
fclose($this->conn);
$obj->info = "jsonRPC发送参数失败!socket-tcp资源是否释放";
return $obj;
}
stream_set_timeout($this->conn, 0, 3000);
$line = fgets($this->conn);
fclose($this->conn);
if ($line === false) {
$obj->info = "jsonRPC返回消息为空!请检查自己的rpc-client代码";
return $obj;
}
$temp = json_decode($line);
$obj->code = $temp->error == null ? 1 : 0;
$obj->data = $temp->result;
return $obj;
}
}
function json_rpc_ip_address($ipString)
{
//redis是后来加上的
$redis = redis();
$jsonString = $redis->hGet('ip_db', $ipString);
if (empty($jsonString)) {
$client = new JsonRPC("127.0.0.1", 3344);
$obj = $client->Call("Ip2addr.Address", ['IpString' => $ipString]);
if ($obj->code === 1) {
$jsonString = json_encode($obj);
$redis->hset('ip_db', $ipString, $jsonString);
return $obj;
}
}
return json_decode($jsonString);
}
```
#2