[go: up one dir, main page]

Skip to content

rio-2607/BeautyRPC

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 

Repository files navigation

BeautyRPC

基于Thrift实现的RPC中间件

使用

1.引入maven依赖(坐标待申请)

<dependency>
    <groupId>com.beautyboss.slogen</groupId>
    <artifactId>BeautyRPC</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

2. 注解

2.1 Server端

实现Service(实现Thrift的Iface接口)并在实现类上注解@ThriftServer

2.2 Client端

直接对.Client成员变量上增加注解@ThriftClient

3. 增加Configuration类,生成ThriftScannerConfig对象。

3.1 Server端

 @Bean
public ThriftScannerConfig scannerConfig() {
    ThriftScannerConfig config = new ThriftScannerConfig();
    // 设置thrift模式为服务端
    config.setThriftModel(ThriftScannerModel.THRIFT_SERVER_MODEL);
    return config;
}

3.2 Client端

@Bean
public ThriftScannerConfig scannerConfig() {
    ThriftScannerConfig config = new ThriftScannerConfig();
    // 设置thrift模式为客户端
    config.setThriftModel(ThriftScannerModel.THRIFT_CLIENT_MODEL);
    return config;
}

4. 增加配置

读取配置的时候首先会去读取thrift-rpc.properties文件,如果该文件存在,则会读取application.propertiesapplication-{env}.properties文件中的配置, 对于相同的key,application-{env}.properties中的配置会覆盖application.properties中的配置。

4.1 Server端

4.2 Client端

5. 启动

5.1 Server端

main函数中调用SpringThriftApplication.run()方法启动。

5.2 Client端

Client端无变化,原来该怎么启动还是怎么启动。

6.Demo

1 thrift文件中定义好接口,生成相应的java文件

namespace java com.beautyboss.slogen.demo

struct Request {
    1:i64 id,
    2:string word,
}

struct Response {
    1:i64 id,
    2:string data,
}

service HelloWorldService {
    Response sayHello(1:Request request);
}

2. Server端

  1. 实现HelloWorldServiceImpl.Iface接口
@ThriftServer
public class HelloWorldServiceImpl implements HelloWorldService.Iface{
    @Override
    public Response sayHello(Request request) throws TException {
        Response response = new Response();
        response.setId(request.getId());
        response.setData("Response from server : word " + request.getWord());
        return response;
    }
}
  1. 生成ThriftScannerConfig
@Configuration
public class ThriftConfig {

    @Bean
    public ThriftScannerConfig scannerConfig() {
        ThriftScannerConfig config = new ThriftScannerConfig();
        config.setThriftModel(ThriftScannerModel.THRIFT_SERVER_MODEL);
        return config;
    }
}
  1. application.properties新增配置

TODO

  1. 启动
@ComponentScans({@ComponentScan("com.beautyboss.slogen.server")})
@SpringBootApplication(scanBasePackages = "com.beautyboss.slogen.server")
public class ServerApplication {

    public static void main(String[] args) {
        SpringThriftApplication.run(ServerApplication.class,args);
    }
}

3. Client端

  1. 生成ThriftScannerConfig
@Configuration
public class ThriftConfig {

    @Bean
    public ThriftScannerConfig scannerConfig() {
        ThriftScannerConfig config = new ThriftScannerConfig();
        config.setThriftModel(ThriftScannerModel.THRIFT_CLIENT_MODEL);
        return config;
    }
}
  1. 添加配置

TODO

  1. 直接使用HelloWorldService.Client对象
@RestController
public class ClientController {

    @ThriftClient
    private HelloWorldService.Client client;

    @GetMapping("/hello")
    public Response sathello(@RequestParam("word") String word) throws TException {

        Request request = new Request();
        request.setId(System.currentTimeMillis());
        request.setWord(word);
        return client.sayHello(request);
    }
}

beuatyrpc.png

About

基于Thrift实现的RPC中间件

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages