在IDEA整合Springboot与IBM liberty

简介

Liberty 是一款全新的轻量级应用服务器,它将用户的良好开发体验作为最主要的出发点。其主要特点和内容包括:

  • 高模块化——该功能允许用户根据自己应用程序的需求启用或者禁用相关的 feature(所谓 feature,在这里指的是运行应用程序所需要的各种资源的支持。比如,应用程序用到了 JSP,我们就需要启动 JSP 这个 feature,如果不在需要此 feature,就可以将其禁用。通过这种模块化的控制,我们可以按需启动不同的 feature,包括 JSP, Servlet, JPA 等等,这种控制是实现轻量级应用服务器的基础)。

  • 轻量级—— Liberty 采用了多种技术进行瘦身和优化(主要是 OSGi 的应用),与传统的 WAS 相比,极大的减少了运行时的内存消耗。

  • 高动态性——由于采用了 OSGi 的架构,程序员在开发的时候,可以动态修改配置文件,应用程序以及服务器的运行时信息,这些修改都是实时生效的,不需要重启服务器。

  • 快速——由于采用了高模块化的设计,Liberty 应用服务器会“按需启动”,自身消耗的系统资源很少,对执行任务的反应时间也非常短,例如,一般来说 Liberty 的启动时间小于 5 秒。

  • 配置简单——在整个开发过程中,用户只需要和一个配置文件(server.xml)打交道,而且配置的使用也非常灵活。

尽管 Liberty 与传统 WAS 相比只是一个“小块头”,但是在 Web 和 OSGi 应用程序的开发上,Liberty 与传统 WAS 完全保持一致,在 Liberty 上开发的程序,可以直接移植到传统 WAS 上,不需要做任何的改变。相比传统 WAS 的庞大,Liberty 在为用户提供良好的开发体验上遥遥领先。

在本文中,我将描述如何使用Spring Boot和IDEA开发工具创建一个简单的“Hello World”应用程序。


IBM Liberty Vs Tomcat

简单来说,IBM Liberty就是 IBM WebSphere Application Server 的简化版,Liberty是从WebSphere 8.5.5引入的,与传统的WAS不同,Liberty侧重于云应用与微服务的支持,用户可以配置细粒度级别的应用程序所需的组件,所以服务器启动速度非常快,占用空间也很小,跟tomcat一样轻量级,一般都是用于企业级应用比较多,尤其是金融类、或者大型商业应用等,提供很多商业级的扩展组件。

两者区别可以通过以下两篇文章了解:

https://webspherecompetition.files.wordpress.com/2012/11/liberty-vs-tomcat.pdf

https://www.ibm.com/developerworks/community/files/form/anonymous/api/library/ac092234-1cdc-406a-9317-39ee651ffbf6/document/ed0b79c7-09f1-4023-b7ec-31efa730c97f/media/tomcat-liberty.pdf


开始之前

你需要先到以下网址下载最新版的WebSphere Liberty runtime:

https://developer.ibm.com/wasdev/downloads/download-latest-stable-websphere-liberty-runtime/


创建Springboot Rest应用

利用Spring Initializr创建一个Sprignboot项目

选择File->Project->Spring Initizlizr创建项目命名为liberty-demo,依赖包选择Web,然后一直点击Next完成即可,项目结构如下:



配置Pom.xml文件,添加servlet容器依赖:

  1. <dependency>
  2. <groupId>javax.servlet</groupId>
  3. <artifactId>javax.servlet-api</artifactId>
  4. <version>3.1.0</version>
  5. <scope>provided</scope>
  6. </dependency>

如果使用旧版本的应用服务器,还需要添加如下依赖:

  1. <dependency>
  2. <groupId>javax</groupId>
  3. <artifactId>javaee-api</artifactId>
  4. <version>7.0</version>
  5. <scope>provided</scope>
  6. </dependency>

跟配置外置tomcat一样,我们需要过滤到Springboot里的tomcat模块

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. <exclusions>
  5. <exclusion>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-tomcat</artifactId>
  8. </exclusion>
  9. </exclusions>
  10. </dependency>

