整合dubbo的主要步骤:
- 接口项目开发,打jar包
- 开发dubbo服务提供者
- 开发dubbo服务消费者
接口项目开发
在一个项目下创建一个实体类和一个能调用该实体类的接口
开发dubbo服务提供者
1.创建好一个springboot项目,并配置好相关的依赖
使用idea创建项目的,直接利用idea插件创建
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot</groupId>
<artifactId>dubbo-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-provider</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
2.加入springboot与dubbo集成的起步依赖
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
3.在springboot的核心配置文件application.properties中配置dubbo的信息
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>5.1.40</version>
</dependency>
<dependency>
<groupId>com.springboot</groupId>
<artifactId>dubbo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
4.编写dubbo的接口实现类
@Component
@Service(version = "1.0.0")
public class CustomerServiceImpl implements CustomerService {
@Autowired
private CustomerMapper customerMapper;
@Override
public String sayHi(String name) {
return "hi Springboot-dubbo" + name;
}
@Override
public Customer getCustomer(Long custId) {
return customerMapper.selectByPrimaryKey(custId);
}
}
开发dubbo服务消费者
@RestController
public class CustomerController {
@Reference(version = "1.0.0")
private CustomerService customerService;
@RequestMapping("/boot/customer")
public Object getCustomer(@RequestParam("custId") Long custId){
return customerService.getCustomer(custId);
}
}
<!--springboot集成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>
</dependency>
然后在浏览器请求,就可以实现了