Skip to content

Commit 050c5e2

Browse files
committed
2 parents 4e4c045 + 7b848b1 commit 050c5e2

File tree

8 files changed

+349
-0
lines changed

8 files changed

+349
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<artifactId>dynamic-tp-example-adapter</artifactId>
7+
<groupId>org.dromara.dynamictp</groupId>
8+
<version>${revision}</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<artifactId>dynamic-tp-example-adapter-thrift</artifactId>
12+
13+
<properties>
14+
<maven.deploy.skip>true</maven.deploy.skip>
15+
</properties>
16+
17+
<dependencyManagement>
18+
<dependencies>
19+
<dependency>
20+
<groupId>com.alibaba.cloud</groupId>
21+
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
22+
<version>2021.1</version>
23+
<type>pom</type>
24+
<scope>import</scope>
25+
</dependency>
26+
</dependencies>
27+
</dependencyManagement>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-web</artifactId>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>org.springframework.boot</groupId>
37+
<artifactId>spring-boot-starter-logging</artifactId>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>org.dromara.dynamictp</groupId>
42+
<artifactId>dynamic-tp-spring-boot-starter-adapter-thrift</artifactId>
43+
<version>${revision}</version>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>org.apache.thrift</groupId>
48+
<artifactId>libthrift</artifactId>
49+
<version>0.21.0</version>
50+
</dependency>
51+
52+
<dependency>
53+
<groupId>org.springframework.cloud</groupId>
54+
<artifactId>spring-cloud-starter-bootstrap</artifactId>
55+
</dependency>
56+
57+
<dependency>
58+
<groupId>com.alibaba.cloud</groupId>
59+
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
60+
</dependency>
61+
62+
<dependency>
63+
<groupId>com.alibaba.cloud</groupId>
64+
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
65+
</dependency>
66+
</dependencies>
67+
</project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.dromara.dynamictp.example;
19+
20+
import org.dromara.dynamictp.spring.annotation.EnableDynamicTp;
21+
import org.springframework.boot.SpringApplication;
22+
import org.springframework.boot.autoconfigure.SpringBootApplication;
23+
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
24+
25+
/**
26+
* @author devin
27+
* @since 1.1.5
28+
*/
29+
@EnableDynamicTp
30+
@SpringBootApplication(exclude = RedisAutoConfiguration.class)
31+
public class ThriftExampleApplication {
32+
public static void main(String[] args) {
33+
SpringApplication.run(ThriftExampleApplication.class, args);
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.dromara.dynamictp.example.controller;
19+
20+
import org.dromara.dynamictp.example.thrift.ThriftClientService;
21+
import lombok.extern.slf4j.Slf4j;
22+
import org.springframework.web.bind.annotation.GetMapping;
23+
import org.springframework.web.bind.annotation.RestController;
24+
25+
import javax.annotation.Resource;
26+
27+
/**
28+
* @author devin
29+
* @since 1.1.5
30+
*/
31+
@Slf4j
32+
@RestController
33+
@SuppressWarnings("all")
34+
public class TestController {
35+
36+
@Resource
37+
private ThriftClientService thriftClientService;
38+
39+
@GetMapping("/dtp-example-adapter/testThrift")
40+
public String testThrift() {
41+
return thriftClientService.sendMessage("test dynamic tp");
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.dromara.dynamictp.example.thrift;
19+
20+
import lombok.extern.slf4j.Slf4j;
21+
import org.apache.thrift.TException;
22+
import org.apache.thrift.protocol.TBinaryProtocol;
23+
import org.apache.thrift.protocol.TProtocol;
24+
import org.apache.thrift.transport.TSocket;
25+
import org.apache.thrift.transport.TTransport;
26+
import org.dromara.dynamictp.example.thrift.service.SimpleService;
27+
import org.springframework.beans.factory.annotation.Value;
28+
import org.springframework.stereotype.Service;
29+
30+
/**
31+
* ThriftClientService related
32+
*
33+
* @author devin
34+
* @since 1.1.5
35+
*/
36+
@Service
37+
@Slf4j
38+
public class ThriftClientService {
39+
40+
@Value("${thrift.server.port:9998}")
41+
private int serverPort;
42+
43+
@Value("${thrift.server.host:localhost}")
44+
private String serverHost;
45+
46+
public String sendMessage(final String name) {
47+
try (TTransport transport = new TSocket(serverHost, serverPort)) {
48+
transport.open();
49+
TProtocol protocol = new TBinaryProtocol(transport);
50+
SimpleService.Client client = new SimpleService.Client(protocol);
51+
return client.sayHello(name);
52+
} catch (TException e) {
53+
log.error("Request failed", e);
54+
return "FAILED with " + e.getMessage();
55+
}
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.dromara.dynamictp.example.thrift;
19+
20+
import org.apache.thrift.TProcessor;
21+
import org.apache.thrift.protocol.TBinaryProtocol;
22+
import org.apache.thrift.server.TThreadPoolServer;
23+
import org.apache.thrift.transport.TServerSocket;
24+
import org.apache.thrift.transport.TTransportException;
25+
import org.dromara.dynamictp.example.thrift.service.SimpleService;
26+
import org.springframework.beans.factory.annotation.Value;
27+
import org.springframework.stereotype.Service;
28+
29+
import javax.annotation.PostConstruct;
30+
import javax.annotation.PreDestroy;
31+
import java.util.concurrent.ExecutorService;
32+
import java.util.concurrent.Executors;
33+
34+
/**
35+
* ThriftServerService related
36+
*
37+
* @author devin
38+
* @since 1.1.5
39+
*/
40+
@Service
41+
public class ThriftServerService implements SimpleService.Iface {
42+
43+
@Value("${thrift.server.port:9998}")
44+
private int serverPort;
45+
46+
private TThreadPoolServer server;
47+
private ExecutorService executorService;
48+
49+
@PostConstruct
50+
public void start() {
51+
executorService = Executors.newSingleThreadExecutor();
52+
executorService.submit(() -> {
53+
try {
54+
TServerSocket serverTransport = new TServerSocket(serverPort);
55+
TProcessor processor = new SimpleService.Processor<>(this);
56+
TThreadPoolServer.Args args = new TThreadPoolServer.Args(serverTransport)
57+
.processor(processor)
58+
.protocolFactory(new TBinaryProtocol.Factory());
59+
server = new TThreadPoolServer(args);
60+
System.out.println("Starting Thrift server on port " + serverPort);
61+
server.serve();
62+
} catch (TTransportException e) {
63+
e.printStackTrace();
64+
}
65+
});
66+
}
67+
68+
@PreDestroy
69+
public void stop() {
70+
if (server != null) {
71+
server.stop();
72+
}
73+
if (executorService != null) {
74+
executorService.shutdown();
75+
}
76+
}
77+
78+
@Override
79+
public String sayHello(String name) {
80+
return "Hello ==> " + name;
81+
}
82+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
server:
2+
port: 9104
3+
4+
spring:
5+
application:
6+
name: dynamic-tp-adapter-thrift-demo
7+
profiles:
8+
active: dev
9+
cloud:
10+
nacos:
11+
discovery:
12+
server-addr: localhost:8848
13+
config:
14+
server-addr: ${spring.cloud.nacos.discovery.server-addr}
15+
file-extension: yml
16+
extension-configs:
17+
- dataId: ${spring.application.name}-dtp-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
18+
group: DEFAULT_GROUP
19+
refresh: true # 必须配置,负责自动刷新不生效
20+
refresh-enabled: true
21+
22+
dynamictp:
23+
enabled: true
24+
enabledCollect: true # 是否开启监控指标采集,默认false
25+
collectorTypes: logging # 监控数据采集器类型(logging | micrometer | internal_logging),默认micrometer
26+
monitorInterval: 5
27+
platforms: # 通知报警平台配置
28+
- platform: wechat
29+
urlKey: 3a7500-1287-4bd-a798-c5c3d8b69c # 替换
30+
receivers: test1,test2 # 接受人企微名称
31+
- platform: ding
32+
urlKey: f80dad441fcd655438f4a08dcd6a # 替换
33+
secret: SECb5441fa6f375d5b9d21 # 替换,非sign模式可以没有此值
34+
receivers: 15810119805 # 钉钉账号手机号
35+
- platform: lark
36+
urlKey: 0d944ae7-b24a-40 # 替换
37+
receivers: test1,test2 # 接受人飞书名称/openid
38+
thriftTp: # thrift 线程池配置
39+
- threadPoolName: thriftTp#TThreadPoolServer#9998
40+
corePoolSize: 10
41+
maximumPoolSize: 20
42+
keepAliveTime: 60
43+
44+
thrift:
45+
server:
46+
port: 9998
47+
host: localhost
48+
49+
# 开启 SpringBoot Actuator Endpoint 暴露出DynamicTp指标接口
50+
# 开启 prometheus 指标采集端点
51+
management:
52+
metrics:
53+
export:
54+
prometheus:
55+
enabled: true
56+
endpoints:
57+
web:
58+
exposure:
59+
include: '*' # 测试使用,线上不要用*,按需开启
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace java org.dromara.dynamictp.example.thrift.service
2+
3+
service SimpleService {
4+
string sayHello(1: string name)
5+
}

example/example-adapter/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<module>example-adapter-rabbitmq</module>
2727
<module>example-adapter-liteflow</module>
2828
<module>example-adapter-webserver</module>
29+
<module>example-adapter-thrift</module>
2930
</modules>
3031

3132
<dependencies>

0 commit comments

Comments
 (0)