微服務體系下如何快速構建一個服務

近兩三年的時間,微服務是熱度陡增,做爲舊有SOA體系的一下特殊展示,在企業級應用市場上面應用愈來愈普遍,愈來愈多的團隊,開始採用微服務架構來改造現有的架構體系。無論實施的狀況如何,至少已經有成形的案例在線上跑。哪咱們這些遠未達到微服務架構的產品該如何呢,手癢的話就本身動手鼓搗吧,畢經並非都有那樣的環境來運用微服務技術。
java

微服務架構部署運維起來很龐大繁雜,但最終提供服務的仍是那些被拆分的很細小的子服務,這些子服務最終是以什麼形式編寫構造出來的?這並非什麼神祕的東西,你們都知道微服務體系是語言無關的,它能夠融合各類語言的服務進來,因此不一樣的語言提供的那些開箱即用的基本框架也是不同的。nginx

本篇主要仍是基於JAVA體系來講一說那些讓你分分鐘構建一個WEB服務的基礎框架吧。web

Spring Boot

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.spring

從官方給出的定義就不難看出spring boot的雄心壯志,spring cloud全家桶微服務體系就是基於Spring Boot之上構建起來的,能夠其戰略地位之高。內置應用服務器無須部署war,聽從約定優於配置的原則,簡單maven配置,自動化配置spring。引入簡單的jar後,便可輕鬆開啓一個web服務。sql

 
  1. <parent>shell

  2.    <groupId>org.springframework.boot</groupId>數據庫

  3.    <artifactId>spring-boot-starter-parent</artifactId>緩存

  4.    <version>1.5.7.RELEASE</version>服務器

  5. </parent>架構

  6. <dependencies>

  7.    <dependency>

  8.        <groupId>org.springframework.boot</groupId>

  9.        <artifactId>spring-boot-starter-web</artifactId>

  10.    </dependency>

  11. </dependencies>

再看Java啓動主類如何編寫

 
  1. import org.springframework.boot.*;

  2. import org.springframework.boot.autoconfigure.*;

  3. import org.springframework.stereotype.*;

  4. import org.springframework.web.bind.annotation.*;

  5. @Controller

  6. @EnableAutoConfiguration

  7. public class SampleController {

  8.    @RequestMapping("/")

  9.    @ResponseBody

  10.    String home() {

  11.        return "Hello World!";

  12.    }

  13.    public static void main(String[] args) throws Exception {

  14.        SpringApplication.run(SampleController.class, args);

  15.    }

  16. }

測試時直接以main的方式運行起來。部署時直接以java -jar xxx.jar的方式運行咱們的子服務。

Dropwizard

官網地址:http://www.dropwizard.io/1.1.4/docs/

Dropwizard is a Java framework for developing ops-friendly, high-performance, RESTful web services.

Dropwizard pulls together stable, mature libraries from the Java ecosystem into a simple, light-weight package that lets you focus on getting things done.

Dropwizard has out-of-the-box support for sophisticated configuration, application metrics, logging, operational tools, and much more, allowing you and your team to ship a production-quality web service in the shortest time possible.

Dropwizard與spring boot最大的不一樣一在於,它幫助你離開對Spring的依賴,當下s、Spring幾乎是Java世界的規範,這對於不使用Spring的團隊來講算是一個福音,但有多少這樣的團隊不使用Spring呢? Dropwizard還包含了不少其它很是有幫助的開發庫,如Guava,Jackson,Logback,,sl4j,habernate,Httpclient,Jersey,Freemaker,Joda等等來幫助咱們快速構建服務。 從其官網提供的入門教程來看,相比Spring Boot來說仍是稍顯複雜,但相較於傳統的框架整合而言仍是至關簡捷的。

Wildfly Swarm

簡單作個歷史回顧:

知道Wildfly的朋友估計很少,但提起JBoss的話熟識度應該是很高的。06年,JBoss被Redhat公司收購,收購後不久Redhat宣佈,將JBoss Application Server(JBoss AS)正式改名爲WildFly。 新名稱WildFly反映了服務器「很是靈活、輕量、不羈、自由」的特性。

Wildfly-swarm是一個基於Wildfly-core的微服務項目,和Wildfly應用服務器共同使用相同的內核組件MSC,擁有類似的架構和開發/構建方法。

基礎組件對好比下:

  • 注入服務: Weld CDI容器

  • Web容器: 嵌入式的Undertow(Undertow 是紅帽公司(RedHat)的開源產品,是 WildFly8(JBoos) 默認的 Web 服務器。)

  • Restful: RestEasy

  • 持久層:採用JPA、Hibernate做爲實現

  • 嵌入式的數據庫:HsqlDB和H2數據庫

  • 基於Maven、Gradle構建的方式

play

Play Framework makes it easy to build web applications with Java & Scala.Play is based on a lightweight, stateless, web-friendly architecture.

Built on Akka, Play provides predictable and minimal resource consumption (CPU, memory, threads) for highly-scalable applications.

Play是一個開源的現代web框架,用於編寫Java和Scala的可擴展Web應用程序。它經過自動重載變化來提升生產力,因爲設計的就是一個無狀態、無阻塞的架構,因此用Play框架來編寫橫向擴展Web應用程序是很容易的。

對於不在Java體系下開發微服務的話,相信其它語言也有對應的開箱便可的腳手架,來幫助你開發細粒度的服務體系,再結合相應的中間件如服務註冊、發現,監控,服務熔斷、降級等等,快速的上手一個微服務的案例。



相關文章
相關標籤/搜索