storm-rpc
Start fast (using Spring Boot)
Api
Interface IHelloService. Java
package me.stormma.api;
/ * * *@author stormma [email protected]
*/
public interface IHelloService {
String sayHello(String name);
}
Copy the code
Service interface POM dependencies
<?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>
<artifactId>storm-rpc-spring-boot-starter-test-api</artifactId>
</project>
Copy the code
Provider
The service provider implements HelloService.java
package me.stormma.storm.rpc.spring.boot.starter.test.provider.provider;
import me.stormma.annoation.Provider;
import me.stormma.api.IHelloService;
/ * * *@author stormma [email protected]
*/
@Provider(interfaceClass = IHelloService.class, version = "2.0.0")
public class HelloService implements IHelloService {
@Override
public String sayHello(String name) {
return "hello " + name + "!"; }}Copy the code
Service provider POM dependencies
<?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>me.stormma</groupId>
<artifactId>storm-rpc-spring-boot-starter-test-provider</artifactId>
<version>0.0.1 - the SNAPSHOT</version>
<packaging>jar</packaging>
<name>storm-rpc-spring-boot-starter-test-provider</name>
<description>storm-rpc-spring-boot-starter-test-provider</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>. 2.0.0 RELEASE</version>
<relativePath/> <! -- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>me.stormma</groupId>
<artifactId>storm-rpc-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>me.stormma</groupId>
<artifactId>storm-rpc-spring-boot-starter-test-api</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Copy the code
The service provider to start class StormRpcSpringBootStarterTestProviderApplication. Java
package me.stormma.storm.rpc.spring.boot.starter.test.provider;
import me.stormma.rpc.spring.boot.annotation.EnableStormRpcConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableStormRpcConfiguration
public class StormRpcSpringBootStarterTestProviderApplication {
public static void main(String[] args) { SpringApplication.run(StormRpcSpringBootStarterTestProviderApplication.class, args); }}Copy the code
Service provider configuration file application.properties
. Spring. Storm. The RPC server = true spring. Storm. The RPC. The host = 127.0.0.1 spring. Storm. The RPC. Port = 52057 spring. Storm. The RPC. Weight = 3 Spring. Storm. The RPC. Registry = 139.199.27.243:2181 spring.storm.rpc.basePackage=me.stormma.storm.rpc.spring.boot.starter.test.provider spring.storm.rpc.name=providerCopy the code
Consumer
The service consumer test class HelloServiceTest. Java
package me.stormma.storm.rpc.spring.boot.starter.test.consumer.consumer;
import me.stormma.api.IHelloService;
import me.stormma.rpc.spring.boot.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/ * * *@author stormma [email protected]
*/
@RestController
public class HelloServiceTest {
@Reference(interfaceClass = IHelloService.class, version = "2.0.0")
private IHelloService helloService;
public int a = 0;
@GetMapping("/say/hello/{name}")
public String test(@PathVariable String name) {
returnhelloService.sayHello(name); }}Copy the code
Service consumer POM dependencies
<?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>me.stormma</groupId>
<artifactId>storm-rpc-spring-boot-starter-test-consumer</artifactId>
<version>0.0.1 - the SNAPSHOT</version>
<packaging>jar</packaging>
<name>storm-rpc-spring-boot-starter-test-consumer</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>. 2.0.0 RELEASE</version>
<relativePath/> <! -- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>me.stormma</groupId>
<artifactId>storm-rpc-spring-boot-starter-test-api</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>me.stormma</groupId>
<artifactId>storm-rpc-spring-boot-starter</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Copy the code
Service consumers start class StormRpcSpringBootStarterTestConsumerApplication. Java
package me.stormma.storm.rpc.spring.boot.starter.test.consumer;
import me.stormma.rpc.spring.boot.annotation.EnableStormRpcConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableStormRpcConfiguration
public class StormRpcSpringBootStarterTestConsumerApplication {
public static void main(String[] args) { SpringApplication.run(StormRpcSpringBootStarterTestConsumerApplication.class, args); }}Copy the code
The service consumer profile application.properties
Spring. Storm. The RPC. Discover = 139.199.27.243:2181 spring. Storm. The RPC. The name = consumer server port = 8090Copy the code
Start Fast (native)
Api
Service interface ihelloService.java
package me.stormma.api;
/ * * *@author stormma [email protected]
*/
public interface IHelloService {
String sayHello(String name);
}
Copy the code
Provider
The service provider implements HelloService.java
package me.stormma.provider;
import me.stormma.annoation.Provider;
import me.stormma.api.IHelloService;
/ * * *@author stormma [email protected]
*/
@Provider(interfaceClass = IHelloService.class, version = "1.0.0")
public class HelloService implements IHelloService {
@Override
public String sayHello(String name) {
return "hello "+ name; }}Copy the code
The service provider starts the serverBootstrap.java class
package me.stormma.provider;
import me.stormma.constants.Constants;
import me.stormma.rpc.model.ServerInfo;
import me.stormma.rpc.netty.bootstrap.RpcServer;
import me.stormma.rpc.registry.ServiceRegistry;
import me.stormma.rpc.registry.zk.ZookeeperServiceRegistry;
/ * * *@author stormma [email protected]
*/
public class ServerBootstrap {
public static void main(String... args) throws Exception {
String zkServer = "139.199.27.243:2181";
ServiceRegistry serviceRegistry = new ZookeeperServiceRegistry(zkServer);
ServerInfo serverInfo = new ServerInfo(Constants.DEFAULT_HOST, Constants.DEFAULT_PORT
, Constants.SERVER_DEFAULT_WEIGHT);
RpcServer rpcServer = new RpcServer(serviceRegistry, serverInfo);
rpcServer.start("me.stormma.provider"); }}Copy the code
Consumer
The service consumer implementation class ConsumerClient.java
package me.stormma.consumer;
import me.stormma.api.IHelloService;
import me.stormma.rpc.proxy.DefaultProxy;
import me.stormma.rpc.registry.ServiceDiscover;
import me.stormma.rpc.registry.zk.ZookeeperServiceDiscover;
/ * * *@author stormma [email protected]
*/
public class ConsumerClient {
public static void main(String[] args) {
String zkServer = "139.199.27.243:2181";
ServiceDiscover serviceDiscover = new ZookeeperServiceDiscover(zkServer);
DefaultProxy proxy = new DefaultProxy(serviceDiscover);
IHelloService helloService = proxy.createProxy(IHelloService.class, "1.0.0");
System.out.println(helloService.sayHello("stormma"));
System.exit(0); }}Copy the code
Storm-rpc technology dependency
-
Zookeeper is used for service registration and discovery (The service name is created as the service provider after startup to create a ZK persistent node for storage, and the corresponding address of the service is created as the content of the temporary node to realize dynamic discovery of service downtime and online. The temporary node is represented as ‘host:port$weight’, where host:port represents the service address and weight represents the weight of the service address, which is used to implement load balancing. The load balancing algorithm is based on dubbo’s load balancing algorithm.
-
Netty, use netty for network communication between services, netty use reference <> this book.
-
Kryo, which is used for serialization and deserialization.
-
Dynamic proxy (this project provides dynamic proxy implemented by native JDK and CGLIB, and users can customize which proxy mode to use). The specific working process is as follows: Override BeanPostProcess to resolve whether the Bean’s fields contain Reference annotations and create a proxy object injection if they do.
The storm xml-rpc function point
- Service registration and discovery
- Load balancing
- Integrated spring boot
- The remote invocation
- Disconnection and reconnection (to be added later)
Afterword.
It took more than a week from the beginning of development to the launch of a version of Storm-RPC. The implementation borrowed a lot of code from Dubbo. We look forward to your joining us to make Storm-RPC perfect.
The project address