core


License

License

GroupId

GroupId

com.github.bdqfork
ArtifactId

ArtifactId

hamal-core
Last Version

Last Version

0.1.1
Release Date

Release Date

Type

Type

jar
Description

Description

core
core
Project URL

Project URL

https://github.com/bdqfork/Hamal
Source Code Management

Source Code Management

https://github.com/bdqfork/Hamal

Download hamal-core

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.bdqfork/hamal-core/ -->
<dependency>
    <groupId>com.github.bdqfork</groupId>
    <artifactId>hamal-core</artifactId>
    <version>0.1.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.bdqfork/hamal-core/
implementation 'com.github.bdqfork:hamal-core:0.1.1'
// https://jarcasting.com/artifacts/com.github.bdqfork/hamal-core/
implementation ("com.github.bdqfork:hamal-core:0.1.1")
'com.github.bdqfork:hamal-core:jar:0.1.1'
<dependency org="com.github.bdqfork" name="hamal-core" rev="0.1.1">
  <artifact name="hamal-core" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.bdqfork', module='hamal-core', version='0.1.1')
)
libraryDependencies += "com.github.bdqfork" % "hamal-core" % "0.1.1"
[com.github.bdqfork/hamal-core "0.1.1"]

Dependencies

compile (1)

Group / Artifact Type Version
org.slf4j : slf4j-api jar 1.7.25

runtime (2)

Group / Artifact Type Version
org.javassist : javassist jar 3.26.0-GA
com.caucho : hessian jar 4.0.63

Project Modules

There are no modules declared in this project.

一个基于netty的轻量级rpc框架,使用方式可参考example。

Features:

  • 基于netty实现rpc协议
  • 支持端到端的rpc调用
  • 支持使用SPI进行功能扩展

GetStart

引入依赖

implementation "com.github.bdqfork:hamal-context:0.1.1"

定义接口,例如UserService

public interface UserService {
    User getUser(Long id);
}

在服务端实现接口

public class UserServiceImpl implements UserService {
    @Override
    public User getUser(Long id) {
        User user = new User();
        user.setId(id);
        user.setUsername("testRpc");
        user.setPassword("testpass");
        return user;
    }
}

初始化上下文,启动服务端

public class Main {
    public static void main(String[] args) throws Exception {
        ApplicationConfig applicationConfig = new ApplicationConfig("127.0.0.1", 8081, "rpc", "netty");
        applicationConfig.setContainer("rpc");
        applicationConfig.setDirect(true);
        applicationConfig.setLoadbalancer("random");
        applicationConfig.setSerilizer("hessian");
        Bootstrap bootstrap = new Bootstrap(applicationConfig);
        ServiceConfig<UserService> serviceConfig = new ServiceConfig<>(UserService.class);
        bootstrap.registerService(new UserServiceImpl(), serviceConfig);
        bootstrap.open();
        CountDownLatch latch = new CountDownLatch(1);
        latch.await();
    }
}

客户端同样初始化上下文,但不需要open服务器

public class Main {
    public static void main(String[] args) throws Exception {
        ApplicationConfig applicationConfig = new ApplicationConfig("127.0.0.1", 8081, "rpc", "netty");
        applicationConfig.setContainer("rpc");
        applicationConfig.setDirect(true);
        applicationConfig.setLoadbalancer("random");
        applicationConfig.setSerilizer("hessian");
        Bootstrap bootstrap = new Bootstrap(applicationConfig);
        ReferenceConfig<?> referenceConfig = new ReferenceConfig<>(UserService.class);
        referenceConfig.setConnections(2);

        UserService userService = (UserService) bootstrap.getProxy(referenceConfig);
        ThreadPoolExecutor executor = new ThreadPoolExecutor(40, 50, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<>(512),
                new ThreadPoolExecutor.DiscardPolicy());
        while (true) {
            executor.execute(() -> {
                User user = userService.getUser(1L);
                System.out.println(user);
            });
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

注册中心

支持使用zookeeper作为注册中心

RegistryConfig registryConfig = new RegistryConfig("zookeeper", "127.0.0.1:8080");
Bootstrap bootstrap = new Bootstrap(applicationConfig, registryConfig);

序列化

支持hessian和json两种序列号方式

applicationConfig.setSerilizer("hessian");

or

applicationConfig.setSerilizer("json");

协议

默认使用基于tcp的自定义rpc协议,支持http协议

ApplicationConfig applicationConfig = new ApplicationConfig("127.0.0.1", 8081, "http", "netty");

todolist:

  • 实现服务分组功能
  • 实现超时重试策略
  • 实现快速失败策略
  • 实现常见的负载均衡算法
  • 实现Filter
  • 支持异步调用
  • 添加log信息

Versions

Version
0.1.1
0.1.0