springboot2.0整合dubbo的示例代码

写在前面:

使用springboot作为web框架,方便开发许多,做分布式开发,dubbo又不可少,那么怎么整合在一起呢,

跟我学一遍,至少会用

注意,springboot2.0和springboot1.x与dubbo整合不一样,

1.环境

1.新建一个空的maven项目,作为父工程,新建moudle,,service(接口层,及实现层,没有具体分,),web(web层,springboot项目)

项目结构如下

springboot2.0整合dubbo的示例代码_第1张图片

父pom如下


    UTF-8
    1.8
    1.8
  

  
    


      
        
        org.springframework.boot
        spring-boot-dependencies
        2.0.3.RELEASE
        pom
        import
      
      
      
      
      
      
      

      
        com.alibaba.boot
        dubbo-spring-boot-starter
        0.2.0
      

      
      
      
        com.github.sgroschupf
        zkclient
        0.1
      
    
  

web层pom

 
    
      com.itzmn
      dubbo-service
      1.0-SNAPSHOT
    
    
      org.springframework.boot
      spring-boot-starter-web
    
    
      com.alibaba.boot
      dubbo-spring-boot-starter
    

    
    
    
      com.github.sgroschupf
      zkclient
    
    
      org.springframework.boot
      spring-boot-starter-test
      test
    
  

service层

 
    
      org.springframework.boot
      spring-boot-starter-web
    
    
      com.alibaba.boot
      dubbo-spring-boot-starter
    

    
    
    
      com.github.sgroschupf
      zkclient
    
  

2.接口设计

springboot2.0整合dubbo的示例代码_第2张图片

在service模块新建接口,

springboot2.0整合dubbo的示例代码_第3张图片

接口实现类的注解,service一定是dubbo的注解

3.配置文件

# Spring boot application
spring.application.name = /springboot-dubbo
server.port = 9099
management.port = 9091

# Service version
demo.service.version = 1.0.0

# Base packages to scan Dubbo Components (e.g @Service , @Reference)
dubbo.scan.basePackages = com.itzmn.dubbo.service.impl

# Dubbo Config properties
## ApplicationConfig Bean
dubbo.application.id = springboot-dubbo
dubbo.application.name = springboot-dubbo

## ProtocolConfig Bean
dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 20880

## RegistryConfig Bean
dubbo.registry.id = my-registry1
dubbo.registry.address = zookeeper://47.106.64.158:2181

在web层的配置文件中,配置,即可,前提,要先安装zookeeper,才能进行服务的注册,然后启动即可

4.消费者

配置

# Spring boot application
spring.application.name = dubbo-consumer-demo
server.port = 8080
management.port = 8081

# Service Version
demo.service.version = 1.0.0

# Dubbo Config properties
## ApplicationConfig Bean
dubbo.application.id = dubbo-consumer-demo
dubbo.application.name = dubbo-consumer-demo

## ProtocolConfig Bean
dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 12345

只需将服务提供者的接口jar包引入,然后注入服务即可

注意,springboot2.0和springboot1.x与dubbo整合不一样,

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(springboot2.0整合dubbo的示例代码)