springboot整合dubbo(4)-消费者
1.导入依赖
父工程
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.4.RELEASE</version> <relativePath/> </parent> <groupId>com.xt</groupId> <artifactId>springboot-dubbo-demo</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <name>springboot-dubbo-demo</name> <url>http://maven.apache.org</url> <modules> <module>springboot-dubbo-provider</module> <module>springboo-dubbo-consumer</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <dubbo.version>2.7.3</dubbo.version> </properties> <dependencyManagement> <dependencies> <!--dubbo依赖--> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo.version}</version> </dependency> <!--zookeeper注册中心客户端引入 curator客户端--> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-zookeeper</artifactId> <version>${dubbo.version}</version> <type>pom</type> </dependency> </dependencies> </dependencyManagement> </project>
消费者
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.xt</groupId> <artifactId>springboot-dubbo-demo</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>springboo-dubbo-consumer</artifactId> <packaging>jar</packaging> <name>springboo-dubbo-consumer</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>com.xt</groupId> <artifactId>springboot-dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-zookeeper</artifactId> </dependency> </dependencies> </project>
2.application.properties配置
#tomcat端口号 server.port=8081 spring.datasource.url=jdbc:mysql://localhost:3306/study2020 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.username=root spring.datasource.password=root mybatis.configuration.map-underscore-to-camel-case=true spring.resources.static-locations:classpath:/static/,classpath:/templates/ #mybatis 配置 # 配置映射文件加载 mybatis.mapper-locations=classpath*:mappers/*.xml # 实体类通过别名使用 #mybatis.type-aliases-package=com.example.springboot.mybatis.entity spring.devtools.restart.enabled=true #dubbo相关配置 #服务名称 dubbo.application.name=springboot-meeting-service #注册中心地址 dubbo.registry.address=zookeeper://127.0.0.1:2181 |
application.yml配置
server:
port: 8002
dubbo:
application:
name: dubbo-consumer
##配置注册中心
registry:
address: zookeeper://127.0.0.1:2181
##注册中心采用的协议 端口号
protocol:
name: dubbo
port: 20880
3.实现类上添加@Reference注解(注册中心中引用服务),相当于consumer.xml中的配置:
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="userService" interface="com.atxuetao.service.UserService" />
package com.xt.service.impl; import com.alibaba.dubbo.config.annotation.Reference; import com.xt.mapper.MeetingPubMapper; import com.xt.pojo.MeetingPub; import com.xt.pojo.User; import com.xt.service.MeetingPubService; import com.xt.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class MeetingPubServiceImpl implements MeetingPubService { @Autowired MeetingPubMapper meetingPubMapper; @Reference UserService userService; @Override public MeetingPub selectByPrimaryKey(Integer pCode) { MeetingPub meetingPub = meetingPubMapper.selectByPrimaryKey(pCode); //调用服务完成服务消费功能 User user = userService.selectByPrimaryKey(meetingPub.getUid()); meetingPub.setUser(user); return meetingPub; } } |
4.在启动类上添加@EnableDubbo注解
package com.xt;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.xt.mapper")
@EnableDubbo
public class SpringbootMeetingServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMeetingServiceApplication.class, args);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律