Loading

springBoot整合dubbo

基本步骤

  1. 开发dubbo接口服务,创建服务接口模块。
  2. 开发dubbo服务提供者。创建提供者模块。
    1)加入dubbo集成springBoot的起步依赖,zookeeper注册中心客户端依赖,(mybatis和mysql依赖,springBoot集成redis依赖)dubbo服务接口模块依赖。
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.spring.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>
        <dependency>
            <groupId>com.hzc</groupId>
            <artifactId>springBoot_interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

2)在springBoot核心配置文件中配置dubbo

# 应用名称
spring.application.name=springBoot_provider
# 应用服务 WEB 访问端口
server.port=8080

spring.dubbo.server=true
spring.dubbo.registry=zookeeper://localhost:2181


spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456

spring.redis.host=127.0.0.1
spring.redis.port=6379


3)编写dubbo接口实现类,并在实现类上加入@Service注解暴露接口服务。
@com.alibaba.dubbo.config.annotation.Service(interfaceName = "com.hzc.service.GoodsService",version = "1.0")
4)在springBoot入口程序类上加开启Dubbo配置支持注解。@EnableDubboConfiguration
3. 开发dubbo服务消费者
1)加入dubbo集成springBoot框架的起步依赖,zookeeper注册中心客户端的依赖,dubbo接口服务依赖。

        <dependency>
            <groupId>com.alibaba.spring.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.hzc</groupId>
            <artifactId>springBoot_interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

2)在springBoot核心配置文件中配置dubbo

# 应用名称
spring.application.name=springBoot_consumer
# 应用服务 WEB 访问端口
server.port=8081

spring.dubbo.registry=zookeeper://localhost:2181


spring.mvc.view.suffix=.jsp
spring.mvc.view.prefix=/

3)编写controller类,调用远程的dubbo服务,在接口服务对象上加入@Reference注解,引入接口服务。

@Reference(interfaceName = "com.hzc.service.GoodsService" ,version = "1.0")
private GoodsService goodsService;

4)在springBoot入口程序类上加开启dubbo配置支持注解。@EnableDubboConfiguration
3.测试

posted @ 2021-09-06 07:47  IamHzc  阅读(140)  评论(0编辑  收藏  举报