-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
485 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
demo-consumer/src/main/java/com/lyz/demo/consumer/HelloController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.lyz.demo.consumer; | ||
|
||
import com.lyz.demo.rpc.service.HelloService; | ||
import com.lyz.rpc.consumer.RpcReference; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import reactor.core.publisher.Mono; | ||
|
||
@RestController | ||
public class HelloController { | ||
@RpcReference | ||
private HelloService helloService; | ||
|
||
@GetMapping({ "/", "" }) | ||
public Mono<?> demo(@RequestParam String hello) { | ||
return Mono.fromCallable(() -> helloService.demo(hello)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,5 @@ rpc: | |
port: 8070 | ||
consumer: | ||
enabled: true | ||
server: | ||
port: 8072 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 0 additions & 8 deletions
8
demo-facade/src/main/java/com/lyz/demo/facade/TestService.java
This file was deleted.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
demo-facade/src/main/java/com/lyz/demo/rpc/service/HelloService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.lyz.demo.rpc.service; | ||
|
||
/** | ||
* @author liyizhen | ||
* @date 2022/3/17 | ||
*/ | ||
public interface HelloService { | ||
String demo(String hello); | ||
} |
18 changes: 18 additions & 0 deletions
18
demo-provider/src/main/java/com/lyz/demo/provider/HelloServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.lyz.demo.provider; | ||
|
||
import com.lyz.demo.rpc.service.HelloService; | ||
import com.lyz.rpc.provider.RpcService; | ||
|
||
import java.util.Random; | ||
|
||
/** | ||
* @author liyizhen | ||
* @date 2022/3/17 | ||
*/ | ||
@RpcService(interfaceClass = HelloService.class, version = "1.0.0") | ||
public class HelloServiceImpl implements HelloService { | ||
@Override | ||
public String demo(String hello) { | ||
return new Random().nextInt() + hello; | ||
} | ||
} |
12 changes: 0 additions & 12 deletions
12
demo-provider/src/main/java/com/lyz/demo/provider/TestServiceImpl.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,6 @@ | |
<name>rpc-framework</name> | ||
|
||
<properties> | ||
<java.version>11</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
|
43 changes: 38 additions & 5 deletions
43
rpc-framework/src/main/java/com/lyz/rpc/consumer/Consumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,63 @@ | ||
package com.lyz.rpc.consumer; | ||
|
||
import com.lyz.rpc.core.InstanceInfo; | ||
import com.lyz.rpc.protocol.Protocol; | ||
import com.lyz.rpc.protocol.ProtocolDecoder; | ||
import com.lyz.rpc.protocol.ProtocolEncoder; | ||
import com.lyz.rpc.protocol.ProtocolResponseHandler; | ||
import com.lyz.rpc.registry.RegistryService; | ||
import io.netty.bootstrap.Bootstrap; | ||
import io.netty.channel.Channel; | ||
import io.netty.channel.ChannelFuture; | ||
import io.netty.channel.ChannelInitializer; | ||
import io.netty.channel.nio.NioEventLoopGroup; | ||
import io.netty.channel.socket.SocketChannel; | ||
import io.netty.channel.socket.nio.NioSocketChannel; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/** | ||
* 消费器,利用 netty 对目标进行连接 | ||
* @author liyizhen | ||
* @date 2022/3/18 | ||
*/ | ||
@Slf4j | ||
public class Consumer { | ||
public Consumer() { | ||
Bootstrap bootstrap = new Bootstrap(); | ||
NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup(); | ||
bootstrap.group(eventLoopGroup) | ||
private final RegistryService registryService; | ||
private final Bootstrap bootstrap; | ||
private final NioEventLoopGroup nioEventLoopGroup; | ||
|
||
public Consumer(RegistryService registryService) { | ||
this.registryService = registryService; | ||
|
||
bootstrap = new Bootstrap(); | ||
nioEventLoopGroup = new NioEventLoopGroup(); | ||
bootstrap.group(nioEventLoopGroup) | ||
.channel(NioSocketChannel.class) | ||
.handler(new ChannelInitializer<SocketChannel>() { | ||
@Override | ||
protected void initChannel(SocketChannel ch) throws Exception { | ||
ch.pipeline().addLast(new ProtocolDecoder()); | ||
ch.pipeline().addLast(new ProtocolEncoder()); | ||
ch.pipeline().addLast(new ProtocolResponseHandler()); | ||
} | ||
}); | ||
} | ||
|
||
public void request() { | ||
public void request(Protocol<Protocol.Request> protocol) throws InterruptedException { | ||
// 发现服务 | ||
InstanceInfo instanceInfo = registryService.discovery(); | ||
|
||
// 发起调用 | ||
ChannelFuture channelFuture = bootstrap.connect(instanceInfo.getHost(), instanceInfo.getPort()).sync(); | ||
channelFuture.addListener(arg0 -> { | ||
if (channelFuture.isSuccess()) { | ||
log.info("成功调用目标 host:{},port:{}", instanceInfo.getHost(), instanceInfo.getPort()); | ||
} else { | ||
log.info("调用目标失败 host:{},port:{}", instanceInfo.getHost(), instanceInfo.getPort()); | ||
channelFuture.cause().printStackTrace(); | ||
nioEventLoopGroup.shutdownGracefully(); | ||
} | ||
}); | ||
channelFuture.channel().writeAndFlush(protocol); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
rpc-framework/src/main/java/com/lyz/rpc/consumer/RequestHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.lyz.rpc.consumer; | ||
|
||
import com.lyz.rpc.protocol.Protocol; | ||
import io.netty.util.concurrent.Promise; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
public class RequestHolder { | ||
private static final AtomicLong ID_GENERATOR = new AtomicLong(); | ||
private static final ConcurrentHashMap<Long, Promise<Protocol.Response>> PROMISES = new ConcurrentHashMap<>(); | ||
|
||
public static long generateId() { | ||
return ID_GENERATOR.incrementAndGet(); | ||
} | ||
|
||
public static void putRequest(long requestId, Promise<Protocol.Response> promise) { | ||
PROMISES.put(requestId, promise); | ||
} | ||
|
||
public static void successRequest(long requestId, Protocol.Response result) { | ||
Promise<Protocol.Response> promise = PROMISES.get(requestId); | ||
if (promise == null) { | ||
throw new IllegalArgumentException("promise 为空 requestId:" + requestId); | ||
} | ||
promise.setSuccess(result); | ||
PROMISES.remove(requestId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.