1 理论
消费者A和消费者B分别部署在两台机器上
一般将User类定义为独立模块,A模块和B模块都通过maven依赖于该模块即可
-
dubbo 内部已经将序列化和反序列化的过程内部封装了
-
只需要在定义pojo类时实现Serializable接口即可
-
一般会定义一个公共的pojo模块,让生产者和消费者都依赖该模块。
2 实践
package com.yppah.pojo;
import java.io.Serializable;
/**
* @Author: haifei
* @Date: 2022/10/14 22:24
*/
public class User implements Serializable {
private int id;
private String username;
private String password;
// 快捷键:alt + insertwedfd
public User(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public User() {
}
// 快捷键:alt + insert
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
<?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.yppah</groupId>
<artifactId>dubbo-interface</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.yppah</groupId>
<artifactId>dubbo-pojo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
package com.yppah.service;
import com.yppah.pojo.User;
/**
* @Author: haifei
* @Date: 2022/10/9 0:06
*/
public interface UserService {
public String sayHello();
/**
* 查询用户
*/
public User findUserById(int id);
}
package com.yppah.service.impl;
import com.yppah.pojo.User;
import com.yppah.service.UserService;
import org.apache.dubbo.config.annotation.Service;
//import org.springframework.stereotype.Service;
/**
* @Author: haifei
* @Date: 2022/10/9 0:07
*/
//@Service //定义bean:将该类的对象创建出来,放到spring的IOC容器中
@Service //将该类提供的方法(服务)对外发布,将访问地址(ip,端口,路径)注册到注册中心中(zk)
public class UserServiceImpl implements UserService {
@Override
public String sayHello() {
return "hello dubbo! hihihi";
}
@Override
public User findUserById(int id) {
// 查询User对象
User user = new User(1, "zhangsan", "123");
return user;
}
}
package com.yppah.controller;
//import com.yppah.service.UserService;
import com.yppah.pojo.User;
import com.yppah.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: haifei
* @Date: 2022/10/9 0:30
*/
@RestController
@RequestMapping("/user")
public class UserController {
//@Autowired//本地注入
/*
1. 从注册中心zk获取useService的访问url
2. 进行远程调用RPC
3. 将结果封装为一个代理对象,并给变量赋值
*/
@Reference//远程注入
private UserService userService;
@RequestMapping("/sayHello")
public String sayHello(){
return userService.sayHello();
}
/**
* 根据id查询用户信息
* @param id
* @return
*/
@RequestMapping("/findUser")
public User findUser(int id){
return userService.findUserById(id);
}
}
- pojo模块被interface模块依赖
- 而service模块和web模块又依赖于interface模块
- 所以service模块和web模块间接依赖于pojo模块
- 启动顺序:pojo模块install,interface模块install,service模块和web模块tomcat-run
- 注意:被依赖的模块有了改动后,一定要重新install后才能生效
访问<localhost:8000/user/findUser.do?id=1>测试
标签:dubbo,HMdubbo6,yppah,id,User,模块,import,序列化,public From: https://www.cnblogs.com/yppah/p/16793281.html