那些年我造的輪子之RPC

程式設計師一北發表於2018-04-05

storm-rpc

快速開始(使用Spring Boot)

Api

介面IHelloService.java

package me.stormma.api;

/**
 * @author stormma stormmaybin@gmail.com
 */
public interface IHelloService {
    String sayHello(String name);
}
複製程式碼

服務介面pom依賴

<?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>
複製程式碼

Provider

服務提供者實現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 stormmaybin@gmail.com
 */
@Provider(interfaceClass = IHelloService.class, version = "2.0.0")
public class HelloService implements IHelloService {
    @Override
    public String sayHello(String name) {
        return "hello " + name + "!";
    }
}
複製程式碼

服務提供者pom依賴

<?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-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>
複製程式碼

服務提供者啟動類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);
	}
}
複製程式碼

服務提供者配置檔案application.properties

spring.storm.rpc.server=true
spring.storm.rpc.host=127.0.0.1
spring.storm.rpc.port=52057
spring.storm.rpc.weight=3
spring.storm.rpc.registry=139.199.27.243:2181
spring.storm.rpc.basePackage=me.stormma.storm.rpc.spring.boot.starter.test.provider
spring.storm.rpc.name=provider
複製程式碼

Consumer

服務消費者測試類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 stormmaybin@gmail.com
 */
@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) {
        return helloService.sayHello(name);
    }
}
複製程式碼

服務消費者pom依賴

<?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-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>
複製程式碼

服務消費者啟動類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);
	}
}
複製程式碼

服務消費者配置檔案application.properties

spring.storm.rpc.discover=139.199.27.243:2181
spring.storm.rpc.name=consumer
server.port=8090
複製程式碼

快速開始(原生)

Api

服務介面IHelloService.java

package me.stormma.api;

/**
 * @author stormma stormmaybin@gmail.com
 */
public interface IHelloService {

    String sayHello(String name);
}
複製程式碼

Provider

服務提供者實現HelloService.java

package me.stormma.provider;

import me.stormma.annoation.Provider;
import me.stormma.api.IHelloService;

/**
 * @author stormma stormmaybin@gmail.com
 */
@Provider(interfaceClass = IHelloService.class, version = "1.0.0")
public class HelloService implements IHelloService {
    @Override
    public String sayHello(String name) {
        return "hello " + name;
    }
}
複製程式碼

服務提供者啟動類ServerBootstrap.java

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 stormmaybin@gmail.com
 */
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");
    }
}
複製程式碼

Consumer

服務消費者實現類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 stormmaybin@gmail.com
 */
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);
    }
}
複製程式碼

storm-rpc技術依賴

  1. zookeeper, 使用zookeeper進行服務註冊與發現(服務名稱作為服務提供者啟動之後建立zk永續性節點儲存, 服務對應的地址建立為臨時性節點的內容, 實現動態 發現服務當機和上線, 臨時性節點表示為'host:port$weight', host:port代表服務地址, weight表示為該服務地址的權重, 用來實現負載均衡, 負載均衡演算法參考 dubbo的負載均衡演算法, 基於權重的負載均衡演算法)

  2. netty, 使用netty進行服務之間的網路通訊, netty的使用參考<>這本書。

  3. kryo, 使用kryo進行序列化和反序列化。

  4. 動態代理(本專案提供了原生JDK動態代理和CGLIB來實現的動態代理, 使用者可以自行配置使用哪種代理方式), 具體工作流程如: 重寫BeanPostProcess來實現 解析Bean的欄位是否包含Reference註解, 如果包含就建立個代理物件注入。

storm-rpc功能點

  1. 服務註冊和發現
  2. 負載均衡
  3. 整合spring boot
  4. 遠端呼叫
  5. 斷線重連(後續增加)

後記

storm-rpc從開始開發到一個版本的推出前前後後經歷了一週多, 實現借鑑了dubbo的很多程式碼, 期待你的加入能讓storm-rpc走向完善。

專案地址

相關文章