《Packt.Mastering.Go.Web.Services.2015.4.pdf》之Using Gorilla for JSON-RPC

官网
http://www.gorillatoolkit.org/pkg/rpc#subdirs

测试代码

为了更好的说明使用方法,对原文的代码有所修改。

type RPCAPIArguments struct {
    Message string
    Msg     string
}
type RPCAPIResponse struct {
    Message string
    Masg    string
}
type StringService struct{}

func (h *StringService) Length(r *http.Request, arguments *RPCAPIArguments, reply *RPCAPIResponse) error {
    Debug(r.Header)
    Debug(arguments.Message)
    Debug(arguments.Msg)
    reply.Message = "Your string is " + fmt.Sprintf("Your string is%d chars long", utf8.RuneCountInString(arguments.Message)) +
        "characters long"
    reply.Masg = "er"
    return nil
}

func f(i *rpc.RequestInfo) {
    Debug("fucn")
    return
}
func main() {
    fmt.Println("Starting service")
    // s := rpc.NewServer()
    // s.RegisterCodec(json.NewCodec(), "application/json")
    // s.RegisterService(new(StringService), "")
    s := rpc.NewServer()
    s.RegisterCodec(json.NewCodec(), "application/json")
    s.RegisterService(new(StringService), "")
    s.RegisterAfterFunc(f)
    http.Handle("/rpc", s)
    http.ListenAndServe(":10000", nil)
}

测试请求

{
  "method": "StringService.Length",
  "params": [
    {
      "Message": "Testing therservied",
      "Msg": "sbig"
    }
  ],
  "id": "3"
}

响应

{
  "result": {
    "Message": "Your string is Your string is19 chars longcharacters long",
    "Masg": "er"
  },
  "error": null,
  "id": "3"
}

说明:
在POST数据中,method指定要远程执行的方法,StringService.Length对应服务器中的StringService类型的Length方法。通过

s.RegisterService(new(StringService), "")

将StringService注册到RPC。
params指定传递的参数,分别对应StringService的两个成员变量 Message,Msg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
About This Book, Effectively deploy and integrate Go web services with applications in the real worldFamiliarize yourself with RESTful practices and apply them in GoA comprehensive tutorial with lots of tips and tricks to program and develop web services in Go, Who This Book Is For, If you are a web programmer with experience in developing web services and have a rudimentary knowledge of using Go, then this is the book for you. Basic knowledge of Go as well as knowledge of relational databases and non-relational NoSQL datastores is assumed. Some basic concurrency knowledge is also required., What You Will Learn, Familiarize yourself with RESTful practices and apply them in Go Acquaint yourself with the best practices for API design such as URL routing and HTTP response methods Utilize useful middleware and frameworks to augment applications otherwise relying on pure Go Understand the basic and complex authentication models for APIs Deploy your code to staging and development environments Look at ways to make your Go web services as speedy and thrifty as possible Secure your web services Ingest your own API and make it appealing to other developers, In Detail, This book will take you through the most important aspects of designing, building, and deploying a web service utilizing idiomatic REST practices with a focus on speed, security, and flexibility. You will begin by building your first API in Go using the HTTP package. You will look at designing and building your application including popular design structures like Model-View-Controller. You will also understand methods for deploying code to staging and development. Finally, you will see how the security features in Go can be used for protection against SQL injection, and sensitive data compromise., By the end of this book, you will have achieved a high level of proficiency in building and deploying web services and web APIs with Go.
Program and deploy fast, scalable web services and create high-performance RESTful APIs using Go About This Book Effectively deploy and integrate Go web services with applications in the real world Familiarize yourself with RESTful practices and apply them in Go A comprehensive tutorial with lots of tips and tricks to program and develop web services in Go Who This Book Is For If you are a web programmer with experience in developing web services and have a rudimentary knowledge of using Go, then this is the book for you. Basic knowledge of Go as well as knowledge of relational databases and non-relational NoSQL datastores is assumed. Some basic concurrency knowledge is also required. What You Will Learn Familiarize yourself with RESTful practices and apply them in Go Acquaint yourself with the best practices for API design such as URL routing and HTTP response methods Utilize useful middleware and frameworks to augment applications otherwise relying on pure Go Understand the basic and complex authentication models for APIs Deploy your code to staging and development environments Look at ways to make your Go web services as speedy and thrifty as possible Secure your web services Ingest your own API and make it appealing to other developers In Detail This book will take you through the most important aspects of designing, building, and deploying a web service utilizing idiomatic REST practices with a focus on speed, security, and flexibility. You will begin by building your first API in Go using the HTTP package. You will look at designing and building your application including popular design structures like Model-View-Controller. You will also understand methods for deploying code to staging and development. Finally, you will see how the security features in Go can be used for protection against SQL injection, and sensitive data compromise. By the end of this book, you will have achieved a high level of proficiency in building and deploying web services and web APIs with Go. Table of Contents Chapter 1: Our First API in Go Chapter 2: RESTful Services in Go Chapter 3: Routing and Bootstrapping Chapter 4: Designing APIs in Go Chapter 5: Templates and Options in Go Chapter 6: Accessing and Using Web Services in Go Chapter 7: Working with Other Web Technologies Chapter 8: Responsive Go for the Web Chapter 9: Deployment Chapter 10: Maximizing Performance Chapter 11: Security
Mastering Elasticsearch[m]》第二版是由Packt Publishing出版的一本关于掌握Elasticsearch的书籍。Elasticsearch是一个开源的分布式搜索和分析引擎,用于全文搜索、日志分析、实时数据分析等应用场景。该书旨在提供关于Elasticsearch的深入理解和高级应用技巧。 这本书的作者应该是一位经验丰富的Elasticsearch专家,他将自己的知识和实践经验融入其中,帮助读者更好地掌握和应用Elasticsearch。第二版相对于第一版进行了更新和改进,可能增加了新的功能、解决了一些问题,或者引入了新的最佳实践和案例研究。 这本书的内容应该包括了Elasticsearch的核心概念、组件和基本用法的介绍,以及更高级的主题,如集群配置、索引优化、数据建模、搜索分析和可视化等。读者可以通过阅读这本书,了解如何在现实世界的应用中部署、管理和优化Elasticsearch集群。 Packt Publishing是一家知名的技术图书出版商,专注于出版面向IT专业人士的书籍。他们的书籍通常具有良好的质量和内容深度,受到读者和行业内专家的好评。 总的来说,《Mastering Elasticsearch[m]》第二版是一本适合具有一定Elasticsearch基础并希望深入了解和应用该技术的读者的书籍。通过阅读该书,读者可以进一步提升在使用Elasticsearch方面的技能和知识水平,为实际项目提供更好的搜索和分析解决方案。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值