childPath;
49 |
50 | childPath = zk.getChildren(servicePath, false);
51 |
52 | if (childPath.size() == 0) {
53 | logger.error("%s address node is not exsited", serviceName);
54 | throw (new Exception("地址未找到"));
55 | }
56 | String addressPath = servicePath + "/";
57 | if (childPath.size() == 1) {
58 | addressPath += childPath.get(0);
59 | }
60 | if (childPath.size() > 1) {
61 | addressPath += childPath.get((int) (Math.random() * childPath.size()));
62 | }
63 | logger.debug("address node is " + addressPath);
64 | byte[] data = zk.getData(addressPath, null, null);
65 | return new String(data);
66 | } catch (Exception e) {
67 | logger.error("get data failure", e);
68 | }
69 | return "";
70 | }
71 |
72 | @Override
73 | public void process(WatchedEvent watchedEvent) {
74 | if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
75 | if (Event.EventType.None == watchedEvent.getType() && null == watchedEvent.getPath()) {
76 | latch.countDown();
77 | }
78 | else if(watchedEvent.getType()== Event.EventType.NodeChildrenChanged){
79 | try{
80 | System.out.print("reGet child:"+zk.getChildren(watchedEvent.getPath(),true));
81 | }
82 | catch (Exception ex){
83 | ex.printStackTrace();
84 | }
85 | }
86 | }
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/spring-zookeeper-invoker/src/main/java/com/hks/springzookeeperweb/controller/ZookeeperRead.java:
--------------------------------------------------------------------------------
1 | package com.hks.springzookeeperweb.controller;
2 |
3 | import com.hks.springzookeeperweb.consumer.ServiceHelp;
4 | import com.hks.springzookeeperweb.consumer.impl.ServiceHelpImpl;
5 | import org.springframework.stereotype.Controller;
6 | import org.springframework.web.bind.annotation.RequestMapping;
7 | import org.springframework.web.bind.annotation.RequestMethod;
8 | import org.springframework.web.bind.annotation.ResponseBody;
9 |
10 | /**
11 | * @Author: hekuangsheng
12 | * @Date: 2018/10/31
13 | */
14 | @Controller
15 | public class ZookeeperRead {
16 |
17 | @RequestMapping(method = RequestMethod.GET, path = "/read")
18 | @ResponseBody
19 | public String hello() {
20 | ServiceHelp serviceHelp = new ServiceHelpImpl("127.0.0.1:2181");
21 | String data = serviceHelp.getData("HelloService");
22 | return data;
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/spring-zookeeper-invoker/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | 2server.port=8082
2 |
--------------------------------------------------------------------------------
/spring-zookeeper-invoker/src/test/java/com/hks/springzookeeperweb/SpringZookeeperWebApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.hks.springzookeeperweb;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.test.context.junit4.SpringRunner;
7 |
8 | @RunWith(SpringRunner.class)
9 | @SpringBootTest
10 | public class SpringZookeeperWebApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/spring-zookeeper-register/.gitignore:
--------------------------------------------------------------------------------
1 | /target/
2 | !.mvn/wrapper/maven-wrapper.jar
3 |
4 | ### STS ###
5 | .apt_generated
6 | .classpath
7 | .factorypath
8 | .project
9 | .settings
10 | .springBeans
11 | .sts4-cache
12 |
13 | ### IntelliJ IDEA ###
14 | .idea
15 | *.iws
16 | *.iml
17 | *.ipr
18 |
19 | ### NetBeans ###
20 | /nbproject/private/
21 | /build/
22 | /nbbuild/
23 | /dist/
24 | /nbdist/
25 | /.nb-gradle/
--------------------------------------------------------------------------------
/spring-zookeeper-register/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | com.hks
7 | spring-zookeeper-register
8 | 0.0.1-SNAPSHOT
9 | jar
10 |
11 | spring-zookeeper-register
12 | spring-zookeeper-register project for Spring Boot
13 |
14 |
15 | org.springframework.boot
16 | spring-boot-starter-parent
17 | 2.0.6.RELEASE
18 |
19 |
20 |
21 |
22 | UTF-8
23 | UTF-8
24 | 1.8
25 |
26 |
27 |
28 |
29 | org.springframework.boot
30 | spring-boot-starter-web
31 |
32 |
33 |
34 | org.apache.zookeeper
35 | zookeeper
36 | 3.4.13
37 |
38 |
39 |
40 |
41 | org.projectlombok
42 | lombok
43 | 1.18.2
44 | provided
45 |
46 |
47 |
48 | com.google.code.gson
49 | gson
50 | 2.8.2
51 |
52 |
53 |
54 |
55 | org.apache.curator
56 | curator-recipes
57 | 4.0.1
58 |
59 |
60 |
61 | org.springframework.boot
62 | spring-boot-starter-test
63 | test
64 |
65 |
66 |
67 |
68 |
69 |
70 | org.springframework.boot
71 | spring-boot-maven-plugin
72 |
73 |
74 |
75 |
76 |
77 |
78 |
--------------------------------------------------------------------------------
/spring-zookeeper-register/src/main/java/com/hks/springzookeeper/SpringZookeeperApplication.java:
--------------------------------------------------------------------------------
1 | package com.hks.springzookeeper;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class SpringZookeeperApplication {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(SpringZookeeperApplication.class, args);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/spring-zookeeper-register/src/main/java/com/hks/springzookeeper/config/RegistryConfig.java:
--------------------------------------------------------------------------------
1 | package com.hks.springzookeeper.config;
2 |
3 | import com.hks.springzookeeper.register.ServiceRegistry;
4 | import com.hks.springzookeeper.register.impl.ServiceRegistryImpl;
5 | import org.springframework.beans.factory.annotation.Value;
6 | import org.springframework.boot.context.properties.ConfigurationProperties;
7 | import org.springframework.context.annotation.Bean;
8 | import org.springframework.context.annotation.Configuration;
9 |
10 | /**
11 | * @Author: hekuangsheng
12 | * @Date: 2018/10/31
13 | *
14 | * 创建一个配置类来读取application.properties的配置
15 | */
16 | @Configuration
17 | @ConfigurationProperties(prefix="registry")
18 | public class RegistryConfig {
19 |
20 | @Value("registry.servers")
21 | private String servers;
22 |
23 | @Bean
24 | public ServiceRegistry serviceRegistry(){
25 | return new ServiceRegistryImpl(servers);
26 | }
27 |
28 | public String getServers() {
29 | return servers;
30 | }
31 |
32 | public void setServers(String servers) {
33 | this.servers = servers;
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/spring-zookeeper-register/src/main/java/com/hks/springzookeeper/config/WebListener.java:
--------------------------------------------------------------------------------
1 | package com.hks.springzookeeper.config;
2 |
3 | import com.hks.springzookeeper.register.ServiceRegistry;
4 | import org.slf4j.Logger;
5 | import org.slf4j.LoggerFactory;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.beans.factory.annotation.Value;
8 | import org.springframework.context.ApplicationContext;
9 | import org.springframework.stereotype.Component;
10 | import org.springframework.web.context.support.WebApplicationContextUtils;
11 | import org.springframework.web.method.HandlerMethod;
12 | import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
13 | import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
14 |
15 | import javax.servlet.ServletContext;
16 | import javax.servlet.ServletContextEvent;
17 | import javax.servlet.ServletContextListener;
18 | import java.util.Map;
19 |
20 | /**
21 | * @Author: hekuangsheng
22 | * @Date: 2018/10/31
23 | *
24 | * 由于我们的服务是以javaweb的形式发布的。
25 | * 所以在每个应用初始化时,去完成接口的注册是一个好时机。
26 | * 因此我们新加一个类去实现ServletContextListener,并把这个类交给spring来管理。
27 | */
28 | @Component
29 | public class WebListener implements ServletContextListener {
30 |
31 | private static Logger logger = LoggerFactory
32 | .getLogger(WebListener.class);
33 | @Value("${server.address}")
34 | private String serverAddress;
35 | @Value("${server.port}")
36 | private int serverPort;
37 |
38 | @Autowired
39 | private ServiceRegistry serviceRegistry;
40 |
41 | @Override
42 | public void contextInitialized(ServletContextEvent servletContextEvent) {
43 | //获取请求映射
44 | ServletContext servletContext = servletContextEvent.getServletContext();
45 | ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
46 | RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
47 | Map infoMap = mapping.getHandlerMethods();
48 | for (RequestMappingInfo info : infoMap.keySet()) {
49 | String serviceName = info.getName();
50 | logger.debug("-----------" + serviceName);
51 | if (null != serviceName) {
52 | serviceRegistry.register(serviceName, String.format("%s:%d/hello", serverAddress, serverPort));
53 | }
54 | }
55 | }
56 |
57 | @Override
58 | public void contextDestroyed(ServletContextEvent servletContextEvent) {
59 |
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/spring-zookeeper-register/src/main/java/com/hks/springzookeeper/controller/DemoTest.java:
--------------------------------------------------------------------------------
1 | package com.hks.springzookeeper.controller;
2 |
3 | import org.springframework.stereotype.Controller;
4 | import org.springframework.web.bind.annotation.RequestMapping;
5 | import org.springframework.web.bind.annotation.RequestMethod;
6 |
7 | /**
8 | * @Author: hekuangsheng
9 | * @Date: 2018/10/31
10 | */
11 | @Controller
12 | public class DemoTest {
13 |
14 | @RequestMapping(name = "HelloService", method = RequestMethod.GET, path = "/hello")
15 | public String hello() {
16 | return "hello word";
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/spring-zookeeper-register/src/main/java/com/hks/springzookeeper/controller/RestBizController.java:
--------------------------------------------------------------------------------
1 | package com.hks.springzookeeper.controller;
2 |
3 | import org.apache.zookeeper.WatchedEvent;
4 | import org.apache.zookeeper.Watcher;
5 | import org.apache.zookeeper.ZooKeeper;
6 | import org.springframework.web.bind.annotation.RequestMapping;
7 | import org.springframework.web.bind.annotation.RequestMethod;
8 | import org.springframework.web.bind.annotation.RestController;
9 |
10 | /**
11 | * @Author: hekuangsheng
12 | * @Date: 2018/10/30
13 | */
14 | @RestController
15 | public class RestBizController {
16 |
17 | @RequestMapping(value = "/zkget", method = RequestMethod.GET)
18 | public String zkget() {
19 | Watcher watcher = new Watcher() {
20 | public void process(WatchedEvent event) {
21 | System.out.println("receive event:" + event);
22 | }
23 | };
24 |
25 | String value = null;
26 | try {
27 | final ZooKeeper zookeeper = new ZooKeeper("127.0.0.1:2181", 999999, watcher);
28 | final byte[] data = zookeeper.getData("/node_1", watcher, null);
29 | value = new String(data);
30 | zookeeper.close();
31 | } catch (Exception e) {
32 | e.printStackTrace();
33 | }
34 |
35 | return "get value from zookeeper [" + value + "]";
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/spring-zookeeper-register/src/main/java/com/hks/springzookeeper/register/ServiceRegistry.java:
--------------------------------------------------------------------------------
1 | package com.hks.springzookeeper.register;
2 |
3 | /**
4 | * @Author: hekuangsheng
5 | * @Date: 2018/10/31
6 | *
7 | * 定义服务注册表接口
8 | */
9 | public interface ServiceRegistry {
10 |
11 | /**
12 | * 注册服务信息
13 | * @param serviceName 服务名称
14 | * @param serviceAddress 服务地址
15 | */
16 | void register(String serviceName,String serviceAddress);
17 | }
18 |
--------------------------------------------------------------------------------
/spring-zookeeper-register/src/main/java/com/hks/springzookeeper/register/impl/ServiceRegistryImpl.java:
--------------------------------------------------------------------------------
1 | package com.hks.springzookeeper.register.impl;
2 |
3 | import com.hks.springzookeeper.register.ServiceRegistry;
4 | import org.apache.zookeeper.*;
5 | import org.slf4j.Logger;
6 | import org.slf4j.LoggerFactory;
7 | import org.springframework.stereotype.Component;
8 |
9 | import java.util.concurrent.CountDownLatch;
10 |
11 | /**
12 | * @Author: hekuangsheng
13 | * @Date: 2018/10/31
14 | *
15 | * 创建一个ServiceRegistry的实现类。实现注册接口
16 | */
17 | @Component
18 | public class ServiceRegistryImpl implements Watcher, ServiceRegistry {
19 |
20 | private static final int SESSION_TIMEOUT = 5000;
21 | private static final String REGISTRY_PATH = "/registry";
22 | private static CountDownLatch latch = new CountDownLatch(1);
23 | private static Logger logger = LoggerFactory
24 | .getLogger(ServiceRegistryImpl.class);
25 | private ZooKeeper zk;
26 |
27 | public ServiceRegistryImpl() {
28 | logger.debug("初始化类");
29 | }
30 |
31 | public ServiceRegistryImpl(String zkServers) {
32 | try {
33 | zk = new ZooKeeper(zkServers, SESSION_TIMEOUT, this);
34 | latch.await();
35 | logger.debug("connected to zookeeper");
36 |
37 | } catch (Exception e) {
38 | logger.error("create zookeeper client failuer", e);
39 | }
40 | }
41 |
42 | @Override
43 | public void register(String serviceName, String serviceAddress) {
44 | String registryPath = REGISTRY_PATH;
45 | try {
46 | logger.debug("-zk---------" + zk);
47 | //创建根节点:持久节点
48 | if (zk.exists(registryPath, true) == null) {
49 | zk.create(registryPath, null, ZooDefs.Ids.OPEN_ACL_UNSAFE,
50 | CreateMode.PERSISTENT);
51 | logger.debug("create registry node:{}", registryPath);
52 | }
53 | //创建服务节点:持久节点
54 | String servicePath = registryPath + "/" + serviceName;
55 | if (zk.exists(servicePath, true) == null) {
56 | zk.create(servicePath, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
57 | logger.debug("create service node :{}" + servicePath);
58 | }
59 | //创建地址节点:临时顺序节点
60 | String addressPath = servicePath + "/address-";
61 | String addressNode = zk.create(addressPath, serviceAddress.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
62 | logger.debug("create node address:{}=>{}" + addressNode);
63 | } catch (Exception e) {
64 | logger.error("create node failure", e);
65 | }
66 | }
67 |
68 | @Override
69 | public void process(WatchedEvent watchedEvent) {
70 | if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
71 | latch.countDown();
72 | }
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/spring-zookeeper-register/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | server.address=127.0.0.1
2 | server.port=8081
3 | #server.name=spring-zookeeper
4 |
5 | registry.servers=127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183
--------------------------------------------------------------------------------
/spring-zookeeper-register/src/test/java/com/hks/springzookeeper/SpringZookeeperApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.hks.springzookeeper;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.test.context.junit4.SpringRunner;
7 |
8 | @RunWith(SpringRunner.class)
9 | @SpringBootTest
10 | public class SpringZookeeperApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/spring-zookeeper-register/src/test/java/com/hks/springzookeeper/distributeLock/RecipesLock.java:
--------------------------------------------------------------------------------
1 | package com.hks.springzookeeper.distributeLock;
2 |
3 | import org.apache.curator.framework.CuratorFramework;
4 | import org.apache.curator.framework.CuratorFrameworkFactory;
5 | import org.apache.curator.framework.recipes.locks.InterProcessLock;
6 | import org.apache.curator.framework.recipes.locks.InterProcessMutex;
7 | import org.apache.curator.retry.ExponentialBackoffRetry;
8 |
9 | import java.text.SimpleDateFormat;
10 | import java.util.Date;
11 | import java.util.SimpleTimeZone;
12 | import java.util.concurrent.CountDownLatch;
13 |
14 | /**
15 | * @Author: hekuangsheng
16 | * @Date: 2018/11/12
17 | */
18 | public class RecipesLock {
19 |
20 | static String lock_path = "/curator_recipes_lock_path";
21 | static CuratorFramework client = CuratorFrameworkFactory.builder()
22 | .connectString("127.0.0.1:2181")
23 | .retryPolicy(new ExponentialBackoffRetry(1000, 3))
24 | .build();
25 |
26 | public static void main(String[] args) {
27 | try{
28 | client.start();
29 | final InterProcessMutex lock = new InterProcessMutex(client,lock_path);
30 | final CountDownLatch down = new CountDownLatch(1);
31 | for(int i=0;i<10;i++){
32 | new Thread(new Runnable() {
33 | @Override
34 | public void run() {
35 | try{
36 | down.await();
37 | lock.acquire();
38 | }
39 | catch (Exception ex){
40 | ex.printStackTrace();
41 | }
42 | SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss|SSS");
43 | String orderNo = sdf.format(new Date());
44 | System.out.print("生成的订单号是:"+orderNo);
45 | try{
46 | lock.release();
47 | }
48 | catch (Exception ex){
49 | ex.printStackTrace();
50 | }
51 | }
52 | }).start();
53 | }
54 | down.countDown();
55 | }
56 | catch (Exception ex){
57 | ex.printStackTrace();
58 | }
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/spring-zookeeper-register/src/test/java/com/hks/springzookeeper/leaderSelector/Test.java:
--------------------------------------------------------------------------------
1 | package com.hks.springzookeeper.leaderSelector;
2 |
3 | import org.apache.curator.framework.CuratorFramework;
4 | import org.apache.curator.framework.CuratorFrameworkFactory;
5 | import org.apache.curator.framework.recipes.leader.LeaderSelector;
6 | import org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter;
7 | import org.apache.curator.retry.ExponentialBackoffRetry;
8 | import org.springframework.beans.factory.annotation.Value;
9 |
10 | /**
11 | * @Author: hekuangsheng
12 | * @Date: 2018/11/12
13 | */
14 | public class Test {
15 |
16 | static String master_path = "/curator_recipes_master_path";
17 | static CuratorFramework client = CuratorFrameworkFactory.builder()
18 | .connectString("127.0.0.1:2181")
19 | .retryPolicy(new ExponentialBackoffRetry(1000, 3))
20 | .build();
21 |
22 | @org.junit.Test
23 | public static void main(String[] args) throws Exception{
24 | client.start();
25 | LeaderSelector selector = new LeaderSelector(client,
26 | master_path,
27 | new LeaderSelectorListenerAdapter() {
28 | @Override
29 | public void takeLeadership(CuratorFramework curatorFramework) throws Exception {
30 | System.out.print("成为master角色");
31 | Thread.sleep(30000);
32 | System.out.print("完成master操作,释放master权利");
33 | }
34 | });
35 | selector.autoRequeue();
36 | selector.start();
37 | Thread.sleep(Integer.MAX_VALUE);
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/thrift-api/.gitignore:
--------------------------------------------------------------------------------
1 | /target/
2 | !.mvn/wrapper/maven-wrapper.jar
3 |
4 | ### STS ###
5 | .apt_generated
6 | .classpath
7 | .factorypath
8 | .project
9 | .settings
10 | .springBeans
11 | .sts4-cache
12 |
13 | ### IntelliJ IDEA ###
14 | .idea
15 | *.iws
16 | *.iml
17 | *.ipr
18 |
19 | ### NetBeans ###
20 | /nbproject/private/
21 | /build/
22 | /nbbuild/
23 | /dist/
24 | /nbdist/
25 | /.nb-gradle/
--------------------------------------------------------------------------------
/thrift-api/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | com.hks
7 | thrift-api
8 | 0.0.1-SNAPSHOT
9 | jar
10 |
11 | thrift-api
12 | thrift-api project for Spring Boot
13 |
14 |
15 | org.springframework.boot
16 | spring-boot-starter-parent
17 | 2.0.3.RELEASE
18 |
19 |
20 |
21 |
22 | UTF-8
23 | UTF-8
24 | 1.8
25 |
26 |
27 |
28 |
29 | org.springframework.boot
30 | spring-boot-starter
31 |
32 |
33 | org.apache.thrift
34 | libthrift
35 | 0.11.0
36 |
37 |
38 |
39 | org.springframework.boot
40 | spring-boot-starter-test
41 | test
42 |
43 |
44 |
45 |
46 |
47 |
48 | org.springframework.boot
49 | spring-boot-maven-plugin
50 |
51 |
52 |
53 |
54 |
55 |
56 |
--------------------------------------------------------------------------------
/thrift-api/src/main/java/com/hks/thriftapi/ThriftApiApplication.java:
--------------------------------------------------------------------------------
1 | package com.hks.thriftapi;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class ThriftApiApplication {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(ThriftApiApplication.class, args);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/thrift-api/src/main/resources/application.properties:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/singgel/RPC-SkillTree/67bfbc076b18bca17850c64fd2a935fdcd88d9ee/thrift-api/src/main/resources/application.properties
--------------------------------------------------------------------------------
/thrift-api/src/main/thrift/Hello.thrift:
--------------------------------------------------------------------------------
1 | namespace java com.hks.thriftapi.service
2 |
3 | service Hello{
4 | string helloString(1:string para)
5 | i32 helloInt(1:i32 para)
6 | bool helloBoolean(1:bool para)
7 | void helloVoid()
8 | string helloNull()
9 | }
--------------------------------------------------------------------------------
/thrift-api/src/test/java/com/hks/thriftapi/ThriftApiApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.hks.thriftapi;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.test.context.junit4.SpringRunner;
7 |
8 | @RunWith(SpringRunner.class)
9 | @SpringBootTest
10 | public class ThriftApiApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/thrift-client/.gitignore:
--------------------------------------------------------------------------------
1 | /target/
2 | !.mvn/wrapper/maven-wrapper.jar
3 |
4 | ### STS ###
5 | .apt_generated
6 | .classpath
7 | .factorypath
8 | .project
9 | .settings
10 | .springBeans
11 | .sts4-cache
12 |
13 | ### IntelliJ IDEA ###
14 | .idea
15 | *.iws
16 | *.iml
17 | *.ipr
18 |
19 | ### NetBeans ###
20 | /nbproject/private/
21 | /build/
22 | /nbbuild/
23 | /dist/
24 | /nbdist/
25 | /.nb-gradle/
--------------------------------------------------------------------------------
/thrift-client/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | com.hks
7 | thrift-client
8 | 0.0.1-SNAPSHOT
9 | jar
10 |
11 | thrift-client
12 | Demo project for Spring Boot
13 |
14 |
15 | org.springframework.boot
16 | spring-boot-starter-parent
17 | 2.0.3.RELEASE
18 |
19 |
20 |
21 |
22 | UTF-8
23 | UTF-8
24 | 1.8
25 |
26 |
27 |
28 |
29 | org.springframework.boot
30 | spring-boot-starter
31 |
32 |
33 | org.apache.thrift
34 | libthrift
35 | 0.11.0
36 |
37 |
38 | com.hks
39 | thrift-api
40 | 0.0.1-SNAPSHOT
41 |
42 |
43 |
44 | org.springframework.boot
45 | spring-boot-starter-test
46 | test
47 |
48 |
49 |
50 |
51 |
52 |
53 | org.springframework.boot
54 | spring-boot-maven-plugin
55 |
56 |
57 |
58 |
59 |
60 |
61 |
--------------------------------------------------------------------------------
/thrift-client/src/main/java/com/hks/thriftclient/ThriftClientApplication.java:
--------------------------------------------------------------------------------
1 | package com.hks.thriftclient;
2 |
3 | import com.hks.thriftapi.service.Hello;
4 | import org.apache.thrift.protocol.TBinaryProtocol;
5 | import org.apache.thrift.protocol.TProtocol;
6 | import org.apache.thrift.transport.TFramedTransport;
7 | import org.apache.thrift.transport.TSocket;
8 | import org.apache.thrift.transport.TTransport;
9 |
10 | import org.springframework.boot.SpringApplication;
11 | import org.springframework.boot.autoconfigure.SpringBootApplication;
12 |
13 | @SpringBootApplication
14 | public class ThriftClientApplication {
15 |
16 | public static void main(String[] args) {
17 | try {
18 | TTransport tTransport = getTTransport();
19 | TProtocol protocol = new TBinaryProtocol(tTransport);
20 | Hello.Client client = new Hello.Client(protocol);
21 | String result = client.helloString("hello");
22 | System.out.println("The result is: " + result);
23 | }catch (Exception e) {
24 | e.printStackTrace();
25 | }
26 | SpringApplication.run(ThriftClientApplication.class, args);
27 | }
28 |
29 | private static TTransport getTTransport() throws Exception{
30 | try{
31 | TTransport tTransport = getTTransport("127.0.0.1", 7911, 5000);
32 | if(!tTransport.isOpen()){
33 | tTransport.open();
34 | }
35 | return tTransport;
36 | }catch(Exception e){
37 | e.printStackTrace();
38 | }
39 | return null;
40 | }
41 |
42 | private static TTransport getTTransport(String host, int port, int timeout) {
43 | final TSocket tSocket = new TSocket(host, port, timeout);
44 | final TTransport transport = new TFramedTransport(tSocket);
45 | return transport;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/thrift-client/src/main/resources/application.properties:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/singgel/RPC-SkillTree/67bfbc076b18bca17850c64fd2a935fdcd88d9ee/thrift-client/src/main/resources/application.properties
--------------------------------------------------------------------------------
/thrift-client/src/test/java/com/hks/thriftclient/ThriftClientApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.hks.thriftclient;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.test.context.junit4.SpringRunner;
7 |
8 | @RunWith(SpringRunner.class)
9 | @SpringBootTest
10 | public class ThriftClientApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/thrift-server/.gitignore:
--------------------------------------------------------------------------------
1 | /target/
2 | !.mvn/wrapper/maven-wrapper.jar
3 |
4 | ### STS ###
5 | .apt_generated
6 | .classpath
7 | .factorypath
8 | .project
9 | .settings
10 | .springBeans
11 | .sts4-cache
12 |
13 | ### IntelliJ IDEA ###
14 | .idea
15 | *.iws
16 | *.iml
17 | *.ipr
18 |
19 | ### NetBeans ###
20 | /nbproject/private/
21 | /build/
22 | /nbbuild/
23 | /dist/
24 | /nbdist/
25 | /.nb-gradle/
--------------------------------------------------------------------------------
/thrift-server/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | com.hks
7 | thrift-server
8 | 0.0.1-SNAPSHOT
9 | jar
10 |
11 | thrift-server
12 | thrift-server project for Spring Boot
13 |
14 |
15 | org.springframework.boot
16 | spring-boot-starter-parent
17 | 2.0.3.RELEASE
18 |
19 |
20 |
21 |
22 | UTF-8
23 | UTF-8
24 | 1.8
25 |
26 |
27 |
28 |
29 | org.springframework.boot
30 | spring-boot-starter
31 |
32 |
33 | org.apache.thrift
34 | libthrift
35 | 0.11.0
36 |
37 |
38 | com.hks
39 | thrift-api
40 | 0.0.1-SNAPSHOT
41 |
42 |
43 |
44 | org.springframework.boot
45 | spring-boot-starter-test
46 | test
47 |
48 |
49 |
50 |
51 |
52 |
53 | org.springframework.boot
54 | spring-boot-maven-plugin
55 |
56 |
57 |
58 |
59 |
60 |
61 |
--------------------------------------------------------------------------------
/thrift-server/src/main/java/com/hks/thriftserver/ThriftServerApplication.java:
--------------------------------------------------------------------------------
1 | package com.hks.thriftserver;
2 |
3 | import com.hks.thriftapi.service.Hello;
4 | import com.hks.thriftserver.service.HelloServiceImpl;
5 | import org.apache.thrift.TProcessorFactory;
6 | import org.apache.thrift.protocol.TBinaryProtocol;
7 | import org.apache.thrift.server.TServer;
8 | import org.apache.thrift.server.TNonblockingServer;
9 | import org.apache.thrift.transport.TFramedTransport;
10 | import org.apache.thrift.transport.TNonblockingServerSocket;
11 | import org.apache.thrift.transport.TTransportException;
12 |
13 | import org.springframework.boot.SpringApplication;
14 | import org.springframework.boot.autoconfigure.SpringBootApplication;
15 |
16 | @SpringBootApplication
17 | public class ThriftServerApplication {
18 |
19 | public static void main(String[] args) {
20 | try {
21 | TNonblockingServerSocket socket = new TNonblockingServerSocket(7911);
22 | Hello.Processor processor = new Hello.Processor(new HelloServiceImpl());
23 | TNonblockingServer.Args arg = new TNonblockingServer.Args(socket);
24 | arg.protocolFactory(new TBinaryProtocol.Factory());
25 | arg.transportFactory(new TFramedTransport.Factory());
26 | arg.processorFactory(new TProcessorFactory(processor));
27 | TServer server = new TNonblockingServer(arg);
28 | server.serve();
29 | SpringApplication.run(ThriftServerApplication.class, args);
30 | } catch (TTransportException e) {
31 | e.printStackTrace();
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/thrift-server/src/main/java/com/hks/thriftserver/service/HelloServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.hks.thriftserver.service;
2 |
3 | import com.hks.thriftapi.service.Hello;
4 | import org.apache.thrift.TException;
5 |
6 | public class HelloServiceImpl implements Hello.Iface {
7 | @Override
8 | public boolean helloBoolean(boolean para) throws TException {
9 | return para;
10 | }
11 | @Override
12 | public int helloInt(int para) throws TException {
13 | try {
14 | Thread.sleep(20000);
15 | } catch (InterruptedException e) {
16 | e.printStackTrace();
17 | }
18 | return para;
19 | }
20 | @Override
21 | public String helloNull() throws TException {
22 | return null;
23 | }
24 | @Override
25 | public String helloString(String para) throws TException {
26 | return para;
27 | }
28 | @Override
29 | public void helloVoid() throws TException {
30 | System.out.println("Hello World");
31 | }
32 | }
--------------------------------------------------------------------------------
/thrift-server/src/main/resources/application.properties:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/singgel/RPC-SkillTree/67bfbc076b18bca17850c64fd2a935fdcd88d9ee/thrift-server/src/main/resources/application.properties
--------------------------------------------------------------------------------
/thrift-server/src/test/java/com/hks/thriftserver/ThriftServerApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.hks.thriftserver;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.test.context.junit4.SpringRunner;
7 |
8 | @RunWith(SpringRunner.class)
9 | @SpringBootTest
10 | public class ThriftServerApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------