开发者社区 > 云原生 > 正文

在Dubbo/Dubbo-go中, 支持metadata local模式么,有没有example

dubbo go 支持metadata local模式么,有没有example

展开
收起
崔问问 2023-11-29 16:54:18 49 0
2 条回答
写回答
取消 提交回答
  • dubbo java框架默认是local, mosn 用到了dubbogo库。https://github.com/apache/dubbo-go/blob/53843b7495f4f2c66be9d6992b980e0b65662a18/metadata/service/local/service.
    go
    local 的实现是这个。此回答整理自钉钉群:dubbogo社区1

    2023-11-29 23:10:07
    赞同 展开评论 打赏
  • 面对过去,不要迷离;面对未来,不必彷徨;活在今天,你只要把自己完全展示给别人看。

    Dubbo 和 Dubbo-go 都支持元数据本地模式(Local Metadata),这是一种简化配置的方式,允许直接在服务提供者和消费者的应用程序中定义元数据,而不是依赖于外部的元数据中心。

    对于 Dubbo:

    <!-- 在服务提供者的配置中 -->
    <dubbo:service interface="com.example.MyService" ref="myServiceBean" version="1.0.0">
        <dubbo:metadata type="local"/>
    </dubbo:service>
    
    <!-- 在服务消费者的配置中 -->
    <dubbo:reference id="myService" interface="com.example.MyService" version="1.0.0">
        <dubbo:metadata type="local"/>
    </dubbo:reference>
    

    对于 Dubbo-go,你可以在代码中设置元数据模式为本地:

    // 在服务提供者的代码中
    providerConfig := common.NewProviderConfig()
    providerConfig.MetadataType = "local"
    
    // 创建并注册服务
    serviceConfig := common.NewServiceConfig()
    serviceConfig.SetInterface("com.example.MyService")
    serviceConfig.SetVersion("1.0.0")
    
    registryConfig := common.NewRegistryConfig()
    registryConfig.SetProtocol("zookeeper")
    registryConfig.SetAddress("127.0.0.1:2181")
    
    providerConfig.SetRegistry(registryConfig)
    providerConfig.Services = append(providerConfig.Services, serviceConfig)
    
    provider := registry.NewProvider(&providerConfig)
    err := provider.Export()
    if err != nil {
        log.Fatal(err)
    }
    
    // 在服务消费者的代码中
    consumerConfig := common.NewConsumerConfig()
    consumerConfig.MetadataType = "local"
    
    // 创建并引用服务
    referenceConfig := common.NewReferenceConfig()
    referenceConfig.SetInterface("com.example.MyService")
    referenceConfig.SetVersion("1.0.0")
    
    registryConfig := common.NewRegistryConfig()
    registryConfig.SetProtocol("zookeeper")
    registryConfig.SetAddress("127.0.0.1:2181")
    
    consumerConfig.SetRegistry(registryConfig)
    consumerConfig.References = append(consumerConfig.References, referenceConfig)
    
    consumer := registry.NewConsumer(&consumerConfig)
    service, err := consumer.Reference(referenceConfig)
    if err != nil {
        log.Fatal(err)
    }
    defer consumer.Unreference(service)
    
    2023-11-29 22:55:40
    赞同 展开评论 打赏

阿里云拥有国内全面的云原生产品技术以及大规模的云原生应用实践,通过全面容器化、核心技术互联网化、应用 Serverless 化三大范式,助力制造业企业高效上云,实现系统稳定、应用敏捷智能。拥抱云原生,让创新无处不在。

相关电子书

更多
Go语言路上踩过的坑 立即下载
gohbase :HBase go客户端 立即下载
Go构建日请求千亿级微服务实践 立即下载