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

临沂市兰山区建设局网站重庆的网站建设公司

临沂市兰山区建设局网站,重庆的网站建设公司,充值中心网站怎么做,wordpress建地方门户转载自 Dubbo 融合 Nacos 成为注册中心 Nacos 作为 Dubbo 生态系统中重要的注册中心实现#xff0c;本文将会介绍如何进行 Dubbo 对接 Nacos 注册中心的工作。 预备工作 请确保后台已经启动 Nacos 服务#xff0c;可先行参考 Nacos 快速入门。 快速上手 Dubbo 融合 Nac…转载自  Dubbo 融合 Nacos 成为注册中心 Nacos 作为 Dubbo 生态系统中重要的注册中心实现本文将会介绍如何进行 Dubbo 对接 Nacos 注册中心的工作。 预备工作 请确保后台已经启动 Nacos 服务可先行参考 Nacos 快速入门。 快速上手 Dubbo 融合 Nacos 成为注册中心的操作步骤非常简单大致步骤可分为“增加 Maven 依赖”以及“配置注册中心“。 增加 Maven 依赖 只需要依赖Dubbo客户端即可关于推荐的使用版本请参考Dubbo官方文档或者咨询Dubbo开发人员 dependencies...!-- Dubbo dependency --dependencygroupIdcom.alibaba/groupIdartifactIddubbo/artifactIdversion[latest version]/version/dependency!-- 使用Spring装配方式时可选: --dependencygroupIdcom.alibaba.spring/groupIdartifactIdspring-context-support/artifactIdversion[latest version]/version/dependency.../dependencies配置注册中心 假设您 Dubbo 应用使用 Spring Framework 装配将有两种配置方法可选分别为Dubbo Spring 外部化配置以及 Spring XML 配置文件以及笔者强烈推荐前者。 Dubbo Spring 外部化配置 Dubbo Spring 外部化配置是由 Dubbo 2.5.8 引入的新特性可通过 Spring Environment 属性自动地生成并绑定 Dubbo 配置 Bean实现配置简化并且降低微服务开发门槛。 假设您 Dubbo 应用的使用 Zookeeper 作为注册中心并且其服务器 IP 地址为10.20.153.10同时该注册地址作为 Dubbo 外部化配置属性存储在 dubbo-config.properties 文件如下所示 ## application dubbo.application.name your-dubbo-application## Zookeeper registry address dubbo.registry.address zookeeper://10.20.153.10:2181 ...假设您的 Nacos Server 同样运行在服务器 10.20.153.10 上并使用默认 Nacos 服务端口 8848您只需将 dubbo.registry.address 属性调整如下 ## 其他属性保持不变## Nacos registry address dubbo.registry.address nacos://10.20.153.10:8848 ##如果要使用自己创建的命名空间可以使用下面2种方式 #dubbo.registry.address nacos://10.20.153.10:8848?namespace5cbb70a5-xxx-xxx-xxx-d43479ae0932 #dubbo.registry.parameters.namespace5cbb70a5-xxx-xxx-xxx-d43479ae0932 ...随后重启您的 Dubbo 应用Dubbo 的服务提供和消费信息在 Nacos 控制台中可以显示 如图所示服务名前缀为 providers: 的信息为服务提供者的元信息consumers: 则代表服务消费者的元信息。点击“详情”可查看服务状态详情 如果您正在使用 Spring XML 配置文件装配 Dubbo 注册中心的话请参考下一节。 Spring XML 配置文件 同样假设您 Dubbo 应用的使用 Zookeeper 作为注册中心并且其服务器 IP 地址为10.20.153.10并且装配 Spring Bean 在 XML 文件中如下所示 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:dubbohttp://dubbo.apache.org/schema/dubboxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd!-- 提供方应用信息用于计算依赖关系 --dubbo:application namedubbo-provider-xml-demo /!-- 使用 Zookeeper 注册中心 --dubbo:registry addresszookeeper://10.20.153.10:2181 /... /beans与 Dubbo Spring 外部化配置 配置类似只需要调整 address 属性配置即可 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:dubbohttp://dubbo.apache.org/schema/dubboxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd!-- 提供方应用信息用于计算依赖关系 --dubbo:application namedubbo-provider-xml-demo /!-- 使用 Nacos 注册中心 --dubbo:registry addressnacos://10.20.153.10:8848 /!-- 如果要使用自己创建的命名空间可以使用下面配置 --!-- dubbo:registry addressnacos://10.20.153.10:8848?namespace5cbb70a5-xxx-xxx-xxx-d43479ae0932 / --... /beans重启 Dubbo 应用后您同样也能发现服务提供方和消费方的注册元信息呈现在 Nacos 控制台中 您是否绝对配置或切换 Nacos 注册中心超级 Easy 呢如果您仍旧意犹未尽或者不甚明白的话可参考以下完整的示例。 完整示例 以上图片中的元数据源于 Dubbo Spring 注解驱动示例以及 Dubbo Spring XML 配置驱动示例下面将分别介绍两者您可以选择自己偏好的编程模型。在正式讨论之前先来介绍两者的预备工作因为它们皆依赖 Java 服务接口和实现。同时请确保本地127.0.0.1环境已启动 Nacos 服务。 示例接口与实现 首先定义示例接口如下所示 package com.alibaba.dubbo.demo.service;/*** DemoService** since 2.6.5*/ public interface DemoService {String sayName(String name);}提供以上接口的实现类 package com.alibaba.dubbo.demo.service;import com.alibaba.dubbo.config.annotation.Service; import com.alibaba.dubbo.rpc.RpcContext;import org.springframework.beans.factory.annotation.Value;/*** Default {link DemoService}** since 2.6.5*/ Service(version ${demo.service.version}) public class DefaultService implements DemoService {Value(${demo.service.name})private String serviceName;public String sayName(String name) {RpcContext rpcContext RpcContext.getContext();return String.format(Service [name :%s , port : %d] %s(\%s\) : Hello,%s,serviceName,rpcContext.getLocalPort(),rpcContext.getMethodName(),name,name);} }接口与实现准备妥当后下面将采用注解驱动和 XML 配置驱动各自实现。 Spring 注解驱动示例 Dubbo 2.5.7 重构了 Spring 注解驱动的编程模型。 服务提供方注解驱动实现 定义 Dubbo 提供方外部化配置属性源 - provider-config.properties ## application dubbo.application.name dubbo-provider-demo## Nacos registry address dubbo.registry.address nacos://127.0.0.1:8848 ##如果要使用自己创建的命名空间可以使用下面2种方式 #dubbo.registry.address nacos://127.0.0.1:8848?namespace5cbb70a5-xxx-xxx-xxx-d43479ae0932 #dubbo.registry.parameters.namespace5cbb70a5-xxx-xxx-xxx-d43479ae0932## Dubbo Protocol dubbo.protocol.name dubbo dubbo.protocol.port -1# Provider Service version demo.service.version1.0.0 demo.service.name demoService实现服务提供方引导类 - DemoServiceProviderBootstrap package com.alibaba.dubbo.demo.provider;import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; import com.alibaba.dubbo.demo.service.DemoService;import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.PropertySource;import java.io.IOException;/*** {link DemoService} provider demo*/ EnableDubbo(scanBasePackages com.alibaba.dubbo.demo.service) PropertySource(value classpath:/provider-config.properties) public class DemoServiceProviderBootstrap {public static void main(String[] args) throws IOException {AnnotationConfigApplicationContext context new AnnotationConfigApplicationContext();context.register(DemoServiceProviderBootstrap.class);context.refresh();System.out.println(DemoService provider is starting...);System.in.read();} } 其中注解 EnableDubbo 激活 Dubbo 注解驱动以及外部化配置其 scanBasePackages 属性扫描指定 Java 包将所有标注 Service 的服务接口实现类暴露为 Spring Bean随即被导出 Dubbo 服务。 PropertySource 是 Spring Framework 3.1 引入的标准导入属性配置资源注解它将为 Dubbo 提供外部化配置。 服务消费方注解驱动实现 定义 Dubbo 消费方外部化配置属性源 - consumer-config.properties ## Dubbo Application info dubbo.application.name dubbo-consumer-demo## Nacos registry address dubbo.registry.address nacos://127.0.0.1:8848 ##如果要使用自己创建的命名空间可以使用下面2种方式 #dubbo.registry.address nacos://127.0.0.1:8848?namespace5cbb70a5-xxx-xxx-xxx-d43479ae0932 #dubbo.registry.parameters.namespace5cbb70a5-xxx-xxx-xxx-d43479ae0932# Reference version demo.service.version 1.0.0同样地dubbo.registry.address 属性指向 Nacos 注册中心其他 Dubbo 服务相关的元信息通过 Nacos 注册中心获取。 实现服务消费方引导类 - DemoServiceConsumerBootstrap package com.alibaba.dubbo.demo.consumer;import com.alibaba.dubbo.config.annotation.Reference; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; import com.alibaba.dubbo.demo.service.DemoService;import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.PropertySource;import javax.annotation.PostConstruct; import java.io.IOException;/*** {link DemoService} consumer demo*/ EnableDubbo PropertySource(value classpath:/consumer-config.properties) public class DemoServiceConsumerBootstrap {Reference(version ${demo.service.version})private DemoService demoService;PostConstructpublic void init() {for (int i 0; i 10; i) {System.out.println(demoService.sayName(小马哥mercyblitz));}}public static void main(String[] args) throws IOException {AnnotationConfigApplicationContext context new AnnotationConfigApplicationContext();context.register(DemoServiceConsumerBootstrap.class);context.refresh();context.close();} } 同样地EnableDubbo 注解激活 Dubbo 注解驱动和外部化配置不过当前属于服务消费者无需指定 Java 包名扫描标注 Service 的服务实现。 Reference 是 Dubbo 远程服务的依赖注入注解需要服务提供方和消费端约定接口interface、版本version以及分组group信息。在当前服务消费示例中DemoService 的服务版本来源于属性配置文件 consumer-config.properties。 PostConstruct 部分代码则说明当 DemoServiceConsumerBootstrap Bean 初始化时执行十次 Dubbo 远程方法调用。 运行注解驱动示例 在本地启动两次 DemoServiceProviderBootstrap注册中心将出现两个健康服务 再运行 DemoServiceConsumerBootstrap运行结果如下 Service [name :demoService , port : 20880] sayName(小马哥mercyblitz) : Hello,小马哥mercyblitz Service [name :demoService , port : 20881] sayName(小马哥mercyblitz) : Hello,小马哥mercyblitz Service [name :demoService , port : 20880] sayName(小马哥mercyblitz) : Hello,小马哥mercyblitz Service [name :demoService , port : 20880] sayName(小马哥mercyblitz) : Hello,小马哥mercyblitz Service [name :demoService , port : 20881] sayName(小马哥mercyblitz) : Hello,小马哥mercyblitz Service [name :demoService , port : 20881] sayName(小马哥mercyblitz) : Hello,小马哥mercyblitz Service [name :demoService , port : 20880] sayName(小马哥mercyblitz) : Hello,小马哥mercyblitz Service [name :demoService , port : 20880] sayName(小马哥mercyblitz) : Hello,小马哥mercyblitz Service [name :demoService , port : 20881] sayName(小马哥mercyblitz) : Hello,小马哥mercyblitz Service [name :demoService , port : 20881] sayName(小马哥mercyblitz) : Hello,小马哥mercyblitz运行无误并且服务消费方使用了负载均衡策略将十次 RPC 调用平均分摊到两个 Dubbo 服务提供方实例中。 Spring XML 配置驱动示例 Spring XML 配置驱动是传统 Spring 装配组件的编程模型。 服务提供方 XML 配置驱动 定义服务提供方 XML 上下文配置文件 - /META-INF/spring/dubbo-provider-context.xml ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:dubbohttp://dubbo.apache.org/schema/dubboxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd!-- 提供方应用信息用于计算依赖关系 --dubbo:application namedubbo-provider-xml-demo/!-- 使用 Nacos 注册中心 --dubbo:registry addressnacos://127.0.0.1:8848/!-- 如果要使用自己创建的命名空间可以使用下面配置 --!-- dubbo:registry addressnacos://127.0.0.1:8848?namespace5cbb70a5-xxx-xxx-xxx-d43479ae0932 / --!-- 用dubbo协议在随机端口暴露服务 --dubbo:protocol namedubbo port-1/!-- 声明需要暴露的服务接口 --dubbo:service interfacecom.alibaba.dubbo.demo.service.DemoService refdemoService version2.0.0/!-- 和本地bean一样实现服务 --bean iddemoService classcom.alibaba.dubbo.demo.service.DefaultService/ /beans实现服务提供方引导类 - DemoServiceProviderXmlBootstrap package com.alibaba.dubbo.demo.provider;import com.alibaba.dubbo.demo.service.DemoService;import org.springframework.context.support.ClassPathXmlApplicationContext;import java.io.IOException;/*** {link DemoService} provider demo XML bootstrap*/ public class DemoServiceProviderXmlBootstrap {public static void main(String[] args) throws IOException {ClassPathXmlApplicationContext context new ClassPathXmlApplicationContext();context.setConfigLocation(/META-INF/spring/dubbo-provider-context.xml);context.refresh();System.out.println(DemoService provider (XML) is starting...);System.in.read();} }服务消费方 XML 配置驱动 定义服务消费方 XML 上下文配置文件 - /META-INF/spring/dubbo-consumer-context.xml ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:dubbohttp://dubbo.apache.org/schema/dubboxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd!-- 提供方应用信息用于计算依赖关系 --dubbo:application namedubbo-consumer-xml-demo/!-- 使用 Nacos 注册中心 --dubbo:registry addressnacos://127.0.0.1:8848/!-- 如果要使用自己创建的命名空间可以使用下面配置 --!-- dubbo:registry addressnacos://127.0.0.1:8848?namespace5cbb70a5-xxx-xxx-xxx-d43479ae0932 / --!-- 引用服务接口 --dubbo:reference iddemoService interfacecom.alibaba.dubbo.demo.service.DemoService version2.0.0//beans实现服务消费方引导类 - DemoServiceConsumerXmlBootstrap package com.alibaba.dubbo.demo.consumer;import com.alibaba.dubbo.demo.service.DemoService;import org.springframework.context.support.ClassPathXmlApplicationContext;import java.io.IOException;/*** {link DemoService} consumer demo XML bootstrap*/ public class DemoServiceConsumerXmlBootstrap {public static void main(String[] args) throws IOException {ClassPathXmlApplicationContext context new ClassPathXmlApplicationContext();context.setConfigLocation(/META-INF/spring/dubbo-consumer-context.xml);context.refresh();System.out.println(DemoService consumer (XML) is starting...);DemoService demoService context.getBean(demoService, DemoService.class);for (int i 0; i 10; i) {System.out.println(demoService.sayName(小马哥mercyblitz));}context.close();} }运行 XML 配置驱动示例 同样地先启动两个 DemoServiceProviderXmlBootstrap 引导类观察 Nacos 注册中心服务提供者变化 XML 配置驱动的服务版本为 2.0.0因此注册服务无误。 再运行服务消费者引导类 DemoServiceConsumerXmlBootstrap观察控制台输出内容 Service [name :null , port : 20882] sayName(小马哥mercyblitz) : Hello,小马哥mercyblitz Service [name :null , port : 20882] sayName(小马哥mercyblitz) : Hello,小马哥mercyblitz Service [name :null , port : 20883] sayName(小马哥mercyblitz) : Hello,小马哥mercyblitz Service [name :null , port : 20882] sayName(小马哥mercyblitz) : Hello,小马哥mercyblitz Service [name :null , port : 20882] sayName(小马哥mercyblitz) : Hello,小马哥mercyblitz Service [name :null , port : 20883] sayName(小马哥mercyblitz) : Hello,小马哥mercyblitz Service [name :null , port : 20882] sayName(小马哥mercyblitz) : Hello,小马哥mercyblitz Service [name :null , port : 20883] sayName(小马哥mercyblitz) : Hello,小马哥mercyblitz Service [name :null , port : 20883] sayName(小马哥mercyblitz) : Hello,小马哥mercyblitz Service [name :null , port : 20883] sayName(小马哥mercyblitz) : Hello,小马哥mercyblitz结果同样运行和负载均衡正常不过由于当前示例尚未添加属性 demo.service.name 的缘故因此“name”部分信息输出为 null。 如果您关注或喜爱 Dubbo 以及 Nacos 等开源工程不妨为它们点 “star”加油打气链接 Apache Dubbohttps://github.com/apache/dubboDubbo Nacos Registryhttps://github.com/apache/dubbo/tree/master/dubbo-registry/dubbo-registry-nacosAlibaba Nacoshttps://github.com/alibaba/nacos
http://www.yutouwan.com/news/466066/

