网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

如何在Spring与SpringBoot项目中使用Dubbo

这篇文章给大家介绍如何在Spring与Spring Boot项目中使用Dubbo,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

公司主营业务:成都网站设计、做网站、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联公司推出吉安免费做网站回馈大家。

一、在Spring中使用Dubbo

1、Maven依赖


  com.alibaba
  dubbo
  2.5.3.6
  
    
      log4j
      log4j
    
    
      commons-logging
      commons-logging
    
    
      org.springframework
      spring
    
    
      com.alibaba
      fastjson
    
  


  com.github.sgroschupf
  zkclient
  0.1

2、DUBBO生产者注册到zookeeper的xml配置方式



 
 
 
 
 
 
 
 
 
 
 

3、DUBBO消费者注册到zookeeper的xml配置方式



 
 
 
 
 
 
 

二、在Spring Boot中使用Dubbo

在Spring Boot中使用Dubbo,不需要使用xml的方式来配置生产者和消费者,需要使用@Bean注解的方式来进行配置。

1、Maven依赖


  org.springframework.boot
  spring-boot-starter-web
  1.2.5.RELEASE


  org.springframework.boot
  spring-boot-starter
  1.2.5.RELEASE


  com.alibaba
  dubbo
  2.5.3.6
  
    
      log4j
      log4j
    
    
      commons-logging
      commons-logging
    
    
      org.springframework
      spring
    
    
      com.alibaba
      fastjson
    
  


  com.github.sgroschupf
  zkclient
  0.1

2、Dubbo基础配置

public class DubboBaseConfig {
  @Bean
  public RegistryConfig registry() {
    RegistryConfig registryConfig = new RegistryConfig();
    registryConfig.setAddress("127.0.0.1:2181");
    registryConfig.setProtocol("zookeeper");
    return registryConfig;
  }
  @Bean
  public ApplicationConfig application() {
    ApplicationConfig applicationConfig = new ApplicationConfig();
    applicationConfig.setName("testApp");
    return applicationConfig;
  }
  @Bean
  public MonitorConfig monitorConfig() {
    MonitorConfig mc = new MonitorConfig();
    mc.setProtocol("registry");
    return mc;
  }
  @Bean
  public ReferenceConfig referenceConfig() {
    ReferenceConfig rc = new ReferenceConfig();
    rc.setMonitor(monitorConfig());
    return rc;
  }
  @Bean
  public ProtocolConfig protocol() {
    ProtocolConfig protocolConfig = new ProtocolConfig();
    protocolConfig.setPort(20880);
    return protocolConfig;
  }
  @Bean
  public ProviderConfig provider() {
    ProviderConfig providerConfig = new ProviderConfig();
    providerConfig.setMonitor(monitorConfig());
    return providerConfig;
  }
}

3、Dubbo生产者配置,需要继承Dubbo基础配置

@Configuration
public class ExportServiceConfig extends DubboBaseConfig {
  @Bean
  public ServiceBean personServiceExport(Person person) {
    ServiceBean serviceBean = new ServiceBean();
    serviceBean.setProxy("javassist");
    serviceBean.setVersion("myversion");
    serviceBean.setInterface(Person.class.getName());
    serviceBean.setRef(person);
    serviceBean.setTimeout(5000);
    serviceBean.setRetries(3);
    return serviceBean;
  }
}

4、Dubbo消费者配置,需要继承Dubbo基础配置

@Configuration
public class ReferenceConfig extends DubboBaseConfig {
  @Bean
  public ReferenceBean person() {
    ReferenceBean ref = new ReferenceBean<>();
    ref.setVersion("myversion");
    ref.setInterface(Person.class);
    ref.setTimeout(5000);
    ref.setRetries(3);
    ref.setCheck(false);
    return ref;
  }
}

关于如何在Spring与Spring Boot项目中使用Dubbo就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


网站标题:如何在Spring与SpringBoot项目中使用Dubbo
网址分享:http://bjjierui.cn/article/jhpjcp.html

其他资讯