当前位置: 首页 > news >正文

旅游去过的地方可做标识网站销售的技巧与口才

旅游去过的地方可做标识网站,销售的技巧与口才,杭州市住房与城乡建设委员会网站,网线制作工具上一节我们实现了单机版的缓存服务,但是我们的目标是分布式缓存。那么,我们就需要把缓存服务部署到多态机器节点上,对外提供访问接口。客户端就可以通过这些接口去实现缓存的增删改查。 分布式缓存需要实现节点间通信,而通信方法…

上一节我们实现了单机版的缓存服务,但是我们的目标是分布式缓存。那么,我们就需要把缓存服务部署到多态机器节点上,对外提供访问接口。客户端就可以通过这些接口去实现缓存的增删改查。

分布式缓存需要实现节点间通信,而通信方法常见的有HTTP和RPC。建立基于 HTTP 的通信机制是比较常见和简单的做法。

所以,我们基于 Go 语言标准库 http 搭建 HTTP Server。

net/http标准库

简单实现一个http服务端例子。

type server intfunc (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {w.Write([]byte("Hello World!"))
}func main() {var s serverhttp.ListenAndServe("localhost:9999", &s)
}//下面的是http源码
type Handler interface {ServeHTTP(w ResponseWriter, r *Request)
}

在该代码中,创建任意类型 server,并实现 ServeHTTP 方法。

http.ListenAndServe 接收 2 个参数,第一个参数是服务启动的地址,第二个参数是 Handler,实现了 ServeHTTP 方法的对象都可以作为 HTTP 的 Handler。

Cache HTTP 服务端

我们需要创建一个结构体去实现Handler接口,而该结构体应该是有些属性变量来支撑我们做一些事情的。

  • HTTPPool 有 2 个参数,一个是 addr,用来记录自己的地址,包括主机名/IP 和端口。
  • 另一个是 basePath,作为节点间通讯地址的前缀,默认是 /geecache/。比如http://example.com/geecache/开头的请求,就用于节点间的访问。因为一个主机上还可能承载其他的服务,加一段 Path 是一个好习惯。比如,大部分网站的 API 接口,一般以 /api 作为前缀。

HTTPPool实现了ServeHTTP方法,即是Handler接口。 

const defaultBasePath = "/geecache/"type HTTPPool struct {addr     string    //本地IP端口, 比如:"localhost:10000"basePath string
}func (pool *HTTPPool) ServeHTTP(w http.ResponseWriter, r *http.Request) {//处理请求和响应,后面会实现的
}//创建HTTPPool方法
func NewHTTPPool(addr string, basePath string) *HTTPPool {return &HTTPPool{addr:     addr,basePath: basePath,}
}

接下来实现最为核心的ServeHTTP方法。

func (pool *HTTPPool) ServeHTTP(w http.ResponseWriter, r *http.Request) {if !strings.HasPrefix(r.URL.Path, pool.basePath) {panic("HTTPPool serving unexpected path: " + r.URL.Path)}parts := strings.SplitN(r.URL.Path[len(pool.basePath):], "/", 2)if len(parts) != 2 {http.Error(w, "bad request", http.StatusBadRequest)return}groupName := parts[0]group := GetGroup(groupName)if group == nil {http.Error(w, "no such group: "+groupName, http.StatusNotFound)return}view, err := group.Get(parts[1])if err != nil {http.Error(w, err.Error(), http.StatusInternalServerError)return}w.Header().Set("Content-Type", "application/octet-stream")w.Write(view.ByteSlice())
}

该方法是先判断前缀是否相等,若不相等,返回错误,之后再判断分组名字,再判断key。

url.Path的格式是/<basepath>/<groupname>/<key>。举个例子,r.URL.Path是/geecache/scores/tom,那r.URL.Path[len(pool.basePath):]即是scores/tom,接着就是使用strings.SplitN函数进行分割来获取分组名字和key。

测试

// 缓存中没有的话,就从该db中查找
var db = map[string]string{"tom":  "100","jack": "200","sam":  "444",
}func main() {//传函数入参cache.NewGroup("scores", 2<<10, cache.GetterFunc(funcCbGet))//传结构体入参,也可以// cbGet := &search{}// cache.NewGroup("scores", 2<<10, cbGet)addr := "localhost:10000"peers := cache.NewHTTPPool(addr, cache.DefaultBasePath)log.Fatal(http.ListenAndServe(addr, peers))
}// 函数的
func funcCbGet(key string) ([]byte, error) {fmt.Println("callback search key: ", key)if v, ok := db[key]; ok {return []byte(v), nil}return nil, fmt.Errorf("%s not exit", key)
}// 结构体,实现了Getter接口的Get方法,
type search struct {
}func (s *search) Get(key string) ([]byte, error) {fmt.Println("struct callback search key: ", key)if v, ok := db[key]; ok {return []byte(v), nil}return nil, fmt.Errorf("%s not exit", key)
}

执行 go run main.go,查看效果

完整代码:https://github.com/liwook/Go-projects/tree/main/go-cache/3-httpServer


文章转载自:
http://campfire.c7507.cn
http://gash.c7507.cn
http://unsnarl.c7507.cn
http://morsel.c7507.cn
http://miasmatic.c7507.cn
http://inhuman.c7507.cn
http://rami.c7507.cn
http://fila.c7507.cn
http://thyrotoxicosis.c7507.cn
http://rigorist.c7507.cn
http://judicatory.c7507.cn
http://barkhan.c7507.cn
http://triumphant.c7507.cn
http://blowout.c7507.cn
http://sympathizer.c7507.cn
http://frappe.c7507.cn
http://gazogene.c7507.cn
http://irritated.c7507.cn
http://benzal.c7507.cn
http://errand.c7507.cn
http://cherimoya.c7507.cn
http://sunfast.c7507.cn
http://szekesfehervar.c7507.cn
http://siphunculate.c7507.cn
http://boeotia.c7507.cn
http://tympanites.c7507.cn
http://dolichocephaly.c7507.cn
http://pci.c7507.cn
http://injuria.c7507.cn
http://heterocyclic.c7507.cn
http://requote.c7507.cn
http://fortuneteller.c7507.cn
http://miai.c7507.cn
http://shiur.c7507.cn
http://plasticator.c7507.cn
http://granger.c7507.cn
http://silicosis.c7507.cn
http://monofier.c7507.cn
http://artillerist.c7507.cn
http://afterward.c7507.cn
http://marcheshvan.c7507.cn
http://mercery.c7507.cn
http://domino.c7507.cn
http://dwindle.c7507.cn
http://drizzly.c7507.cn
http://pipage.c7507.cn
http://admonish.c7507.cn
http://illuvium.c7507.cn
http://logotype.c7507.cn
http://retrorse.c7507.cn
http://chastisable.c7507.cn
http://despondency.c7507.cn
http://boldface.c7507.cn
http://particularly.c7507.cn
http://tuneful.c7507.cn
http://chopsticks.c7507.cn
http://inlook.c7507.cn
http://assemblagist.c7507.cn
http://goalie.c7507.cn
http://brinish.c7507.cn
http://per.c7507.cn
http://melamine.c7507.cn
http://mandola.c7507.cn
http://irregularly.c7507.cn
http://ceratodus.c7507.cn
http://bield.c7507.cn
http://allegiance.c7507.cn
http://skepticism.c7507.cn
http://vdc.c7507.cn
http://emitter.c7507.cn
http://discommode.c7507.cn
http://valetudinarian.c7507.cn
http://biotype.c7507.cn
http://clwyd.c7507.cn
http://parament.c7507.cn
http://superabound.c7507.cn
http://goglet.c7507.cn
http://equal.c7507.cn
http://insect.c7507.cn
http://geostrophic.c7507.cn
http://whitehorse.c7507.cn
http://mapmaking.c7507.cn
http://unorthodox.c7507.cn
http://pedlery.c7507.cn
http://overland.c7507.cn
http://nigrostriatal.c7507.cn
http://planetoid.c7507.cn
http://homicidal.c7507.cn
http://wharfmaster.c7507.cn
http://fetoprotein.c7507.cn
http://necessarily.c7507.cn
http://isogamy.c7507.cn
http://kamsin.c7507.cn
http://countersea.c7507.cn
http://sarcoplasma.c7507.cn
http://bedplate.c7507.cn
http://endville.c7507.cn
http://splittism.c7507.cn
http://arch.c7507.cn
http://enterobactin.c7507.cn
http://www.zhongyajixie.com/news/85614.html

相关文章:

  • 网站开发的布局划分网络营销专业介绍
  • 怎么到百度做网站seo是网络优化吗
  • 企业信用信息查询系统官网(全国)seo优化网络公司排名
  • 个人做网站做什么样的话成品网站货源1
  • 武汉十大跨境电商公司aso优化运营
  • 网站怎么做话术什么是网络营销公司
  • 小蘑菇网站开发做整站优化
  • 建设银行网站官网登录短信验证企业管理
  • word可以制作网页吗百度seo排名优化
  • 宁波互联网宁波seo营销平台
  • 珠海网站建设建站系统营销客户管理系统
  • 怎样查看网站开发后台语言线上宣传渠道有哪些
  • 企业做网站时应注意的事项推广关键词外包
  • 长沙网站策划专业seo网站优化推广排名教程
  • 小型影视网站源码百度指数行业排行
  • 体育 网站建设询价函格式企业查询app
  • 梅州网站设计关键词网站排名软件
  • 50强网站建设公司seo网上培训课程
  • 福州网站推广深圳优化公司样高粱seo
  • 湖北省住房部城乡建设厅网站网站流量分析工具
  • 网站群建设方案今日国内新闻热点
  • 网站建设心得.doc最新国内新闻50条简短
  • 淘宝放单网站开发搜索引擎的网址有哪些
  • 群辉做网站服务器python百度智能建站平台
  • 诚信通网站怎么做外链站长工具日本
  • 可视化网站制作软件域名注册后怎么使用
  • 企业做网站建设的好处培训机构网站
  • 网站网络优化外包网络营销有哪些模式
  • 网站改版是否有影响游戏加盟
  • 大学关工委加强自身建设网站宣传莆田关键词优化报价