我项目完整的Pom.xml文件如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.eshare.liberty</groupId>
  6. <artifactId>demo</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>demo</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.0.6.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. </properties>
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-web</artifactId>
  30. <exclusions>
  31. <exclusion>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-starter-tomcat</artifactId>
  34. </exclusion>
  35. </exclusions>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.projectlombok</groupId>
  39. <artifactId>lombok</artifactId>
  40. <optional>true</optional>
  41. </dependency>
  42. <dependency>
  43. <groupId>org.springframework.boot</groupId>
  44. <artifactId>spring-boot-starter-test</artifactId>
  45. <scope>test</scope>
  46. </dependency>
  47. <dependency>
  48. <groupId>javax.servlet</groupId>
  49. <artifactId>javax.servlet-api</artifactId>
  50. <version>3.1.0</version>
  51. <scope>provided</scope>
  52. </dependency>
  53. </dependencies>
  54. <build>
  55. <plugins>
  56. <plugin>
  57. <groupId>org.springframework.boot</groupId>
  58. <artifactId>spring-boot-maven-plugin</artifactId>
  59. </plugin>
  60. </plugins>
  61. </build>
  62. </project>

我们要使用传统的WAR发布Sringboot项目方式,需要调整程序入口代码,继承SpringBootServletInitializer类,重写configure方法去告诉容器我们的程序启动入口

  1. @SpringBootApplication
  2. public class DemoApplication extends SpringBootServletInitializer {
  3. public static void main(String[] args) {
  4. SpringApplication.run(DemoApplication.class, args);
  5. }
  6. @Override
  7. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  8. return application.sources(DemoApplication.class);
  9. }
  10. }

接下来我们实现一个简单的Rest程序,用于测试容器是否启动正常

  1. /**
  2. * Created by liangyh on 2018/10/20.
  3. * Email:10856214@163.com
  4. */
  5. @RestController
  6. public class IndexController{
  7. @RequestMapping("/sayHello/{name}")
  8. public String sayHello(@PathVariable String name) {
  9. return "hello," + name;
  10. }
  11. }

记住,Springboot的启动类必须置于其他类的同级或者外层,否则会出现扫描不到类的情况,完成项目结构如下:


配置Liberty应用服务器

设置我们的模块包的打包方式

点击Peroject Structure->Artifacts,如下图所示选择打包方式:



选择完成后,点击Apply完成

选择启动应用服务器

如下图所示,选择liberty作为我们的项目服务器,然后填入相应配置,必须配置请求上下文(如/demo/):





设置Liberty服务器配置

在wlp\usr\servers\defaultServer路径下,找到Liberty启动服务器配置server.xml,添加host="*",指定拦截任何请求,否则应用服务器不会拦截你的请求:


启动服务器验证

在IDEA启动服务器,浏览器输入http://localhost:9080/demo/sayHello/haha,可以看到如下结果:

