zoukankan      html  css  js  c++  java
  • 3.(服务注册)快速把服务注册到Consul中

    安装go-plugins

    go get -v github.com/micro/go-plugins,原来go-micro consul的支持已经迁移到了go-plugins里面

    package main
    
    import (
        "github.com/gin-gonic/gin"
        "github.com/micro/go-micro/registry"
        "github.com/micro/go-micro/web"
        "github.com/micro/go-plugins/registry/consul"
    )
    
    func main() {
        consulReg := consul.NewRegistry( //新建一个consul注册的地址,也就是我们consul服务启动的机器ip+端口
            registry.Addrs("localhost:8500"),
        )
        ginRouter := gin.Default()
        ginRouter.Handle("GET", "/user", func(context *gin.Context) {
            context.String(200, "user api")
        })
        ginRouter.Handle("GET", "/news", func(context *gin.Context) {
            context.String(200, "news api")
        })
        server := web.NewService( //go-micro很灵性的实现了注册和反注册,我们启动后直接ctrl+c退出这个server,它会自动帮我们实现反注册
            web.Name("prodservice"), //注册进consul服务中的service名字
            web.Address(":8001"), //注册进consul服务中的端口
            web.Handler(ginRouter), //web.Handler()返回一个Option,我们直接把ginRouter穿进去,就可以和gin完美的结合
            web.Registry(consulReg),//注册到哪个服务器伤的consul中
        )
        server.Run()
    }
    




  • 相关阅读:
    web项目获取spring的applicationContext方式一
    idea关于tomcat中文乱码问题
    java面试题02
    JDBC连接步骤(三)
    JDBCAPI简介(二)
    JDBC简介(一)
    Maven(三)archetype
    如何javaSwing关闭子窗口不关闭父窗口
    Java中Object转化为int类型
    java double转int
  • 原文地址:https://www.cnblogs.com/hualou/p/12097128.html
Copyright © 2011-2022 走看看