Skip to content

Commit 9c664f3

Browse files
zihongzihong
zihong
authored and
zihong
committed
添加基于Thrift的RPC调用方式
1 parent 03f7e66 commit 9c664f3

File tree

21 files changed

+2332
-6
lines changed

21 files changed

+2332
-6
lines changed

.gitignore

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
HELP.md
12
.gradle
2-
/build/
3+
build/
34
!gradle/wrapper/gradle-wrapper.jar
4-
/gradle/
5+
!**/src/main/**
6+
!**/src/test/**
57

68
### STS ###
79
.apt_generated
@@ -17,11 +19,14 @@
1719
*.iws
1820
*.iml
1921
*.ipr
20-
/out/
22+
out/
2123

2224
### NetBeans ###
2325
/nbproject/private/
2426
/nbbuild/
2527
/dist/
2628
/nbdist/
27-
/.nb-gradle/
29+
/.nb-gradle/
30+
31+
### VS Code ###
32+
.vscode/

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,21 @@ SpringBoot 使用Nsq消息中间件,此项目为消息消费者例子。
125125
### SpringBoot-Nacos-Config
126126
使用Nacos注册中心实现动态配置
127127

128+
### SpringBoot-SpringSecurity
129+
SpringBoot 基于Spring-Security实现资源的权限控制
130+
[SpringBoot2 集成SpringSecurity对资源访问认证](https://blog.csdn.net/qq_39211866/article/details/90273681)
131+
132+
### SpringBoot-Oauth2
133+
SpringBoot 基于Oauth2授权协议实现简化、密码、客户端模式.
134+
135+
### SpringBoot-Oauth2-Code
136+
SpringBoot 基于Oauth2授权协议实现完整的授权码登录流程.
137+
138+
### SpringBoot-Test-Starter
139+
SpringBoot 自动配置例子,了解SpringBoot的Starter原理。
140+
141+
### SpringBoot-Builder-Docker
142+
SpringBoot 使用Gradle做依赖管理时,配置打包docker镜像的DEMO
143+
128144
### SpringBoot-Utils
129145
各项目的依赖模块,存放一些工具类

SpringBoot-Thrift-Client/.gitignore

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
HELP.md
2+
.gradle
3+
build/
4+
!gradle/wrapper/gradle-wrapper.jar
5+
!**/src/main/**
6+
!**/src/test/**
7+
8+
### STS ###
9+
.apt_generated
10+
.classpath
11+
.factorypath
12+
.project
13+
.settings
14+
.springBeans
15+
.sts4-cache
16+
17+
### IntelliJ IDEA ###
18+
.idea
19+
*.iws
20+
*.iml
21+
*.ipr
22+
out/
23+
24+
### NetBeans ###
25+
/nbproject/private/
26+
/nbbuild/
27+
/dist/
28+
/nbdist/
29+
/.nb-gradle/
30+
31+
### VS Code ###
32+
.vscode/

SpringBoot-Thrift-Client/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
dependencies {
2+
compile project(':SpringBoot-Utils')
3+
compile 'org.apache.thrift:libthrift:0.12.0'
4+
testCompile('org.springframework.boot:spring-boot-starter-test:2.0.4.RELEASE')
5+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.dashuai.learning.thrift;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class ThriftClientApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(ThriftClientApplication.class, args);
11+
}
12+
13+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.dashuai.learning.thrift.api;
2+
3+
import com.dashuai.learning.thrift.client.RPCThriftClient;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RequestMethod;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
import javax.servlet.http.HttpServletRequest;
12+
import javax.servlet.http.HttpServletResponse;
13+
14+
@RestController
15+
@RequestMapping("/test")
16+
public class RPCThriftController {
17+
private final Logger logger = LoggerFactory.getLogger(RPCThriftController.class);
18+
@Autowired
19+
RPCThriftClient rpcThriftClient;
20+
21+
@RequestMapping(value = "/thrift", method = RequestMethod.GET)
22+
public String thriftTest(HttpServletRequest request, HttpServletResponse response) {
23+
try {
24+
rpcThriftClient.open();
25+
return rpcThriftClient.getRPCThriftService().getDate("大帥");
26+
} catch (Exception e) {
27+
logger.error("RPC调用失败", e);
28+
return "error";
29+
} finally {
30+
rpcThriftClient.close();
31+
}
32+
}
33+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.dashuai.learning.thrift.client;
2+
3+
import com.dashuai.learning.thrift.service.RPCDateService;
4+
import org.apache.thrift.protocol.TBinaryProtocol;
5+
import org.apache.thrift.transport.TSocket;
6+
import org.apache.thrift.transport.TTransportException;
7+
8+
public class RPCThriftClient {
9+
private RPCDateService.Client client;
10+
private TBinaryProtocol protocol;
11+
private TSocket transport;
12+
private String host;
13+
private int port;
14+
15+
public String getHost() {
16+
return host;
17+
}
18+
public void setHost(String host) {
19+
this.host = host;
20+
}
21+
public int getPort() {
22+
return port;
23+
}
24+
public void setPort(int port) {
25+
this.port = port;
26+
}
27+
28+
public void init() {
29+
transport = new TSocket(host, port);
30+
protocol = new TBinaryProtocol(transport);
31+
client = new RPCDateService.Client(protocol);
32+
}
33+
34+
public RPCDateService.Client getRPCThriftService() {
35+
return client;
36+
}
37+
38+
public void open() throws TTransportException {
39+
transport.open();
40+
}
41+
42+
public void close() {
43+
transport.close();
44+
}
45+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.dashuai.learning.thrift.config;
2+
3+
import com.dashuai.learning.thrift.client.RPCThriftClient;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
8+
@Configuration
9+
public class RPCThriftClientConfig {
10+
@Value("${thrift.host}")
11+
private String host;
12+
@Value("${thrift.port}")
13+
private int port;
14+
15+
@Bean(initMethod = "init")
16+
public RPCThriftClient rpcThriftClient() {
17+
RPCThriftClient rpcThriftClient = new RPCThriftClient();
18+
rpcThriftClient.setHost(host);
19+
rpcThriftClient.setPort(port);
20+
return rpcThriftClient;
21+
}
22+
23+
}

0 commit comments

Comments
 (0)