SpringBoot整合dubbo(注解配置dubbo)

准备

1、在服务器或者本地搭建好zookeeper,具体可参考文章:https://blog.csdn.net/qq_35620501/article/details/87519306
2、在服务器或者本地搭建好dubbo-admin,具体可参考文章:https://blog.csdn.net/qq_35044419/article/details/120044815

代码:

新springboot-dubbo01---空的maven工程

项目目录结构如下图:

在这里插入图片描述

 pom.xml:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
</parent>

<modules>
    <module>springboot-dubbo01-api-web</module>
    <module>springboot-dubbo01-service-impl</module>
    <module>springboot-dubbo01-service</module>
</modules>

新建模块,命名为springboot-dubbo01–service

service主要是用来存放在实体类、Enum、工具类、service等

项目结构如下图:

在这里插入图片描述

 接口  DemoService:

public interface DemoService {
    public List<Message> findMessage();
}

实体类  Message:

public class Message implements Serializable {
    private int id;
    private String city;
    private String time;
    private String weather;

    public Message() {}
    public int getId() {return id;}
    public void setId(int id) {this.id = id;}
    public String getCity() {return city;}
    public void setCity(String city) {this.city = city;}
    public String getTime() {return time;}
    public void setTime(String time) {this.time = time;}
    public String getWeather() {return weather;}
    public void setWeather(String weather) {this.weather = weather;}
}

新建模块,命名为springboot-dubbo01-service-impl

service-impl主要是存放service接口实现类、持久层的实现

项目结构如下图:

在这里插入图片描述

 pom.xml:

<parent>
    <groupId>com.wyj</groupId>
    <artifactId>springboot-dubbo01</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

<dependencies>
    <!-- mybatis -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.0.1</version>
    </dependency>
    <!-- mysql -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <!-- springboot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- dubbo -->
    <dependency>
        <groupId>io.dubbo.springboot</groupId>
        <artifactId>spring-boot-starter-dubbo</artifactId>
        <version>1.0.0</version>
    </dependency>
    <!-- 项目依赖 -->
    <dependency>
        <groupId>com.wyj</groupId>
        <artifactId>springboot-dubbo01-service</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <!-- 日志 -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.25</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>log4j-over-slf4j</artifactId>
    </dependency>
</dependencies>

<build>
    <finalName>springboot-dubbo01-service-impl</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

DemoServiceImpl:                @Service(timeout = 6000)//这个service是dubbo的service

@Service(timeout = 6000)//这个service是dubbo的service
public class DemoServiceImpl implements DemoService {

    @Autowired
    private DemoMapper demoMapper;

    @Override
    public List<Message> findMessage() {
        return demoMapper.findMessage();
    }
}

DemoMapper:

public interface DemoMapper {
    public List<Message> findMessage();
}

DemoMappe.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.wyj.mapper.DemoMapper">
    <select id="findMessage" resultType="com.wyj.entity.po.Message">
        select * from message
    </select>
</mapper>

application.properties:

# tomcat
server.port=8081
# dubbo
spring.dubbo.application.name=demo-provider
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
spring.dubbo.scan=com.wyj.service.impl
# dataBase
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot-dubbo01?allowMultiQueries=true&useUnicode=true&useSSL=false&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
# mybatis
mybatis.mapper-locations=classpath:mapper/*.xml

新建模块,命名为springboot-dubbo01-api-web

api主要是用来存放controller、handler、interceptor

项目结构如下图:

在这里插入图片描述

 pom.xml:

<parent>
    <groupId>com.wyj</groupId>
    <artifactId>springboot-dubbo01</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

<dependencies>
    <!-- thymeleaf -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <!--  springboor-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- dubbo -->
    <dependency>
        <groupId>io.dubbo.springboot</groupId>
        <artifactId>spring-boot-starter-dubbo</artifactId>
        <version>1.0.0</version>
    </dependency>
    <!-- 项目依赖,去除springboot整合mybatis的jar包原因是该jar包中会自动注入sqlsession,需要在api层配置DataSource相关参数 -->
    <dependency>
        <groupId>com.wyj</groupId>
        <artifactId>springboot-dubbo01-service-impl</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <exclusions>
            <exclusion>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
            </exclusion>
            <exclusion>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<build>
    <finalName>springboot-dubbo01-api-web</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

DemoController:

注意:

这里不能使用@Autowired注入bean,要使用dubbo中的注解@Reference注入bean和dubbo接口

@RestController
public class DemoController {
    @Reference
    private DemoService demoService;

    @RequestMapping(value = "/query")
    public ApiResponse demo() {
        try {
            List<Message> messageList = demoService.findMessage();
            return new ApiResponse(200, "操作成功", messageList);
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResponse(500, "系统异常");
        }
    }
}

application.properties:

# tomcat
server.port=8080
# dubbo
spring.dubbo.application.name=demo-consumer
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.scan=com.wyj.controller

测试

  • 先运行service-impl的服务,再启动api的服务,原则上是先启动service-impl的

  • 访问

浏览器访问:http://127.0.0.1:8080/query

查看监控中心

访问dubbo-admin

在这里插入图片描述

 点击服务治理中的服务

在这里插入图片描述

从监控中心可以看到com.wyj.service.DemoService这个接口能够正常注册到zookeeper中,并被api给消费 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值