http://bsideup.blogspot.com/2015/04/spring-boot-thrift-part2.html

 

Few words about Swift. Swift - Java annotation-based generator for Apache Thrift. It's priceless when you develop your APIs in Java and want to expose them to the other world using Apache Thrift.

Protocol

Lets recreate our previous example ( https://github.com/bsideup/spring-boot-thrift ) with it. Start with simple build.gradle file:
  '
  '
   
  buildscript {
  repositories {
  jcenter()
  }
   
  dependencies {
 
  }
  }
   
  repositories {
  jcenter()
  }
   
  1.8
  1.8
   
  dependencies {
   
 
   
 
   
  '
  '
  '
   
  '
   
  '
   
 
  }
view rawbuild.gradle hosted with ❤ by GitHub

Nothing special, Spring Boot with few Facebook Swift libraries.

Next we need to add Swift protocol classes:
  com.example.calculator.protocol;
   
  com.facebook.swift.service.ThriftMethod;
  com.facebook.swift.service.ThriftService;
  com.google.common.util.concurrent.ListenableFuture;
   
  @ThriftService
  TCalculatorService {
   
  @ThriftMethod
  TDivisionByZeroException;
  }

Exception is simple Swift struct which extend Exception (See Ma no TException)
  com.example.calculator.protocol;
   
  com.facebook.swift.codec.ThriftStruct;
   
  @ThriftStruct
  Exception {
  }

Enums are handled with Swift, so we don't need to annotate them (but we can)
  com.example.calculator.protocol;
   
  TOperation {
  ADD,
  SUBTRACT,
  MULTIPLY,
  DIVIDE
  }
view rawTOperation.java hosted with ❤ by GitHub

Next - application main class for Spring Boot:
  com.example.calculator;
   
  com.example.calculator.protocol.TCalculatorService;
  com.facebook.nifty.processor.NiftyProcessorAdapters;
  com.facebook.swift.codec.ThriftCodecManager;
  com.facebook.swift.service.ThriftEventHandler;
  com.facebook.swift.service.ThriftServiceProcessor;
  org.apache.thrift.protocol.*;
  org.apache.thrift.server.TServlet;
  org.springframework.boot.SpringApplication;
  org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  org.springframework.context.annotation.*;
   
  javax.servlet.Servlet;
  java.util.Arrays;
   
  @Configuration
  @EnableAutoConfiguration
  @ComponentScan
  CalculatorApplication {
  args) {
  .class, args);
  }
   
  @Bean
  tProtocolFactory() {
  Factory();
  }
   
  @Bean
  thriftCodecManager() {
  ThriftCodecManager();
  }
   
  @Bean
  exampleService) {
  >asList(), exampleService);
   
  TServlet(
  .processorToTProcessor(processor),
  protocolFactory,
  protocolFactory
  );
  }
  }

Test

Now we're ready to write some tests:
  com.example.calculator;
   
  com.example.calculator.protocol.TCalculatorService;
  com.example.calculator.protocol.TDivisionByZeroException;
  com.example.calculator.protocol.TOperation;
  com.facebook.nifty.client.HttpClientConnector;
  com.facebook.swift.codec.ThriftCodecManager;
  com.facebook.swift.service.ThriftClientManager;
  org.apache.thrift.protocol.TProtocolFactory;
  org.junit.Before;
  org.junit.Test;
  org.junit.runner.RunWith;
  org.springframework.beans.factory.annotation.Autowired;
  org.springframework.beans.factory.annotation.Value;
  org.springframework.boot.test.IntegrationTest;
  org.springframework.boot.test.SpringApplicationConfiguration;
  org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  org.springframework.test.context.web.WebAppConfiguration;
   
  java.net.URI;
   
  org.junit.Assert.*;
   
  .class)
  .class)
  @WebAppConfiguration
 
  CalculatorApplicationTest {
   
  @Autowired
  TProtocolFactory protocolFactory;
   
  @Autowired
  ThriftCodecManager thriftCodecManager;
   
 
  int port;
   
  TCalculatorService client;
   
  @Before
  Exception {
 
   
  ThriftClientManager(thriftCodecManager);
  .get();
  }
   
  @Test
  Exception {
  .ADD));
  }
   
  @Test
  Exception {
  .SUBTRACT));
  }
   
  @Test
  Exception {
  .MULTIPLY));
  }
   
  @Test
  Exception {
  .DIVIDE));
  }
   
  .class)
  Exception {
  .DIVIDE);
  }
  }

As you can see, only difference here (compared to Thrift version) is setUp method.
Diff with Thrift version

Implementation

We still have no Swift service implementation. Implementation of handler looks almost the same asprevious:
  com.example.calculator.handler;
   
  com.example.calculator.protocol.TCalculatorService;
  com.example.calculator.protocol.TDivisionByZeroException;
  com.example.calculator.protocol.TOperation;
  org.springframework.stereotype.Component;
   
  com.example.calculator.service.CalculatorService;
  org.springframework.beans.factory.annotation.Autowired;
   
  @Component
  TCalculatorService {
   
  @Autowired
  CalculatorService calculatorService;
   
  @Override
  TDivisionByZeroException {
  switch(op) {
  :
  .add(num1, num2);
  :
  .subtract(num1, num2);
  :
  .multiply(num1, num2);
  :
  try {
  .divide(num1, num2);
  IllegalArgumentException e) {
  TDivisionByZeroException();
  }
  :
  + op);
  }
  }
  }

Diff with Thrift version

Now if you will run tests you should see all tests green.
Building Microservices with Spring Boot and Apache Thrift. Part 2. Swifty services

Thrift integration

But hey, how about other non-Java consumers of service? Don't worry, Swift comes with a tool for generating *.thrift files from annotated Java classes:https://github.com/facebook/swift/tree/master/swift2thrift-generator-cli

Example output for our service will be:
  com.example.calculator.protocol
  com.example.calculator.protocol
  com.example.calculator.protocol
  com.example.calculator.protocol
   
   
  TOperation {
  DIVIDE
  }
   
  TDivisionByZeroException {
  }
   
  TCalculatorService {
  ex1);
  }
view rawcalculator.thrift hosted with ❤ by GitHub


Conclusion

Full source files for this example can be found at GitHub: https://github.com/bsideup/spring-boot-swift

Next time I will show you how to write Async Thrift services using Swift with minimal changes. Stay tuned!

相关文章:

  • 2021-06-25
  • 2022-12-23
  • 2021-08-03
  • 2022-12-23
  • 2021-12-05
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-20
  • 2022-12-23
  • 2021-08-10
  • 2022-12-23
  • 2021-11-14
  • 2021-06-05
  • 2021-06-30
相关资源
相似解决方案