项目作者: dressrosa

项目描述 :
一款简洁的rpc
高级语言: Java
项目地址: git://github.com/dressrosa/beacon.git


beacon

(beacon:灯塔.意为技术乃coder前进之方向)
image
image
LICENSE-blue.svg)
996.icu

基本原理:

基本原理

调用流程:

consumer:

spring factory bean,基于接口生成
->proxy,生成代理类(cglib/jdk)
->registry,注册接口
->filter 自定义过滤器,对接口进行过滤
->tolerant,容错机制(failfast等)
->loadBalance(负载均衡,random等)
->strategy(熔断降级)
->invocation(生成调用者)
->serialize(进行消息序列化)
->tranporter to pro ,wait result(通讯层传输给provider端)

provider:

->transporter接收消息
->deserialize 反序列化消息
->local invoke
->serialize 进行消息序列化
->transporter to con,send result(通讯层传输给consumer端)

快速开始:

provider端:

  1. 在pom文件中引入:
    1. <dependency>
    2. <groupId>com.xiaoyu</groupId>
    3. <artifactId>beacon</artifactId>
    4. <version>0.0.1</version>
    5. </dependency>
  2. 创建公共api
    1. public interface IHelloService {
    2. public String hello(String name);
    3. }
  3. 实现IHelloService
    1. public class HelloServiceImpl implements IHelloService {
    2. @Override
    3. public String hello(String name) {
    4. return "hello " + name;
    5. }
    6. }
  4. 创建beacon-server.xml

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:beacon="http://www.iwouldbe.com/schema/beacon"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    6. http://www.iwouldbe.com/schema/beacon http://www.iwouldbe.com/schema/beacon.xsd">
    7. <!--beacon相关配置 -->
    8. <beacon:protocol name="beacon" port="1992" ></beacon:protocol>
    9. <beacon:registry address="127.0.0.1:2181" protocol="zookeeper" ></beacon:registry>
    10. <beacon:exporter id="helloService1" group="dev"
    11. interfaceName="com.xiaoyu.test.api.IHelloService" ref="com.xiaoyu.test.api.impl.HelloServiceImpl" ></beacon:exporter>
    12. </beans>
  5. 在你的spring xml中引入beacon-server.xml

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:tx="http://www.springframework.org/schema/tx"
    5. xmlns:context="http://www.springframework.org/schema/context"
    6. xsi:schemaLocation="
    7. http://www.springframework.org/schema/context
    8. http://www.springframework.org/schema/context/spring-context-3.0.xsd
    9. http://www.springframework.org/schema/beans
    10. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    11. ">
    12. <context:component-scan base-package="com.xiaoyu " ></context:component-scan>
    13. <import resource="classpath:beacon-server.xml" ></import>
    14. </beans>
  6. 创建启动类
  1. public static void main(String[] args) throws Exception {
  2. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-application.xml");
  3. try {
  4. CountDownLatch latch = new CountDownLatch(1);
  5. context.start();
  6. latch.await();
  7. } finally {
  8. context.stop();
  9. context.close();
  10. }
  11. }

