【SpringBoot】集成Dubbo分布式框架

SpringBoot集成Dubbo分布式框架

a.接口工程:存放实体bean和业务接口(maven项目)

在这里插入图片描述

StudentService

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

pom

<?xml version="1.0" encoding="UTF-8"?>
<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>

    <groupId>com.utkvrjan</groupId>
    <artifactId>020-spring-dubbo-interface</artifactId>
    <version>1.0.0</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>

b.服务提供者:业务接口的实现类并将服务暴露且注册到注册中心,调用数据持久层

  • 添加依赖(dubbo,注册中心,接口工程)
  • 配置服务提供者核心配置文件

在这里插入图片描述

StudentServiceImpl

import com.alibaba.dubbo.config.annotation.Service;
import com.utkvrjan.springboot.service.StudentService;
import org.springframework.stereotype.Component;


@Component//交给spring容器来管理
@Service(interfaceClass = StudentService.class,version="1.0.0",timeout = 15000)
public class StudentServiceImpl implements StudentService {
    @Override
    public Integer queryAllStudentCount() {

        //调用数据持久层

        return 1250;
    }
}

Application

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication  //开启spring注解的配置
@EnableDubboConfiguration  //开启Dubbo配置
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

application.properties

# 设置内嵌Tomcat端口号
server.port=8080
# 设置上下文根
server.servlet.context-path=/

#设置dubbo配置
spring.application.name=021-springboo-dubbo-provider
# 当前工程是一个服务提供者
spring.dubbo.server=true
# 设置注册中心
spring.dubbo.registry=zookeeper://localhost:2181

pom

<?xml version="1.0" encoding="UTF-8"?>
<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 https://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.4.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.utkvrjan.springboot</groupId>
    <artifactId>021-springboo-dubbo-provider</artifactId>
    <version>1.0.0</version>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--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.utkvrjan</groupId>
            <artifactId>020-spring-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>

</project>

c.服务消费者:处理浏览器客户端发送的请求,从注册中心调用服务提供者所提供的服务

  • 添加依赖(dubbo,注册中心,接口工程)
  • 配置服务消费者核心配置文件
    在这里插入图片描述

StudentController

import com.alibaba.dubbo.config.annotation.Reference;
import com.utkvrjan.springboot.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class StudentController {
    //check=false当服务消费者开启时不去检查提供者是否开启,只会在调用服务时再去检查
    @Reference(interfaceClass = StudentService.class,version="1.0.0",check = false)
    private StudentService studentService;


    @RequestMapping("/student/count")
    public @ResponseBody Object studentConut(){
        Integer allStudentCount = studentService.queryAllStudentCount();
        return "学生总人数为:"+allStudentCount;
    }
}

Application

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication  //开启spring注解的配置
@EnableDubboConfiguration  //开启Dubbo配置
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

application.properties

# 设置内嵌Tomcat端口号
server.port=8081
server.servlet.context-path=/
#设置dubbo配置
spring.application.name=022-springboot-dubbo-consumer
# 指定注册中心
spring.dubbo.registry=zookeeper://localhost:2181

pom

<?xml version="1.0" encoding="UTF-8"?>
<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 https://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.4.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.utkvrjan.springboot</groupId>
    <artifactId>022-springboot-dubbo-consumer</artifactId>
    <version>1.0.0</version>
    <name>022-springboot-dubbo-consumer</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--springboot项目web项目起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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.utkvrjan</groupId>
            <artifactId>020-spring-dubbo-interface</artifactId>
            <version>1.0.0</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
endency>
    </dependencies>

    <build>
        <plugins>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值