相关文章:

  • 导航站 wordpressyxcms wordpress
  • 柳州专业网站优化wordpress图文模板
  • 大型网站建设视频经开区网站建设
  • 绵阳做最好优化网站的请人做网站收费多少
  • 18+网站推广青岛苍南网站建设
  • 网站建设发展历程php wordpress 备份
  • 国内电子商务网站有哪些大连商城网站建设
  • 网站建设邀标书黑龙江建筑职业技术学院招生网站
  • wordpress 如何 删除授权企业做网站乐云seo快速上线
  • 在门户网站中营销者通过制作什么wordpress 新建文章类
  • 做商品网站的教学视频教程wordpress 调用page
  • 网站开发费用摊销时间柳州网站建设优化推广
  • 五金加工东莞网站建设山东丽天建设集团网站
  • 网站做app要权限南昌市网络开发公司
  • 网站改版html静态网站 站内搜索
  • 武清网站建设公司商场设计调研
  • 十堰网站免费建设网上手机网站建设计划书
  • 网站除了wordpress外建一个网站需要哪些东西
  • 最新流行网站开发技术商丘猎狐网络科技有限公司
  • 美文分享网站源码wordpress_ joomla_ drupal
  • 河南省住建厅网站官网网站域名区别吗
  • 书签图片东莞seo公司首选3火星
  • 阿里云服务器 网站模板网站建设联系我们
  • 外贸soho虚拟公司做网站wordpress sweep
  • 建一个网站多少钱WordPress工作发布
  • 网站群建设管理办法平顶山做网站推广
  • 网站推广多少钱专业网站建设软件开发
  • 鸭梨网站建设企业网站建设进什么科目核算
  • 安徽网新科技怎么建设网站校园电商平台网站建设
  • 国内餐饮类网站欣赏水果行业为什么做网站