After studying Netty, I decided to write a lightweight RPC framework based on Netty, ZooKeeper, and Spring, which has been a rewarding experience for me.
- Support for long connections
- Support for asynchronous invocations
- Support for heartbeat detection
- Support for JSON serialization
- Near-zero configuration, implemented through annotations
- Service registration center implemented based on ZooKeeper
- Support for dynamic management of client connections
- Support for client service listening and discovery functionality
- Support for server service registration functionality
- Implemented using Netty 4.X version
-
Add your own Service under the Service package on the server-side, and annotate it with the @Service annotation.
@Service public class TestService { public void test(User user){ System.out.println("调用了TestService.test"); } }
-
Generating a Service Interface and its Implementation Class ######The interface is as follows:
public interface TestRemote { public Response testUser(User user); }
In the implementation class, we add the @Remote annotation to indicate that this class is where the actual service invocation takes place. This class is responsible for invoking the service methods and can generate any form of response that you desire to be returned to the client
@Remote public class TestRemoteImpl implements TestRemote{ @Resource private TestService service; public Response testUser(User user){ service.test(user); Response response = ResponseUtil.createSuccessResponse(user); return response; } }
- Generate an interface on the client-side that represents the interface you want to invoke.
public interface TestRemote { public Response testUser(User user); }
- Generate a property in the place where you want to make the invocation, in the form of an interface, and annotate the property with @RemoteInvoke.
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes=RemoteInvokeTest.class) @ComponentScan("\\") public class RemoteInvokeTest { @RemoteInvoke public static TestRemote userremote; public static User user; @Test public void testSaveUser(){ User user = new User(); user.setId(1000); user.setName("张三"); userremote.testUser(user); } }