搭建springboot+dubbo+zookeeper+mybatis具体操作:(注意必须启动zookeeper服务哦!)
一、打开IDE创建springboot父项目demo,再新建三个module:
1. dubbo-api(对外RPC接口)
2. dubbo-provider(dubbo接口提供者)
3. dubbo-customer(dubbo接口消费者)
如图所示:
二、 父项目demo的pom.xml文件的配置
<?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.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dubbo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<!--子模块-->
<modules>
<module>dubbo-api</module>
<module>dubbo-provider</module>
<module>dubbo-customer</module>
</modules>
<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>
<!--手动添加包-->
<!--dubbo整合springboot包-->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>1.0.1</version>
</dependency>
<!--zookeeper客户端来连接zookeeper服务并注册服务-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<!--zookeeper包-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
三、 3个子项目相关配置与文件
3.1 dubbo-api(对外RPC接口) -->主要放实体、接口、也可以放一些公用的工具类工程
业务接口
3.2 dubbo-provider(dubbo接口提供者) -->主要是服务提供者
mapper接口:
import com.dubbo.demo.api.entity.Userinfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* 底层数据UserinfoMapper接口
*/
@Mapper //扫描注入bean
public interface UserinfoMapper {
@Select("select * from userinfo")
@Results(value = {
@Result(property = "id", column = "id"),
@Result(property = "userCode", column = "userCode"),
@Result(property = "userName", column = "userName"),
@Result(property = "userPassword", column = "userPassword")
})
public List<Userinfo> getAll();
@Select("select * from userinfo where id=#{id}")
public Userinfo findById(Integer id);
}
实现接口的业务类
application.yml
spring:
dubbo:
appname: provider
registry: zookeeper://47.240.20.111:2181
port: 20080
protocol: dubbo
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/smbms?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false
username: root
password: root
server:
port: 8882
providerApplication运行:
3.3 dubbo-customer(dubbo接口消费者) -->主要是服务消费者
控制器:
package com.hlx.demo.customer.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.dubbo.demo.api.entity.Userinfo;
import com.dubbo.demo.api.service.UserinfoService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
//控制器
@RestController
public class UserinfoController {
//业务接口bean的注入
@Reference(check = false) //dubbo的包
private UserinfoService userinfoService;
@RequestMapping("/say1/{name}")
public String say1(@PathVariable("name") String name){
System.out.println("进来了!say1");
//调用业务方法
return "how are you ? "+userinfoService.hello(name);
}
@GetMapping("say2")
public String say2(){
System.out.println("进来了!say2");
//调用业务方法
return "你好吗? "+userinfoService.hello("mike");
}
@GetMapping("all")
public List<Userinfo> all(){
System.out.println("查看数据列表++++》");
return userinfoService.getList();
}
@RequestMapping("/find/{id}")
public Userinfo find(@PathVariable("id") Integer id){
System.out.println("查看用户信息一条++++》");
return userinfoService.getUserinfo(id);
}
}
application.yml文件:
server:
port: 8884
servlet:
context-path: /
spring:
dubbo:
appname: consumer-test
registry: zookeeper://47.240.20.111:2181
protocol: dubbo
customerApplication运行:
先启动服务器,再启动消费者
运行效果:
有参数的传递方式
无参数的传递方式
查询所有的数据以JSON格式显示
根据ID查询数据