OpenShift 4 - 用 OpenShift DevSpaces 在线开发 Quarkus 云原生应用(附视频)

34 篇文章 1 订阅
7 篇文章 0 订阅

OpenShift / RHEL / DevSecOps 汇总目录
本文在 OpenShift 4.11 中已验证。

请先根据《OpenShift 4 - 用 OpenShift DevSpaces 构建多租户的在线云原生应用开发环境》一文完成 OpenShift Dev Spaces 环境安装。

了解 Workspace 运行模式和 Devfile 文件

OpenShift DevSpaces 提供的 WebIDE 需要提供针对应用代码的界面交互功能和后台处理功能。在 OpenShift DevSpaces 中所有运行的界面、后台组件、处理功能都是运行在 Pod 中。例如要编译应用代码,就需要使用带有编译命令的 Image 然后使用编译命令。
Devfile 是在 OpenShift DevSpaces 中运行的 Workspace 的定义文件,它包含以下各方面的配置:包括应用代码源,IDE 可以使用的插件,Build、Run、Debug 应用代码的操作命令和运行环境等。
在这里插入图片描述
在 OpenShift DevSpaces 中提供了部分常用的 Devfile 模板,我们也可以在 https://registry.devfile.io/viewer 上获取其他 Devfile 使用。
在这里插入图片描述

使用定制 Devfile 配置 Quarkus 应用开发环境

  1. 在 OpenShift Dev Spacs 控制台的 Create Workspace 页面中为 Git Repo URL 提供 https://github.com/liuxiaoyu-git/hello-world-quarkus,然后点击 Create & Open 按钮。
    在这里插入图片描述
  2. 可以查看 https://github.com/liuxiaoyu-git/hello-world-quarkus 中的 .devfile.yaml 文件的内容。说明:由于下文演示了将 Java 应用编译为 Native 应用的场景,因此 memoryLimit 最小需要 4Gi 内存。
schemaVersion: 2.1.0
metadata:
  name: hello-world-quarkus
components:
  - name: tools
    container:
      image: quay.io/devfile/universal-developer-image:ubi8-latest
      endpoints:
        - name: webapp
          targetPort: 8080
      memoryLimit: 4Gi
      memoryRequest: 256Mi
commands:
  - exec:
      commandLine: './mvnw quarkus:dev'
      component: tools
      group:
        kind: build
      workingDir: '${PROJECTS_ROOT}/hello-world-quarkus'
    id: 1-build-dev
  - exec:
      commandLine: './mvnw test'
      component: tools
      group:
        kind: test
      workingDir: '${PROJECTS_ROOT}/hello-world-quarkus'
    id: 2-test
  - exec:
      commandLine: './mvnw package'
      component: tools
      group:
        kind: run
      workingDir: '${PROJECTS_ROOT}/hello-world-quarkus'
    id: 3-package
  - exec:
      commandLine: 'java -jar target/quarkus-app/quarkus-run.jar'
      component: tools
      group:
        kind: run
      workingDir: '${PROJECTS_ROOT}/hello-world-quarkus'
    id: 4-run-jar
  - exec:
      commandLine: './mvnw install -Dnative -DskipTests -Dquarkus.native.native-image-xmx=4g'
      component: tools
      group:
        kind: build
      workingDir: '${PROJECTS_ROOT}/hello-world-quarkus'
    id: 5-native
  - exec:
      commandLine: './target/getting-started-1.0.0-SNAPSHOT-runner'
      component: tools
      group:
        kind: run
      workingDir: '${PROJECTS_ROOT}/hello-world-quarkus'
    id: 6-run-native
  1. 名为 hello-world-quarkus 的 Workspace 创建完后会自动打开与其关联的 Web IDE 界面。
    在这里插入图片描述
  2. 在界面右侧 Workspace 的 User Runtime 中是快捷操。点击 tools 的 New terminal,然后在终端中运行修改 mvnw 的文件执行权限。
$ chmod +x mvnw

在这里插入图片描述

  1. 点击 tools 中的 1-build-dev,根据前面 Devfile.yaml 文件,可知会在终端中运行 ./mvnw quarkus:dev 命令,它代表应用运行在开发模式中。
    在这里插入图片描述
  2. 完成后会自动在新窗口显示 Quarkus 应用的默认调试页面。
    在这里插入图片描述
  3. 在 tools terminal 0 窗口中执行以下命令,确认可以正常返回结果。
$ curl -w "\n" http://localhost:8080/hello

在这里插入图片描述

  1. 关闭上图 1-build-dev 窗口。然后先点击右侧 tools 中的 3-package,再点击 4-run,此时应用再次运行起来。
  2. 另外也可在 tools terminal 0 中执行以下命令将应用编译转换成 Linux 可执行程序。
./mvnw install -Dnative -DskipTests -Dquarkus.native.native-image-xmx=4g
  1. 随后在 tools terminal 0 中执行命令运行可执行程序。
./target/getting-started-1.0.0-SNAPSHOT-runner
  1. 点击 tools 中的 New terminal,然后在新打开的窗口中执行命令测试可正常访问微服务。
$ curl -w "\n" http://localhost:8080/hello

查看 DevWorkspace 实例对应的 Pod 资源

用 user1 用户进入 DevWorkspace Operator 的 DevWorkspace 实例界面,有名为 hello-world-quarkus 的实例对应以上 Quarkus 应用。
在这里插入图片描述
进入 hello-world-quarkus 实例可以看到 Deployment、ReplicaSet、Pod 等相关资源,其中 Pod 显示 Running 的状态。
在这里插入图片描述
进入上图运行的 Pod,可以看到其中运行的 Container 和 对应的 Image,以及使用到的 Volume 配置。 注意:其中名为 tools 的容器既是由前面 Devfile 文件的 commponents 组件定义的。
在这里插入图片描述
以上 4 个运行的容器正是在 Web IDE 中 WORKSPACE 显示的 4 个 User Runtimes 项目,可以通过打开 New terminal 进入运行的容器。
在这里插入图片描述

演示视频

视频

参考

https://che.eclipseprojects.io/2021/10/12/@mario.loriedo-devfile-v2-and-the-devworkspace-operator-p1.html
https://che.eclipseprojects.io/2021/06/23/@florent.benoit-devfile-v2-and-ide-plug-ins.html
https://che.eclipseprojects.io/2019/10/02/@florent.benoit-discover-che-7-devfile.html
https://github.com/che-samples/java-spring-petclinic/blob/devfilev2/devfile.yaml
https://github.com/devfile-samples
https://developers.redhat.com/articles/2022/04/01/codeready-workspaces-scales-now-red-hat-openshift-dev-spaces#changes_to_the_workspace_engine

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值