微服务体系下如何快速构建一个服务

近两三年的时间,微服务是热度陡增,作为旧有SOA体系的一下特殊展现,在企业级应用市场上面应用越来越广泛,越来越多的团队,开始采用微服务架构来改造现有的架构体系。不管实施的情况如何,至少已经有成形的案例在线上跑。哪我们这些远未达到微服务架构的产品该如何呢,手痒的话就自己动手鼓捣吧,毕经并不是都有那样的环境来运用微服务技术。

微服务架构部署运维起来很庞大繁杂,但最终提供服务的还是那些被拆分的很细小的子服务,这些子服务最终是以什么形式编写构造出来的?这并不是什么神秘的东西,大家都知道微服务体系是语言无关的,它可以融合各种语言的服务进来,所以不同的语言提供的那些开箱即用的基本框架也是不一样的。

本篇主要还是基于JAVA体系来说一说那些让你分分钟构建一个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 boot的雄心壮志,spring cloud全家桶微服务体系就是基于Spring Boot之上构建起来的,可以其战略地位之高。内置应用服务器无须部署war,遵从约定优于配置的原则,简单maven配置,自动化配置spring。引入简单的jar后,即可轻松开启一个web服务。

  1. <parent>

  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体系下开发微服务的话,相信其它语言也有对应的开箱即可的脚手架,来帮助你开发细粒度的服务体系,再结合相应的中间件如服务注册、发现,监控,服务熔断、降级等等,快速的上手一个微服务的案例。




成长的乐趣,在于分享!
大龄程序员,一路走来,感慨颇多。闲暇时写写字,希望能给同行人一点帮助。
本文版权归作者growithus和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/growithus/p/11012206.html