1. 程式人生 > >基於 Thrift + Spring Boot 的微服務開發

基於 Thrift + Spring Boot 的微服務開發

先決條件

安裝 thrift

$ brew install thrift
$ thrift -version
Thrift version 0.11.0

如果需要安裝 0.10.0, 請執行如下命令

brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/16ebe5f1843e6cb54856311ff0f676be53007329/Formula/thrift.rb

建立 Maven 專案

建立 Maven 專案 thrift-server, 包含兩個模組 thrift-api 和thrift-service, 前者定義 API 介面, 後者是具體的實現.

父專案

父專案 thrift-server 的 POM 檔案定義

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.9.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>thrift-server</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>

<modules>
  <module>thrift-api</module>
  <module>thrift-service</module>
</modules>

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.6.1</version>
      <configuration>
        <source>1.8</source>
        <target>1.8</target>
      </configuration>
    </plugin>
  </plugins>
</build>

介面子專案

API 介面子專案 thrift-api 的 POM 檔案主要定義

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.thrift</groupId>
      <artifactId>thrift-maven-plugin</artifactId>
      <version>0.10.0</version>
      <configuration>
        <thriftExecutable>/usr/local/bin/thrift</thriftExecutable>
      </configuration>
      <executions>
        <execution>
          <id>thrift-sources</id>
          <phase>generate-sources</phase>
          <goals>
            <goal>compile</goal>
          </goals>
        </execution>
        <execution>
          <id>thrift-test-sources</id>
          <phase>generate-test-sources</phase>
          <goals>
            <goal>testCompile</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

<dependencies>
  <dependency>
    <groupId>org.apache.thrift</groupId>
    <artifactId>libthrift</artifactId>
    <version>0.11.0</version>
  </dependency>
</dependencies>

注意, 這裡使用的是 thrift-maven-plugin 外掛, 而不是 maven-thrift-plugin, 該外掛將在編譯之前, 將 thrift 檔案生成 Java 程式碼.

新建檔案 thrift-api/src/main/thrift/service.thrift, 內容如下

namespace java com.example.thrift.api
service HelloService {
    string greet(1:string name)
}

實現子專案

服務實現子專案 thrift-service 的 POM 檔案主要定義

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
</build>

<dependencies>
  <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
  </dependency>
  <dependency>
    <groupId>com.example</groupId>
    <artifactId>thrift-api</artifactId>
    <version>1.0-SNAPSHOT</version>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
  </dependency>
</dependencies>

新建入口類檔案 thrift-service/src/main/java/com/example/Application.java, 內容如下

@Slf4j
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

建立 HelloService.Iface 的實現類com.example.service.HelloServiceImpl, 內容如下

@Service("helloService")
public class HelloServiceImpl implements HelloService.Iface {
    @Override
    public String greet(String para) throws TException {
        return String.format("Hello %s!", para);
    }
}

編寫抽象測試類 com.example.AbstractTest, 內容如下

@RunWith(SpringRunner.class)
@SpringBootTest
public abstract class AbstractTest {
}

建立服務測試類 com.example.service.HelloServiceTest, 首先測試本地呼叫

@Autowired
private HelloService.Iface helloService;

@Test
public void testLocal() {
    try {
        log.info("本地呼叫服務...{}", helloService.greet("Local"));
    } catch (TException e) {
        log.error("本地呼叫異常.", e);
    }
}

執行測試輸出 本地呼叫服務...Hello Local!, 說明介面實現沒有問題.

遠端呼叫

服務端

在 Application.java 的 main 方法中增加如下邏輯

try {
    TProcessor tprocessor = new HelloService.Processor<>(new HelloServiceImpl());
    TServerSocket serverTransport = new TServerSocket(9898);
    TServer.Args tArgs = new TServer.Args(serverTransport);
    tArgs.processor(tprocessor);
    tArgs.protocolFactory(new TBinaryProtocol.Factory());
    TServer server = new TSimpleServer(tArgs);
    server.serve();
    log.info("服務端開啟....");
} catch (TTransportException e) {
    log.error("服務端開啟異常.", e);
}

將專案構建打包 mvn clean package, 將生成的 thrift-service-1.0-SNAPSHOT.jar 可執行 jar 包移到專案以外, 比如 /tmp 目錄, 然後用 java -jar xxx.jar 啟動服務端.

客戶端

在測試類 HelloServiceTest 中增加遠端呼叫測試

@Test
public void testRemote() {
    try (TTransport transport = new TSocket("localhost", 9898, 30000)) {
        TProtocol protocol = new TBinaryProtocol(transport);
        HelloService.Client helloService = new HelloService.Client(protocol);
        transport.open();
        log.info("遠端呼叫服務...{}", helloService.greet("Remote"));
    } catch (TException e) {
        log.error("遠端呼叫異常.", e);
    }
}

執行測試輸出 遠端呼叫服務...Hello Remote!, 說明遠端呼叫服務也沒有問題. 以上我們就構建了一個簡單的單執行緒微服務模型.

參考文獻

相關推薦

Spring boot服務開發實戰視訊

一 SpringCloud與Springboot二 Spring Boot快速構建天氣預報體系三 效勞拆分與事務建模;四 天氣預報體系的微效勞架構規劃與實現五 微效勞的和諧者Spring Cloud六 微效勞的註冊與發現-七 微效勞的消費八 API 閘道器九 微效勞的集中化配

基於 Thrift + Spring Boot服務開發

先決條件 安裝 thrift $ brew install thrift $ thrift -version Thrift version 0.11.0 如果需要安裝 0.10.0, 請執行如下命令 brew install https://raw.git

spring boot 服務例子一

