1.使用zookeeper作为注册中心,所以需要在服务器上安装zookeeper,将Dubbo Admin 先运行在服务器上,以便查看服务状态
2.导入sql:
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`sex` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, '王尔瓦', '女', 20);
INSERT INTO `user` VALUES (2, '李四', '男', 21);
INSERT INTO `user` VALUES (3, '王五', '女', 26);
SET FOREIGN_KEY_CHECKS = 1;
3.创建接口类和实体类的工程commons,创建消费者工程consumer,创建生产者工程provider
4.commos工程中创建
实体类:(由于远程服务调用需要网络传输,所以实体类需要实现序列化)
//使用lombok注解简化代码,记得引入依赖,安装Lombok插件
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
private Integer id;
private String name;
private String sex;
private Integer age;
}
接口类:
public interface UserService {
/**
* 添加用户
* @param user
* @return
*/
boolean addUser(User user);
/**
* 根据id删除用户
* @param id
* @return
*/
boolean deleteUserById(int id);
/**
* 根据id删除用户
* @param user
* @return
*/
boolean updateUserById(User user);
/**
* 根据id查询用户
* @param id
* @return
*/
User getUserById(int id);
/**
* 获取所有用户
* @return
*/
List getAllUser();
}
5.将commons工程安装到本地仓库,在provider工程中引入
pom.xml:
springboot_dubbo
org.example
1.0-SNAPSHOT
4.0.0
dubbo_provider
org.example
dubbo_commons
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
com.baomidou
mybatis-plus-boot-starter
3.4.1
mysql
mysql-connector-java
8.0.26
org.apache.dubbo
dubbo-spring-boot-starter
2.7.6
org.apache.dubbo
dubbo-dependencies-zookeeper
2.7.6
pom
org.slf4j
slf4j-log4j12
provider工程中创建服务实现类实现UserService接口(注意service注解是dubbo包中的)
@Service//不是spring中的service而是dubbo中的
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public boolean addUser(User user) {
return userMapper.addUser(user);
}
@Override
public boolean deleteUserById(int id) {
return userMapper.deleteUserById(id);
}
@Override
public boolean updateUserById(User user) {
return userMapper.updateUserById(user);
}
@Override
public User getUserById(int id) {
return userMapper.getUserById(id);
}
@Override
public List getAllUser() {
return userMapper.getAllUser();
}
}
provider工程中创建mapper接口
public interface UserMapper {
boolean addUser(User user);
boolean deleteUserById(int id);
boolean updateUserById(User user);
User getUserById(int id);
List getAllUser();
}
provider工程中创建mapper.xml
insert into user values (null ,#{name},#{sex},#{age})
delete from user where id=#{id}
update user set name=#{name},sex=#{sex},age=#{age} where id=#{id}
provider配置文件:
server:
port: 8080
spring:
application:
name: mc_provider
#访问数据库
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql:///dubbo_demo?serverTimezone=UTC&useSSL=true&characterEncoding=utf-8
username: root
password: 123456
#mybatis-plus
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.it.entity
#dubbo,zookeeper
dubbo:
application:
name: dubbo_provider
#zookeeper地址
registry:
address: zookeeper://192.14.80.206:2181
timeout: 10000
#暴露端口
protocol:
name: dubbo
port: 20880
启动类:(注意加上开启dubbo注解)
@SpringBootApplication
@EnableDubbo//开启dubbo
@MapperScan("com.it.mapper")//扫描mapper接口
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class,args);
}
}
6.将commons映入consumer工程
pom.xml:
springboot_dubbo
org.example
1.0-SNAPSHOT
4.0.0
dubbo_customer
org.example
dubbo_commons
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
在controller中调用服务(注意使用Dubbo中的@Reference才能调用服务,而不是jdk中的@Reference)
@RestController
@RequestMapping("/user")
public class UserController {
@Reference
private UserService userService;
/**
* 获取所有用户
* @param
* @return
*/
@RequestMapping("/getAllUser")
public List getAllUser(){
return userService.getAllUser();
}
/**
* 添加用户
* @param user
* @return
*/
@RequestMapping("/addUser")
public boolean addUser(@RequestBody User user){
return userService.addUser(user);
}
/**
* 根据id删除用户
* @param id
* @return
*/
@RequestMapping("/deleteUserById")
public boolean deleteUserById(int id){
return userService.deleteUserById(id);
}
/**
* 根据id更新用户
* @param user
* @return
*/
@RequestMapping("/updateUserById")
public boolean updateUserById(@RequestBody User user){
return userService.updateUserById(user);
}
/**
* 根据id获取用户
* @param id
* @return
*/
@RequestMapping("/getUserById")
public User getUserById(int id){
return userService.getUserById(id);
}
}
配置文件:
server:
port: 8081
dubbo:
application:
name: dubbo_customer
registry:
address: zookeeper://192.14.80.206:2181
timeout: 10000
启动类:(注意加上开启dubbo注解)
@SpringBootApplication
@EnableDubbo//开启dubbo
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class,args);
}
}
7.先启动生产者工程,再启动消费者工程
消费者能够调用到服务