consumer端:

  1. 在pom文件中引入:
    1. <dependency>
    2. <groupId>com.xiaoyu</groupId>
    3. <artifactId>beacon</artifactId>
    4. <version>0.0.1</version>
    5. </dependency>
  2. 创建beacon-server.xml
    ```
    <?xml version=”1.0” encoding=”UTF-8”?>

    <beacon:reference id=”helloService”

    1. interfaceName="com.xiaoyu.test.api.IHelloService" timeout="3000" retry="0"
    2. tolerant="failfast" group="dev" ></beacon:reference>

  1. 3. 创建启动类
  1. public static void main(String[] args) throws Exception {
  2. ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("classpath:beacon-client.xml");
  3. try {
  4. CountDownLatch latch = new CountDownLatch(1);
  5. // service调用
  6. IHelloService service = (IHelloService) context.getBean(IHelloService.class);
  7. System.out.println(service.hello("xiaoming"));
  8. latch.await();
  9. } finally {
  10. context.stop();
  11. context.close();
  12. }
  13. }
  1. ### 更多详细使用可见[beacon-example](https://github.com/dressrosa/beacon/tree/master/beacon-example)
  2. ## 配置
  3. ### consumer端常用配置:
  4. 1. beacon:protocol 配置协议
  5. 2. beacon:registry 配置注册中心
  6. 3. beacon:reference 配置接口引用
  7. **beacon:protocol:**
  8. | 字段名称 | 含义 | 备注 |
  9. | -------- | -------- | ------------------ |
  10. | name | 协议名称 | 支持beaconhttp |
  11. | port | 暴露端口 | 用于连接provider |
  12. **beacon:registry:**
  13. | 字段名称 | 含义 | 备注 |
  14. | -------- | ------------ | --------------------- |
  15. | protocol | 协议名称 | 支持zookeeper |
  16. | address | 注册中心地址 | 格式如:127.0.0.1:2181 |
  17. **beacon:reference:**
  18. | 字段名称 | 含义 | 备注 |
  19. | ------------- | ------------ | ------------------------------------------------ |
  20. | id | 接口id. | userService |
  21. | interfaceName | 接口名称 | com.xxx.com.xxx.UserService |
  22. | timeout | 接口超时时间 | 默认3000(ms) |
  23. | retry | 重试次数 | 默认0 |
  24. | tolerant | 容错机制 | 默认failfast 支持failfast,failover |
  25. | group | 接口分组 |
  26. | check | 启动时检查 | truefalse |
  27. | generic | 泛型接口 | truefalse |
  28. | downgrade | 降级策略 | 支持timeout:xxxretry:xxxfault:xxxlimit:xxx |
  29. ### provider端常用配置:
  30. ### provider端配置释义:
  31. 1. beacon:protocol 配置协议
  32. 2. beacon:registry 配置注册中心
  33. 3. beacon:exporter 配置接口暴露
  34. **beacon:protocol:**
  35. | 字段名称 | 含义 | 备注 |
  36. | -------- | -------- | ------------------ |
  37. | name | 协议名称 | 支持beaconhttp |
  38. | port | 暴露端口 | 用于连接provider |
  39. **beacon:registry:**
  40. | 字段名称 | 含义 | 备注 |
  41. | -------- | ------------ | --------------------- |
  42. | protocol | 协议名称 | 支持zookeeper |
  43. | address | 注册中心地址 | 格式如:127.0.0.1:2181 |
  44. **beacon:exporter:**
  45. | 字段名称 | 含义 | 备注 |
  46. | ------------- | -------------- | ---------------------------------- |
  47. | id | 接口id. | userService |
  48. | interfaceName | 接口名称 | com.xxx.UserService |
  49. | ref | 接口实现类 | com.xxx.UserServiceImpl |
  50. | methods | 接口暴露的方法 | 按逗号分隔,默认全部暴露 |
  51. | tolerant | 容错机制 | 默认failfast 支持failfast,failover |
  52. | group | 接口分组 |
  53. ## 泛型调用
  54. 泛型调用的作用是无需配置consumer信息来直接调用provider端方法.
  55. ### 使用方法
  56. 1. consumer端配置文件中<beacon:reference ></beacon:reference>中增加配置
  57. generic=true(默认false),来启动泛型调用.
  58. 2.beacon提供了一个通用泛型接口GenericService

public interface GenericService {

  1. /**
  2. * @param method
  3. * 需要调用的方法
  4. * @param returnType
  5. * 返回类型
  6. * @param args
  7. * 参数
  8. * @return
  9. */
  10. public Object $_$invoke(String method, Object returnType, Object[] args);

}

  1. 2.在需要调用的地方

public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(“classpath:beacon-client.xml”);
try {
CountDownLatch latch = new CountDownLatch(1);
// 泛型调用
GenericReference refer = new GenericReference();
//设置相关信息
refer.setInterfaceName(“com.xiaoyu.test.api.IHelloService”)
.setTimeout(“3000”)
.setGroup(“dev”);
GenericService generic = GenericRequestLauncher.launch(refer);
Object result = generic.$_$invoke(“hello”, String.class, new Object[] { “cat” });
System.out.println(“re:” + result);
latch.await();
} finally {
context.stop();
context.close();
}
}
```

使用Spring boot:

beacon-spring-boot-starter

目标:

健壮,扩展,优化细节.