boot整合dubbo
Posted IT技术大神
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了boot整合dubbo相关的知识,希望对你有一定的参考价值。
服务发布者
application.properties
server.port:9001
spring.dubbo.application.name=provider //服务名称
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880 //暴露的dubbo端口
spring.dubbo.scan=com.example.e3mall.management.service //扫描的注解包
spring.datasource.url=jdbc:mysql://localhost:3306/e3mall?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
pom文件:引入dubbo坐标
<dependencies>
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
在service接口实现类上引入dubbo的注解,并添加版本号就可以了
消费者
pom文件:(重点是dubbo包)
<dependencies>
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.example.e3mall</groupId>
<artifactId>management</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
application.properties
注意:dubbo扫描要配置在mvc前面,否则会出现空指针
spring.dubbo.application.name=consumer //注册应用的名字
spring.dubbo.scan=com.example.management_web.controller //dubbo注解扫描包
server.port=9002
spring.mvc.view.suffix=".html"
spring.mvc.static-path-pattern=/**
spring.mvc.servlet.load-on-startup=1
spring.datasource.url=jdbc:mysql://localhost:3306/e3mall?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
控制器引用dubbo注解@Reference:
通过访问消费者localhost:9002/aaa 可以看到调用测试结果:
进入到dubbo-admin可以看到服务提供者和消费者存在
IT技术大神 ∣小白到大神的进阶之路
以上是关于boot整合dubbo的主要内容,如果未能解决你的问题,请参考以下文章
Dubbo Spring Boot 最佳实践整合 Demo 征集