ide oid 4.0 utf com pen tor not pid package com.example.hello.demo;import org.springframework.boot.SpringApplication;import org.springfra

spring eureka叢集+spring boot 服務,容器化部署示例

1.搭建eureka docker叢集 (1)建立eureka容器 參考如下指令,在不同的伺服器上建立eureka容器。(提前在docker hup 上下載好java:8的映象) docker run -d --name registry1 -p 8762:808

Spring Boot 服務叢集日誌等

API spring cloud gateway 日誌搜尋 elk(ElasticSearch,Logstash,Kibana) 持續整合 Jenkins 程式碼質量

Spring Boot 服務之間通過FeignClient進行大檔案下載:

使用FeignClient作為中介軟體進行一個微服務之間的呼叫的時候,一般的服務請求是沒有什麼問題,但是,當下載大檔案,會出現:java heap space 也就是堆溢位問題。 具體解決方案如下: 1、首先是service層返回ResponseEntit

Spring Boot服務框架

1. Spring Boot是什麼,解決哪些問題      1) Spring Boot使編碼變簡單      2) Spring Boot使配置變簡單      3) Spring Boot使部署變簡單      4) Spring Boot使監控變簡單      5) Spring Bo

Spring Boot服務如何集成fescar解決分布式事務?

getx 如何 text tin ati 數據覆蓋 tor password 服務 什麽是fescar?   關於fescar的詳細介紹,請參閱fescar wiki。   傳統的2PC提交協議,會持有一個全局性的鎖,所有局部事務預提交成功後一起提交,或有一個局部事務預提

Spring Boot服務框架整合之通用mapper外掛(tk.mybatis)

繼我上次的Springboot框架整合,整合了初步的微服務框架,還有mybatis整合實現資料庫查詢資料的demo現在給大家再整合一個本人覺得比較好用的通用mapper外掛(tk.mybatis),這個外掛裡面封裝好了我們需要用到的很多sql語句,不過這個外掛是通過我們去呼叫

Docker容器及Spring Boot服務應用

1 什麼是Docker 1.1 Docker的出現 問題一:專案實施環境複雜問題   傳統專案實施過程中經常會出現“程式在我這跑得好好的,在你那怎麼就不行呢?! ”   這是一個典型的應用場景,Docker image中包含了程式需要的所有的執行時依賴,比如java的

Spring Cloud 服務開發系列整理

環境搭建 分布 docker 第七篇 apol hpu 控制 tails mar 轉載請標明出處: https://blog.csdn.net/u010562966/article/details/88995076本文出自程序員果果的博客 源碼 github | Spr

服務開發的入門級框架Spring Boot詳解(四)

前幾章把Spring Boot大概都介紹了個遍,如果各位朋友有什麼疑問和指教歡迎在評論指出。下面我將介紹如何使用Spring Boot進行企業級開發。大概會從以下幾點開始介紹。 如何使用Spring Boot進行企業開發Spring Boot與Spring Cloud(Sp

基於Spring Boot構建應用開發規範

SpringBoot 項目規範 1.規範的意義和作用 編碼規範可以最大限度的提高團隊開發的合作效率 編碼規範可以盡可能的減少一個軟件的維護成本 , 並且幾乎沒有任何一個軟件,在其整個生命周期中,均由最初的開發人員來維護 編碼規範可以改善軟件的可讀性,可以讓開發人員盡快而徹底地理解新的代碼 規範性編碼

Https系列之二:https的SSL證書在服務器端的部署,基於tomcat,spring boot

onf 基於 分享 height 轉化 自簽名 size class ont 一:本文的主要內容介紹 CA證書的下載及相應文件的介紹 CA證書在tomcat的部署 CA證書在spring boot的部署 自簽名證書的部署 二:一些內容的回顧 在Https系列之一中已介

公開課:服務開發利器Spring Cloud實踐課程

原始碼時代線下免費公開課:微服務開發利器Spring Cloud實踐課程 課程介紹 本堂課主要講解 1、為什麼需要spring cloud 2、spring cloud簡介 3、spring cloud微服務實踐 學習目標 1、理解微服務架構 2、spring cloud在微服務

Spring Boot & Angular2快速開發檔案上傳服務

序 檔案上傳可以作為一個獨立的微服務。用Spring Boot和Angular2開發這樣的服務非常有優勢,可以用最少的程式碼,實現非常強的功能。如果比了解Spring Boot和Angular2的,請先看這幾個文章: Angular2檔案上傳元件 Angular2

java--spring boot信支付服務端實現含程式碼(app支付、掃碼支付等)

最近在弄支付寶、微信支付等的事,發現這兩家的文件都寫得很差,demo也讓人看的雲裡霧裡。所以寫篇部落格,來儘量減少後來的同學走的彎路。 首先,若是要做app支付的話,在微信第三方平臺申請APP,若是安卓的話上傳你的keystore和包名,ios上傳bundle id,微信會

信公眾號推送訊息(基於idea+spring boot

微服務功能:提供推送微信模板訊息介面,並打成jar包執行;pom檔案:<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmln

Spring Cloud服務架構開發實戰!贈書活動!

點選上方“Java後端技術”,右上角-設定星標有趣有深度的文章每天八點半準時送達!本次送書活動由

基於Java (spring-boot)和信小程式的校園閒置二手交易商城(畢業設計優秀論文)

1. 總體功能圖 1使用者端 (1)使用者資訊模組  使用者註冊登入、個人資訊維護 (2)閒置資訊模組  閒置資訊的釋出與求購、查詢 (3)留言模組      實時留言功能 (4)關注使用者      實時瞭解關注使用者動態 (5)資料統計模組  統計交易資訊 2