SpringBoot---(3) Spring Boot 集成Dubbo

一、SpringBoot集成Dubbo分布式框架思路

1、接口工程:存放实体类bean和业务接口
2、服务提供者:业务接口的实现类并将服务暴露且注册到注册中心,调用数据库持久层
(1)添加依赖(Dubbo,注册中心,接口工程)
(2)配置服务提供者核心配置文件
3、服务消费者:处理浏览器客户端发送的请求,从注册中心调用服务提供者所提供的服务
(1)添加依赖(Dubbo,注册中心,接口工程)
(2)配置服务消费者核心配置文件

二、集成Dubbo的基本步骤:

1、创建好三个工程项目:
  • 接口工程(Maven工程)
    在这里插入图片描述

  • 服务提供者工程(SpringBoot Web工程)
    在这里插入图片描述

  • 服务消费者工程(SpringBoot Web工程)
    在这里插入图片描述

2、整理和修改服务提供者的pom.xml文件的依赖

添加Dubbo依赖、注册中心zookeeper的依赖,接口工程的依赖

<dependencies>
        <!--解决使用@ConfigurationProperties 注解出现警告问题-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <!--SpringBoot框架web项目起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


        <!--Dubbo集成SpringBoot框架起步依赖-->
        <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.hcz</groupId>
            <artifactId>011-springboot-dubbo-interface</artifactId>
            <version>1.0.0</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>

            <!--SpringBoot项目打包编译的插件-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
3、配置服务提供者核心配置文件application.properties
#设置端口号
server.port=8081
#设置上下文跟
server.servlet.context-path=/


#设置Dubbo的配置
spring.application.name=012-springboot-dubbo-provider
#当前工程是一个服务提供者
spring.dubbo.server=true
#设置注册中心
spring.dubbo.registry=zookeeper://localhost:2181
4、整理和修改服务消费者的pom.xml文件的依赖

和步骤2一样

5、配置服务消费者核心配置文件application.properties
#设置端口号
server.port=8080
#设置上下文跟
server.servlet.context-path=/

#设置Dubbo的配置
spring.application.name=013-springboot-dubbo-consumer
#指定注册中心
spring.dubbo.registry=zookeeper://localhost:2181
6、在服务消费者项目中创建控制类方法

在这里插入图片描述

@Controller
public class StudentController {
    
    

    /**
     * 控制层调用业务层
     */
    private StudentService studentService;

    @RequestMapping(value = "/student/count")
    public @ResponseBody String studentCount(){
    
    

        Integer allStudentCount = studentService.queryAllStudentCount();
        return "学生总人数为:" + allStudentCount;
    }
}

这里引入了业务层接口StudentService,并在接口工程中创建这个接口类

7、在接口工程中创建接口类

在这里插入图片描述

public interface StudentService {
    
    
    /**
     * 获取学生总人数
     * @return
     */
    Integer queryAllStudentCount();

}
8、在服务提供者项目中实现该接口的实现类

在这里插入图片描述

@Component
@Service(interfaceClass = StudentService.class,version = "1.0.0",timeout = 15000)
public class StudentServiceImpl implements StudentService {
    
    
    @Override
    public Integer queryAllStudentCount() {
    
    
        //调用数据库持久层

        return 10;
    }
}

1、将该实现类注入到Spring容器,使用注解@Component而不是@Service注解
2、使用注解@Service将该接口实现类暴露出去(注意区别和以前的注入注解,这里的为@Service(com.alibaba.dubbo.config.annotation))

9、服务消费者引用暴露出来的接口实现类

使用注解@Reference引用暴露出来的接口实现类

在这里插入图片描述

10、在Application类中添加注解来开启Dubbo配置

在这里插入图片描述

11、开启zookeeper服务和注册中心服务

在这里插入图片描述
在这里插入图片描述

12、启动服务提供项目,然后再启动服务消费者项目

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/hcz666/article/details/115416715