Skip to content

Commit

Permalink
添加RpcContext
Browse files Browse the repository at this point in the history
  • Loading branch information
brucelwl committed Dec 21, 2020
1 parent 098e79d commit dc301d6
Show file tree
Hide file tree
Showing 26 changed files with 750 additions and 5 deletions.
33 changes: 33 additions & 0 deletions lightning-rpc-example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/
45 changes: 45 additions & 0 deletions lightning-rpc-example/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.bruce.rpc</groupId>
<artifactId>lightning-parent</artifactId>
<version>1.0</version>
</parent>

<artifactId>lightning-rpc-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<name>lightning-rpc-example</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<modules>
<module>rpc-provider</module>
<module>rpc-consumer</module>
<module>rpc-interface</module>
</modules>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.bruce.rpc</groupId>
<artifactId>lightning-rpc</artifactId>
<version>1.0</version>
</dependency>

<dependency>
<groupId>com.bruce.rpc</groupId>
<artifactId>rpc-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

</dependencies>
</dependencyManagement>

</project>
33 changes: 33 additions & 0 deletions lightning-rpc-example/rpc-consumer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/
66 changes: 66 additions & 0 deletions lightning-rpc-example/rpc-consumer/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.bruce.rpc</groupId>
<artifactId>lightning-rpc-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>rpc-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>rpc-consumer</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>

<dependency>
<groupId>com.bruce.rpc</groupId>
<artifactId>lightning-rpc</artifactId>
</dependency>

<dependency>
<groupId>com.bruce.rpc</groupId>
<artifactId>rpc-interface</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.lightning.rpc.example;

import com.bruce.lightning.rpc.spring.client.EnableNettyRpcClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableNettyRpcClient(host = "127.0.0.1",port = 8008)
@SpringBootApplication
public class LightningConsumerApplication {

public static void main(String[] args) {
SpringApplication.run(LightningConsumerApplication.class, args);


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.lightning.rpc.example.config;

import com.bruce.lightning.rpc.client.LightningClient;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;

//@Configuration
public class LightningRpcClientBootstrap implements ApplicationListener<ApplicationReadyEvent>, DisposableBean {

private LightningClient lightningClient;

public LightningClient getNettyRpcClient() {
return lightningClient;
}

@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
lightningClient = new LightningClient();
try {
lightningClient.startSync("127.0.0.1", 8088);
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void destroy() throws Exception {
if (lightningClient != null) {
lightningClient.close();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.lightning.rpc.example.consumer;

import com.bruce.lightning.rpc.spring.client.RpcReference;
import com.lightning.api.UserService;
import com.lightning.api.bean.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;

@RestController
@RequestMapping("/consumer")
public class RpcConsumerController {

@RpcReference
private UserService userService;

@GetMapping("/test")
public String test() {
User user = new User();
user.setId(123);
user.setName("bruce");

String s = userService.toString();
String s1 = userService.saveUsers(Collections.singletonList(user));

return s1;
}

@GetMapping("/test2")
public User test2() {
User user = new User();
user.setId(123);
user.setName("bruce");

return userService.saveUser(user);
}

@GetMapping("/test3")
public int test3() {
User user = new User();
user.setId(123);
user.setName("bruce");
return userService.updateUser(user, 18, "上海", true);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.lightning.rpc.example;


import com.lightning.api.UserService;
import com.lightning.api.bean.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(classes = LightningConsumerApplication.class)
public class ApplicationTest {

@Autowired
private UserService userService;

@Test
public void test1(){

User user = new User();
userService.saveUser(user);

}






}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.lightning.rpc.example;

import com.bruce.lightning.rpc.client.DefaultResponseFuture;
import com.bruce.lightning.rpc.client.LightningClient;
import com.bruce.lightning.rpc.common.RpcRequest;
import com.bruce.lightning.rpc.common.RpcResponse;
import com.bruce.lightning.rpc.util.JsonUtils;
import com.lightning.api.UserService;
import com.lightning.api.bean.User;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;

public class LightningClientTest {

@Test
public void test1() {
LightningClient client = new LightningClient();
client.startSync("127.0.0.1", 8008);

RpcRequest rpcRequest = new RpcRequest();
rpcRequest.setId(1L);
rpcRequest.setFullMethodName("com.bruce.use.service.UserService.saveUsers");

User user = new User();
user.setId(121);
user.setName("hahaha 中文");

ArrayList<User> users = new ArrayList<>();
users.add(user);
users.add(user);

rpcRequest.setArgs(new Object[]{users});

DefaultResponseFuture responseFuture = client.send(rpcRequest);
RpcResponse response = responseFuture.get();

System.out.println(JsonUtils.toJson(response));
}


@Test
public void test2() {
LightningClient client = new LightningClient();
client.startSync("127.0.0.1", 8008);
UserService userService = client.createProxy(UserService.class, 3000);

User user = new User();
user.setName("bruce");

userService.saveUser(user);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.lightning.rpc.example;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class LightningConsumerApplicationTests {

@Test
void contextLoads() {
}

}
Loading

0 comments on commit dc301d6

Please sign in to comment.