创建 http 服务器
- package main
- import (
- "net/http"
- "github.com/hprose/hprose-golang/rpc"
- )
- func hello(name string) string {
- return "Hello " + name + "!"
- }
- func main() {
- service := rpc.NewHTTPService()
- service.AddFunction("hello", hello)
- http.ListenAndServe(":8080", service)
- }
创建 fasthttp 服务器
- package main
- import (
- rpc "github.com/hprose/hprose-golang/rpc/fasthttp"
- "github.com/valyala/fasthttp"
- )
- func hello(name string) string {
- return "Hello " + name + "!"
- }
- func main() {
- service := rpc.NewFastHTTPService()
- service.AddFunction("hello", hello)
- fasthttp.ListenAndServe(":8080", service.ServeFastHTTP)
- }
对比上面两个服务,我们发现服务函数(方法)的编写和发布上是没有区别的,区别只在于创建服务器所使用的构造函数和启动服务器所使用的库。
hello
import
创建 TCP 服务器
- package main
- import (
- "github.com/hprose/hprose-golang/rpc"
- )
- func hello(name string) string {
- return "Hello " + name + "!"
- }
- func main() {
- server := rpc.NewTCPServer("tcp4://0.0.0.0:4321/")
- server.AddFunction("hello", hello)
- server.Start()
- }
TCPServerTCPServiceTCPService
- service := rpc.NewTCPService()
- service.AddFunction("hello", hello)
- addr, _ := net.ResolveTCPAddr("tcp", ":4321")
- listener, _ := net.ListenTCP("tcp", addr)
- service.ServeTCP(listener)
serviceservicelistener
创建 Unix Socket 服务器
- package main
- import (
- "github.com/hprose/hprose-golang/rpc"
- )
- func hello(name string) string {
- return "Hello " + name + "!"
- }
- func main() {
- server := rpc.NewUnixServer("unix:/tmp/my.sock")
- server.AddFunction("hello", hello)
- server.Start()
- }
UnixServiceTCPService
创建 WebSocket 服务器
- package main
- import (
- "net/http"
- rpc "github.com/hprose/hprose-golang/rpc/websocket"
- )
- func hello(name string) string {
- return "Hello " + name + "!"
- }
- func main() {
- service := rpc.NewWebSocketService()
- service.AddFunction("hello", hello)
- http.ListenAndServe(":8080", service)
- }
这个跟 HTTP 服务器的创建很类似,而且这个 WebSocket 的 hprose 服务器,同时也是一个 HTTP 的 hprose 服务器,它可以同时接受 HTTP 和 WebSocket 的 hprose 客户端请求。
import
跟 gin 框架结合
HTTP\HTTPS 和 WebSocket 服务并没有单独实现 Server,而只是实现了 Service,所以,这些服务器的启动与关闭依赖于你所使用的 http 库或框架。上面在创建服务器一节中,我们已经介绍了使用 net/http 和 fasthttp 如何来创建服务器。下面我们再举一个在 gin 框架下面如何发布服务的例子:
- package main
- import (
- "github.com/hprose/hprose-golang/rpc"
- "gopkg.in/gin-gonic/gin.v1"
- )
- func hello(name string) string {
- return "Hello " + name + "!"
- }
- func main() {
- service := rpc.NewHTTPService()
- service.AddFunction("hello", hello)
- router := gin.Default()
- router.Any("/path", func(c *gin.Context) {
- service.ServeHTTP(c.Writer, c.Request)
- })
- router.Run(":8080")
- }
"/path"
rpc.NewHTTPService()rpc.NewWebSocketService()
跟 echo 框架结合
- package main
- import (
- "github.com/hprose/hprose-golang/rpc"
- "github.com/labstack/echo"
- )
- func hello(name string) string {
- return "Hello " + name + "!"
- }
- func main() {
- service := rpc.NewHTTPService()
- service.AddFunction("hello", hello)
- e := echo.New()
- e.Any("/path", echo.WrapHandler(service))
- e.Start(":8080")
- }
"/path"
rpc.NewHTTPService()rpc.NewWebSocketService()
跟 beego 框架结合
- package main
- import (
- "github.com/astaxie/beego"
- "github.com/hprose/hprose-golang/rpc"
- )
- func hello(name string) string {
- return "Hello " + name + "!"
- }
- func main() {
- service := rpc.NewHTTPService()
- service.AddFunction("hello", hello)
- beego.Handler("/path", service)
- beego.Run()
- }
发布 WebSocket 服务的方式,跟 gin 和 echo 一样。
跟 iris 框架结合
- package main
- import (
- rpc "github.com/hprose/hprose-golang/rpc/fasthttp"
- "github.com/kataras/iris"
- )
- func hello(name string) string {
- return "Hello " + name + "!"
- }
- func main() {
- service := rpc.NewFastHTTPService()
- service.AddFunction("hello", hello)
- iris.Any("/path", func(c *iris.Context) {
- service.ServeFastHTTP(c.RequestCtx)
- })
- iris.Listen(":8080")
- }
FastHTTPService
Socket 服务器的启动与关闭
TCPServerUnixServer
Start 和 Handle 方法区别
TCPServerUnixServerStartHandle
HandleHandle
StartStart
syscall.SIGHUP
Stop 和 Close 方法的区别
StopStartCloseHandleStopsyscall.SIGQUITStartHandleStopCloseHandle
Restart 方法
RestartStartHandle