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>