Jupiter
v1.3.1
═ ═ ═▷ init ─ ─ ─ ▷ async ──────▶ sync
----------------------------------------------------------------------------------------
┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
┌ ─ ─ ─ ┐ │
─ ─ ─ ─ ─ ─ ─ ─ ─│ Registry Monitor ───────────────────────────┐
│ └ ─ ─ ─ ┘ │ │
└ ─ ─△─ ─ ─ ─ ─△─ ─ ─ ▼
│ ┌ ─ ─ ─ ─
Notify ║ ║ Telnet │
│ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ ═ └ ─ ─ ─ ─
║ ║ ▲
│ Subscribe Register │
║ ║ │
│ ┌ ─ ─ ─ ─ ─ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │
│─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ▷ ┌ ─ ─ ─ ┐ │ │
└ ▷│ Consumer Invoke │ Provider Monitor ─────┘
│────────────────────────▶ └ ─ ─ ─ ┘ │
└ ─ ─ ─ ─ ─ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
---------------------------------------------------------------------------------------
Terima kasih kepada @yuanmo karena telah menyediakan gambarnya
< dependency >
< groupId >org.jupiter-rpc</ groupId >
< artifactId >jupiter-all</ artifactId >
< version >${jupiter.version}</ version >
</ dependency >
@ ServiceProvider ( group = "test" , name = "serviceTest" )
public interface ServiceTest {
String sayHelloString ();
}
@ ServiceProvider :
- 建议每个服务接口通过此注解来指定服务信息, 如不希望业务代码对jupiter依赖也可以不使用此注解而手动去设置服务信息
+ group : 服务组别(选填, 默认组别为'Jupiter' )
+ name : 服务名称(选填, 默认名称为接口全限定名称)
@ ServiceProviderImpl ( version = "1.0.0" )
public class ServiceTestImpl implements ServiceTest {
@ Override
public String sayHelloString () {
return "Hello jupiter" ;
}
}
@ ServiceProviderImpl :
- 建议每个服务实现通过此注解来指定服务版本信息, 如不希望业务代码对jupiter依赖也可以不使用此注解而手动去设置版本信息
+ version : 服务版本号(选填, 默认版本号为'1.0.0' )
public class HelloJupiterRegistryServer {
public static void main ( String [] args ) {
// 注册中心
RegistryServer registryServer = RegistryServer . Default . createRegistryServer ( 20001 , 1 );
try {
registryServer . startRegistryServer ();
} catch ( InterruptedException e ) {
e . printStackTrace ();
}
}
}
默认注册中心只建议在测试环境使用, 线上建议使用 zookeeper 实现
// 设置使用 zookeeper 作为注册中心
JServer server = new DefaultServer(RegistryService.RegistryType.ZOOKEEPER)
JClient client = new DefaultClient(RegistryService.RegistryType.ZOOKEEPER)
在 server 和 client 中配置 jupiter-registry-zookeeper 依赖(jupiter-all 包含 jupiter-registry-zookeeper)
< dependency >
< groupId >org.jupiter-rpc</ groupId >
< artifactId >jupiter-registry-zookeeper</ artifactId >
< version >${jupiter.version}</ version >
</ dependency >
public class HelloJupiterServer {
public static void main ( String [] args ) throws Exception {
JServer server = new DefaultServer (). withAcceptor ( new JNettyTcpAcceptor ( 18090 ));
// provider
ServiceTestImpl service = new ServiceTestImpl ();
// 本地注册
ServiceWrapper provider = server . serviceRegistry ()
. provider ( service )
. register ();
// 连接注册中心
server . connectToRegistryServer ( "127.0.0.1:20001" );
// 向注册中心发布服务
server . publish ( provider );
// 启动server
server . start ();
}
}
public class HelloJupiterClient {
public static void main ( String [] args ) {
JClient client = new DefaultClient (). withConnector ( new JNettyTcpConnector ());
// 连接RegistryServer
client . connectToRegistryServer ( "127.0.0.1:20001" );
// 自动管理可用连接
JConnector . ConnectionWatcher watcher = client . watchConnections ( ServiceTest . class );
// 等待连接可用
if (! watcher . waitForAvailable ( 3000 )) {
throw new ConnectFailedException ();
}
ServiceTest service = ProxyFactory . factory ( ServiceTest . class )
. version ( "1.0.0" )
. client ( client )
. newProxyInstance ();
service . sayHelloString ();
}
}
Contoh kode Server/Klien
v1.3 menambahkan InvokeType.AUTO
. Jika nilai kembalian antarmuka Anda adalah CompletableFuture
atau subkelasnya, maka secara otomatis akan beradaptasi dengan panggilan asinkron. Jika tidak, itu akan menjadi panggilan sinkron.
< jupiter : server id = " jupiterServer " registryType = " default " > <!-- registryType="zookeeper" 代表使用zk作为注册中心 -->
< jupiter : property registryServerAddresses = " 127.0.0.1:20001 " />
</ jupiter : server >
<!-- provider -->
< bean id = " serviceTest " class = " org.jupiter.example.ServiceTestImpl " />
< jupiter : provider id = " serviceTestProvider " server = " jupiterServer " providerImpl = " serviceTest " >
< jupiter : property weight = " 100 " />
</ jupiter : provider >
< jupiter : client id = " jupiterClient " registryType = " default " > <!-- registryType="zookeeper" 代表使用zk作为注册中心 -->
< jupiter : property registryServerAddresses = " 127.0.0.1:20001 " />
</ jupiter : client >
<!-- consumer -->
< jupiter : consumer id = " serviceTest " client = " jupiterClient " interfaceClass = " org.jupiter.example.ServiceTest " >
< jupiter : property version = " 1.0.0.daily " />
< jupiter : property serializerType = " proto_stuff " />
< jupiter : property loadBalancerType = " round_robin " />
< jupiter : property timeoutMillis = " 3000 " />
< jupiter : property clusterStrategy = " fail_over " />
< jupiter : property failoverRetries = " 2 " />
< jupiter : methodSpecials >
<!-- 方法的单独配置 -->
< jupiter : methodSpecial methodName = " sayHello " timeoutMillis = " 5000 " clusterStrategy = " fail_fast " />
</ jupiter : methodSpecials >
</ jupiter : consumer >
Contoh kode SpringServer/SpringClient