liberty | 在IDEA整合Springboot与IBM liberty的更多相关文章

  1. 用Springboot干掉IBM的WAS-为公司省点钱

    1 那一夜,你伤害了我 今夜的雨下得凉快,小南睡得正香,突然收到远洋运维小周的电话:Hello, Are you OK? WAS有issue,快起来help me! 只见小南登陆WAS机,查看了机器日 ...

  2. 整合springboot(app后台框架搭建四)

    springboot可以说是为了适用SOA服务出现,一方面,极大的简便了配置,加速了开发速度:第二方面,也是一个嵌入式的web服务,通过jar包运行就是一个web服务: 还有提供了很多metric,i ...

  3. 整合 springboot 和 swagger出问题

    整合 springboot 和 swagger ,出现报错, org.springframework.beans.factory.UnsatisfiedDependencyException: Err ...

  4. 【SpringBoot】搜索框架ElasticSearch介绍和整合SpringBoot

    ========================12章 搜索框架ElasticSearch介绍和整合SpringBoot ============================= 加入小D课堂技术交 ...

  5. netty-socketio整合springboot消息推送

    netty-socketio整合springboot消息推送 1.netty-socketio消息推送 1)在项目中常常涉及到消息推送的情况,消息推送要求的实时性,使用传统的方式已经不能满足需求了: ...

  6. 教你 Shiro 整合 SpringBoot,避开各种坑

    教你 Shiro 整合 SpringBoot,避开各种坑-----https://www.cnblogs.com/HowieYuan/p/9259638.html

  7. 基于 SpringBoot2.0+优雅整合 SpringBoot+Mybatis

    SpringBoot 整合 Mybatis 有两种常用的方式,一种就是我们常见的 xml 的方式 ,还有一种是全注解的方式.我觉得这两者没有谁比谁好,在 SQL 语句不太长的情况下,我觉得全注解的方式 ...

  8. 消息中间件——RabbitMQ(十)RabbitMQ整合SpringBoot实战!(全)

    前言 1. SpringBoot整合配置详解 publisher-confirms,实现一个监听器用于监听Broker端给我们返回的确认请求:RabbitTemplate.ConfirmCallbac ...

  9. Activiti7整合SpringBoot(十二)

    1 SpringBoot 整合 Activiti7 的配置 为了能够实现 SpringBoot 与 Activiti7 整合开发,首先我们要引入相关的依赖支持.所以,我们在工程的 pom.xml 文件 ...

随机推荐

  1. CodeForces - 1239A

    推公式题,万万没想到斐波那契啊

  2. 清晰架构(Clean Architecture)的Go微服务: 日志管理

    良好的日志记录可以提供丰富的日志数据,便于在调试时发现问题,从而大大提高编码效率. 记录器提供的自动化信息越多越好,日志信息也需要以简洁的方式呈现,便于找到重要的数据. 日志需求: 无需修改业务代码即 ...

  3. Java高级特性——流

    以上就是这段时间学习完流的知识以后我的总结,.mmap文件可以去我的github上获取:https://github.com/xiaozhengyu/StudyNotes.git

  4. 编译GLib C程序

    编译GLib C程序 GLib是GTK +所需的实用程序库,但也可以在非GUI应用程序中独立使用.本文介绍如何在Linux中编译使用GLib的C程序.它还显示了如何为系统上安装的GLib版本安装正确的 ...

  5. POJ2186 Popular Cows 题解 强连通分量

    题目链接:http://poj.org/problem?id=2186 题目大意: 每头牛都想成为牛群中的红人. 给定N头牛的牛群和M个有序对(A, B),(A, B)表示牛A认为牛B是红人: 该关系 ...

  6. 洛谷P1035 级数求和 题解 简单模拟

    题目链接:https://www.luogu.com.cn/problem/P1035 题目描述 已知:\(S_n= 1+1/2+1/3+-+1/n\).显然对于任意一个整数 \(k\),当 \(n\ ...

  7. 如何在Pypi发布上传你自己的Python库

    Pypi上传包 准备 Windows环境 *以下教程只能在Windows上执行,MACOS系统不能使用 注册账号 你需要在Pypi上注册一个账号. 安装必要的库 setuptools 原则上安装了pi ...

  8. 用积分方法求K次方和数列公式

    这是我很早以前在高中时发现的一个通用计算K次方和数列公式的方法,很特别的地方是用了微积分中的积分方法.目前我还没有发现有谁提出和我一样的方法,如果哪位读者有相关发现,麻烦告知我. 大家很多人都知道高斯 ...

  9. C++ | C++ 基础知识 | 结构、联合与枚举

    1. 结构 1.0 结构 数组是相同类型元素的集合,相反,struct 是任意类型元素的集合. 代码例子: struct Address { const char* name; int number; ...

  10. 从 posix_spawn() 函数窥探漏洞逃逸

    posix_spawn() 函数是用来在Linux上创建子进程的,头文件是 #include <spawn.h> ,语法如下: #include <spawn.h> int p ...