前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Boot 微服务上容器平台的最佳实践 - 8 - Rest Service

Spring Boot 微服务上容器平台的最佳实践 - 8 - Rest Service

作者头像
东风微鸣
发布2022-04-21 14:02:46
6840
发布2022-04-21 14:02:46
举报
文章被收录于专栏:东风微鸣技术博客

前言

今天开始第八篇, 主要介绍 spring微服务的相关设计和开发思路, 这次介绍REST服务的实现.

Airports 服务是应用程序中最简单的微服务,这为构建基本的Spring Boot REST服务提供了很好的参考。

Spring Boot Rest Service

Spring Boot Application Class

要将Java项目指定为Spring Boot应用程序,需要包含一个用SpringBootApplication注释的Java类,该类实现默认的Java main方法.

代码语言:javascript
复制
package com.redhat.refarch.obsidian.brownfield.lambdaair.airports;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RestApplication
{
 public static void main(String[] args)
 {
  SpringApplication.run( RestApplication.class, args );
 }
}

声明应用程序名称(application name)也是一种良好的实践,它可以作为common application properties的一部分。这个应用程序使用以每个项目的名称开头的application.yml 文件.

代码语言:javascript
复制
spring:
  application:
    name: airports

Maven Project File

每个微服务项目都包含一个Maven POM文件,该文件除了声明模块属性(module properties)和依赖项外,还包含一个配置文件定义(profile definition),用于使用fabric8-maven-plugin创建和部署K8S或OpenShift镜像。

该POM文件使用一个属性(property)来声明包含操作系统和Java开发工具包(JDK)的基础镜像。此应用程序中的所有服务都构建在Red Hat Enterprise Linux (RHEL)基础镜像之上,其中包含一个受支持的OpenJDK版本:

代码语言:javascript
复制
<properties>
...
  <fabric8.generator.from>registry.access.redhat.com/redhat-openjdk-18/openjdk18-openshift</fabric8.generator.from>
</properties>

要轻松地包含提供REST服务的简单Spring Boot应用程序的依赖项,请声明以下两个构件(artifacts):

代码语言:javascript
复制
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>

此应用程序中的每个服务还声明了对Spring Boot Actuator 组件的依赖关系,其中包括许多附加功能,可以帮助监视和管理应用程序。

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

当声明了对Actuator的依赖时,fabric8会生成默认的OpenShift health probes,该probes与Actuator服务通信,以确定服务是否正在运行(running)并准备(ready)好为请求提供服务:

代码语言:javascript
复制
    livenessProbe:
      failureThreshold: 3
      httpGet:
        path: /health
        port: 8080
        scheme: HTTP
      initialDelaySeconds: 180
      periodSeconds: 10
      successThreshold: 1
      timeoutSeconds: 1
    readinessProbe:
      failureThreshold: 3
      httpGet:
        path: /health
        port: 8080
        scheme: HTTP
      initialDelaySeconds: 10
      periodSeconds: 10
      successThreshold: 1
      timeoutSeconds: 1

Spring Boot REST Controller

要接收和处理REST请求,需要包含一个用RestController注释的Java类.

代码语言:javascript
复制
...
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Controller

在application properties(应用程序属性)中为该服务指定监听端口:

代码语言:javascript
复制
server:
  port: 8080

每个REST操作都由Java方法实现。业务操作通常需要指定请求参数:

代码语言:javascript
复制
@RequestMapping( value = "/airports", method = RequestMethod.GET )
public Collection<Airport> airports(
  @RequestParam( value = "filter", required = false ) String filter)
{
 ...

启动初始化

Airports 服务在启动时使用 eager initialization(即时初始化)将机场数据加载到内存中。这是通过监听特定类型事件的ApplicationListener实现的:

代码语言:javascript
复制
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

@Component
public class ApplicationInitialization implements 
ApplicationListener<ContextRefreshedEvent>
{
 @Override
 public void onApplicationEvent(ContextRefreshedEvent 
contextRefreshedEvent)

小结

这篇有2个知识点:

  1. 当声明了对Actuator的依赖时,fabric8会生成默认的OpenShift health probes. 这也算fabric8的一个优势, 少了人工加probe的步骤;
  2. 能外部化的配置都可以外部化到: application properties里. 它可以是application.yml.
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-11-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 东风微鸣技术博客 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • Spring Boot Rest Service
    • Spring Boot Application Class
      • Maven Project File
        • Spring Boot REST Controller
          • 启动初始化
          • 小结
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档