-
a. 新建一个maven的module
-
b. 添加需要远程调用的接口声名以及这些接口中所需要的类
举个例子
public interface IUser {
boolean addUser(UserDto userDto);
List<UserDto> getUsers();
void addUserVoid(UserDto userDto);
}
- c. 打包
a. Maven dependency
<!-- CaoyxRpc core 的依赖 -->
<dependency>
<groupId>com.github.iancao</groupId>
<artifactId>caoyx-rpc-core</artifactId>
<version>${caoyxRpc.version}</version>
</dependency>
<!-- 步骤1中的声名接口依赖 -->
<dependency>
<artifactId>xxxxx</artifactId>
<groupId>xxxx</groupId>
<version>xxxxx</version>
</dependency>
b. 实现接口声名暴露服务
实现步骤一的接口声名
public class UserServer {
public static void main(String[] args) throws CaoyxRpcException {
CaoyxRpcProviderConfig config = new CaoyxRpcProviderConfig();
config.setApplicationName("caoyxRpc-sample-simple-server"); // 设置服务名称
config.setPort(1118); // 设置端口
CaoyxRpcProviderFactory caoyxRpcProviderFactory = new CaoyxRpcProviderFactory(config);
caoyxRpcProviderFactory.exportService(IUser.class, new UserImpl()); // 暴露服务 UserImpl为声名接口实现类
}
}
c. 启动服务
这条日志代表对应className的实现启动成功
exportService: className[xxxx] implVersion:[x] success:[true]
a. Maven dependency
<!-- CaoyxRpc core 的依赖 -->
<dependency>
<groupId>com.github.iancao</groupId>
<artifactId>caoyx-rpc-core</artifactId>
<version>${caoyxRpc.version}</version>
</dependency>
<!-- 步骤1中的声名接口依赖 -->
<dependency>
<artifactId>xxxxx</artifactId>
<groupId>xxxx</groupId>
<version>xxxxx</version>
</dependency>
b. 创建远程调用对象
CaoyxRpcInvokerConfig config = new CaoyxRpcInvokerConfig();
config.setIFace(IUser.class);
config.setProviderApplicationName("caoyxRpc-sample-simple-server");
config.setApplicationName("caoyxRpc-sample-simple-client");
config.setRegisterConfig(new RegisterConfig("127.0.0.1:1118", RegisterType.DIRECT));
CaoyxRpcReferenceBean rpcReferenceBean = new CaoyxRpcReferenceBean(config);
IUser user = (IUser)rpcReferenceBean.getObject();
c. 启动服务并调用
上一步骤中获取的IUser
对象就可以当做一个本地的对象进行调用