所需必备知识
- zookeeper 集群搭建(传送门:zookeeper集群-mac版本)
- ide 一个
- 表一张
一 项目的创建
上图是一个简单的聚合项目,工具是idea, 可以用eclipse,sts,都差不太多。
dubbo-c是消费者,dubbo-i是接口,dubbo-p是提供者
二 项目的配置
父级pom,关键依赖
<dependencies>
<!--springBoot动态的引入springMVC全部的配置 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--添加属性注入依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--引入插件lombok 自动的set/get/构造方法插件 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--引入数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--springBoot数据库连接 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--spring整合mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!--引入dubbo配置 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
</dependencies>
消费者的yml文件
server:
port: 9001
dubbo:
scan:
basePackages: com.fxm
application:
name: consumer-user #定义消费者名称
registry: #注册中心地址,要改成你自己的ip
address: zookeeper://192.168.15.2:2181?backup=192.168.15.2:2182,192.168.15.2:2183
提供者的yml文件
server:
port: 9000 #定义端口
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/cjtdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
username: root
password: root #要改成你的账户密码
dubbo:
scan:
basePackages: com.fxm
application:
name: provider-user #指定服务名称(必须指定)
registry:
address: zookeeper://192.168.15.2:2181?backup=192.168.15.2:2182,192.168.15.2:2183
protocol:
name: dubbo #使用dubbo协议(tcp-ip)
port: 20880 #服务链接时的端口号
mybatis-plus:
type-aliases-package: com.fxm.dubbo.pojo
mapper-locations: classpath:/mybatis/mappers/*.xml
configuration:
map-underscore-to-camel-case: true
分别在消费者和提供者的pom文件里加入i接口包的依赖
三 简单代码实现
提供者: 需要提供数据,创建mapper和serviceimpL(故意写成这样,因为这个字体 i 和 L 大小写难以区分)
接口包: 提供封装对象和接口
消费者 :就写个控制器,给前台测试即可
消费者控制器代码
package com.fxm.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.fxm.pojo.User;
import com.fxm.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
@Reference(timeout=3000,check=false,loadbalance="consistenthash")
private UserService userService;
@RequestMapping("/findAll")
public List<User> findAll(){
System.out.println(userService.getClass());
return userService.findAll();
}
@RequestMapping("/saveUser/{name}/{age}/{sex}")
public String saveUser(User user) {
userService.saveUser(user);
return "用户入库成功!!!";
}
}
提供者,公共接口代码
这里用的单表操作插件,mybatisplus插件,创建好mapper接口累即可,没有实际的sql.
其实提供者,接口代码都不用贴出来,都可以推断出来,和普通的mvc没有啥区别
这提供者负责查询数据库,实现接口,和普通的业务写法一样;
package com.fxm.dubbo.service;
import java.util.List;
import com.fxm.pojo.User;
import com.fxm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;
import com.fxm.dubbo.mapper.UserMapper;
@Service(timeout=3000) //3秒超时
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> findAll() {
System.out.println("我是服务的提供者");
return userMapper.selectList(null);
}
@Override
public void saveUser(User user) {
userMapper.insert(user);
}
}
#接口代码
Lis<User> findAll();
void saveUser(User user);
#------------------------------------------
#------------------------------------------
#pojo
@Data
@Accessors(chain=true)
@TableName
public class User implements Serializable{
private static final long serialVersionUID = 1L;
@TableId(type=IdType.AUTO)
private Integer id;
private String name;
private Integer age;
private String sex;
}
四 启动项目测试
转载:https://blog.csdn.net/weixin_45560548/article/details/105993110
查看评论