spring-boot 2.5.4,nacos 作为配置、服务发现中心,Cloud Native Buildpacks 打包镜像,GitLab CI/CD

摘要:
以及使用nacos作为配置中心,使用grpc作为RPC框架。前置条件:JDK版本:1.8gradle版本:7.1spring-boot版本:2.5.4nacos版本:1.3.1GitLab配置spring-bootgradle插件spring-bootgradle插件在gradle中提供spring-boot支持。配置jar任务,该任务可以配置jar包的classifier。创建bootRun任务用于运行应用程序。当应用maven插件时会为bootArchives配置创建uploadBootArchives任务。bootArchives默认情况下包含bootJar或bootWar任务生成的文件。它等价于runtimeClasspath中的依赖减去developmentOnly配置中的依赖。工程结构目前为止,我们介绍了java项目中引入springgradle所需的插件,以及各个组件的作用。
spring-boot 2.5.4,nacos 作为配置、服务发现中心,Cloud Native Buildpacks 打包镜像,GitLab CI/CD

本文主要介绍 Java 通过 Cloud Native Buildpacks 打包镜像,通过 Gitlab 配置 CI/CD。以及使用 nacos 作为配置中心,使用 grpc 作为 RPC 框架。

前置条件:

  • JDK 版本:1.8
  • gradle 版本:7.1
  • spring-boot 版本:2.5.4
  • nacos 版本:1.3.1
  • GitLab 配置
    spring-boot 2.5.4,nacos 作为配置、服务发现中心,Cloud Native Buildpacks 打包镜像,GitLab CI/CD第1张

spring-boot gradle 插件

spring-boot gradle 插件在 gradle 中提供 spring-boot 支持。该插件可以打 jar 或者 war 包。

plugins {
	id 'org.springframework.boot' version '2.5.4'
}

新建一个 gradle 项目,该项目在只引用 id 'org.springframework.boot' version '2.5.4' 插件的情况下,gralde 任务分布完全没有变化,如下图所示。

spring-boot 2.5.4,nacos 作为配置、服务发现中心,Cloud Native Buildpacks 打包镜像,GitLab CI/CD第2张

引入 java 插件

plugins {
    id 'java'
    id 'org.springframework.boot' version '2.5.4'
}

但当引入 java 插件后,情况就大大不同了,可见,spring-boot 插件和 java 插件一起应用后,将产生如下反应:

spring-boot 2.5.4,nacos 作为配置、服务发现中心,Cloud Native Buildpacks 打包镜像,GitLab CI/CD第3张

  1. 创建bootJar任务,执行该任务会生成一个 fat jar。该 jar 包把所有的类文件打包进 BOOT-INF/classes 中,把项目依赖的所有 jar 包打包进 BOOT-INF/lib 中。

  2. 配置 assemble 任务,该任务依赖于 bootJar 任务,所以执行 assemble 任务的时候也会执行 bootJar

  3. 配置 jar 任务,该任务可以配置 jar 包的 classifier。配置方式如下,默认情况下 classifier 为空字符串:

    bootJar {
        classifier = 'boot'
    }
    
    jar {
        classifier = ''
    }
    
  4. 创建 bootBuildImage 任务,该任务可以使用 CNB 打包 OCI 镜像。后面会详细介绍如何使用 CNB。

  5. 创建 bootRun 任务用于运行应用程序。

  6. 创建 bootArchives 配置,注意这里是配置,不是任务。当应用 maven 插件时会为 bootArchives 配置创建 uploadBootArchives 任务。bootArchives 默认情况下包含 bootJarbootWar 任务生成的文件。

    uploadBootArchives {
        repositories {
            mavenDeployer {
                repository url: 'https://repo.example.com'
            }
        }
    }
    
  7. 创建 developmentOnly 配置。该配置用于管理开发时的依赖,比如 org.springframework.boot:spring-boot-devtools,该依赖仅在开发时使用,无需打进 jar 包中。

    dependencies {
        developmentOnly 'org.springframework.boot:spring-boot-devtools'
    }
    
  8. 创建 productionRuntimeClasspath 配置。它等价于 runtimeClasspath 中的依赖减去 developmentOnly 配置中的依赖。

  9. 配置 JavaCompile 任务默认使用 UTF-8

  10. 配置 JavaCompile 任务使用 -parameters 配置编译器参数。

引入 io.spring.dependency-management 插件

引入该插件后,将自动管理依赖版本。

plugins {
    id 'java'
    id 'org.springframework.boot' version '2.5.4'
    id "io.spring.dependency-management" version "1.0.11.RELEASE"
}

group 'com.toy'
version '1.0.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
}

spring-boot 2.5.4,nacos 作为配置、服务发现中心,Cloud Native Buildpacks 打包镜像,GitLab CI/CD第4张

引入 grpc 框架

基于本示例使用 nacos 作为服务发现中心,本示例将使用 net.devh:grpc-spring-boot-starter 依赖作为框架。

工程结构

目前为止,我们介绍了 java 项目中引入 spring gradle 所需的插件,以及各个组件的作用。接下来我们介绍如何引入 grpc,以及引入 grpc 后,我们的工程结构。

改造后工程结构总体如下:

spring-boot 2.5.4,nacos 作为配置、服务发现中心,Cloud Native Buildpacks 打包镜像,GitLab CI/CD第5张

protobuf

用于保存 proto 文件,以及发布 proto 文件,当客户端引用时,保证 jar 包最小。build.gradle 文件内容如下:

plugins {
    id 'java'
    id 'idea'
    id 'com.google.protobuf' version '0.8.17' //google proto 插件
    id 'maven-publish'
}

group 'com.toy'
version '1.0.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    //用于生成 java 类
    compileOnly 'io.grpc:grpc-protobuf:1.39.0'
    compileOnly 'io.grpc:grpc-stub:1.39.0'
}

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.17.3"
    }
    plugins {
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.39.0'
        }
    }

    generateProtoTasks {
        all()*.plugins {
            grpc {
            }
        }
    }
}

publishing {
    publications {
        proto_package(MavenPublication) {
        }
    }
    repositories {
        maven {
            allowInsecureProtocol = true
            url '你的 Maven 仓库地址'
            credentials {
                username = 'Maven 账号'
                password = 'Maven 密码'
            }
        }
    }
}

生成的 Java 类路径为 $projectName/build/.. 如下所示,生成的所有 class 文件位于 proto 文件夹下:

spring-boot 2.5.4,nacos 作为配置、服务发现中心,Cloud Native Buildpacks 打包镜像,GitLab CI/CD第6张

rpc

  1. 在 rpc 项目中添加启动类 ToyApplication,内容如下:

    package com.toy.rpc;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    /**
     * @author Zhang_Xiang
     * @since 2021/8/20 15:34:58
     */
    @SpringBootApplication(scanBasePackages = {"com.toy.*"})
    public class ToyApplication {
        public static void main(String[] args) {
            SpringApplication.run(ToyApplication.class, args);
        }
    }
    
  2. 在包 com.toy.rpc.impl 中添加 HelloImpl 文件,内容如下:

    package com.toy.rpc.impl;
    
    import com.toy.proto.GreeterGrpc;
    import com.toy.proto.HelloReply;
    import com.toy.proto.HelloRequest;
    import io.grpc.stub.StreamObserver;
    import net.devh.boot.grpc.server.service.GrpcService;
    
    /**
     * @author Zhang_Xiang
     * @since 2021/8/20 15:35:56
     */
    @GrpcService
    public class HelloImpl extends GreeterGrpc.GreeterImplBase {
    
        @Override
        public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
            HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + request.getName()).build();
            responseObserver.onNext(reply);
            responseObserver.onCompleted();
        }
    }
    
  3. 添加集成测试

    (1)添加集成测试配置

    package com.toy.config;
    
    import net.devh.boot.grpc.client.autoconfigure.GrpcClientAutoConfiguration;
    import net.devh.boot.grpc.server.autoconfigure.GrpcServerAutoConfiguration;
    import net.devh.boot.grpc.server.autoconfigure.GrpcServerFactoryAutoConfiguration;
    import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
    import org.springframework.boot.test.context.TestConfiguration;
    
    /**
    * @author Zhang_Xiang
    * @since 2021/8/12 16:26:25
    */
    @TestConfiguration
    @ImportAutoConfiguration({
            GrpcServerAutoConfiguration.class, // Create required server beans
            GrpcServerFactoryAutoConfiguration.class, // Select server implementation
            GrpcClientAutoConfiguration.class}) // Support @GrpcClient annotation
    public class IntegrationTestConfigurations {
    
    }
    

    (2)添加测试类

    package com.toy;
    
    import com.toy.config.IntegrationTestConfigurations;
    import com.toy.proto.GreeterGrpc;
    import com.toy.proto.HelloReply;
    import com.toy.proto.HelloRequest;
    import net.devh.boot.grpc.client.inject.GrpcClient;
    import org.junit.jupiter.api.Test;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.annotation.DirtiesContext;
    import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
    
    import static org.junit.jupiter.api.Assertions.assertEquals;
    
    /**
    * @author Zhang_Xiang
    * @since 2021/8/20 16:02:41
    */
    @SpringBootTest(properties = {
            "grpc.server.inProcessName=test", // Enable inProcess server
            "grpc.server.port=-1", // Disable external server
            "grpc.client.inProcess.address=in-process:test" // Configure the client to connect to the inProcess server
    })
    @SpringJUnitConfig(classes = {IntegrationTestConfigurations.class})
    @DirtiesContext
    public class HelloServerTest {
    
        @GrpcClient("inProcess")
        private GreeterGrpc.GreeterBlockingStub blockingStub;
    
        @Test
        @DirtiesContext
        public void sayHello_replyMessage() {
            HelloReply reply = blockingStub.sayHello(HelloRequest.newBuilder().setName("Zhang").build());
            assertEquals("Hello Zhang", reply.getMessage());
        }
    }
    
    
  4. build.gradle

    plugins {
        id 'java'
        id 'idea'
        id 'org.springframework.boot' version '2.5.4'
        id "io.spring.dependency-management" version "1.0.11.RELEASE"
    }
    
    group 'com.toy'
    version '1.0.0-SNAPSHOT'
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        implementation platform('io.grpc:grpc-bom:1.39.0') //使所有 protobuf 插件的版本保持一致
        implementation 'net.devh:grpc-spring-boot-starter:2.12.0.RELEASE'
        developmentOnly 'org.springframework.boot:spring-boot-devtools'
    
        implementation project(':protobuf') //引入 protobuf 项目
    
        testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.2'
        testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2'
        testImplementation 'io.grpc:grpc-testing'
        testImplementation('org.springframework.boot:spring-boot-starter-test')
    }
    
    bootBuildImage {
        imageName = "harbor.xxx.com/rpc/${project.name}:${project.version}"
        publish = true
        docker {
        publishRegistry {
                username = "admin"
                password = "admin"
                url = "harbor.xxx.com"
            }
        }
    }
    
    test {
        useJUnitPlatform()
    }
    

至此,整个 grpc 项目基础结构完成。

添加 nacos 配置中心、服务发现

  1. 在 rpc 项目 build.gradle 文件中引入读取 nacos 配置的 jar 包和注册服务到 nacos 中的 jar 包。

    dependencies{
        implementation 'org.springframework.boot:spring-boot-starter-web' //用于注册服务
        //添加此引用的原因是为了解决 spring boot 2.5.4 无法读取 nacos 配置的问题
        implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap:3.0.3'
        implementation 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery:2021.1'
        implementation 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-config:2021.1'
    }
    
  2. 添加读取服务配置,在 rpc 项目中添加 bootstrap.propertise,内容如下:

    spring.profiles.active=dev
    spring.application.name=toy
    

    添加 bootstrap-dev.properties,内容如下:

    spring.cloud.nacos.config.server-addr=127.0.0.1:8848
    spring.cloud.nacos.config.namespace=52f2f610-46f6-4c57-a089-44072099adde
    spring.cloud.nacos.config.file-extension=yaml
    spring.cloud.nacos.config.group=DEFAULT_GROUP
    spring.cloud.nacos.discovery.namespace=52f2f610-46f6-4c57-a089-44072099adde
    spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
    

至此,完成了服务端通过 nacos 读取配置,并且把服务端注册到 nacos 中。

gitlab CI/CD

在根项目目录下添加 .gitlab-ci.yml 文件。当 gitlab 安装了 runner 后,将自动触发 CI/CD,内容如下:

variables:
  CONTAINER_NAME: toy
  IMAGE_VERSION: 1.0.0
  IMAGE_TAG: harbor.xxx.com/toy/rpc
  PORT: 10086

stages:
  - test
  - publishJar
  - bootBuildImage //spring-boot 从 2.3.0 版本以后引入了 BootBuildImage 任务。
  - deploy

test:
  stage: test
  script:
    - gradle clean
    - gradle rpc:test

publishProtoBuf:
  stage: publishJar
  script:
    - gradle protobuf:publish

bootBuildImage:
  stage: bootBuildImage
  script:
    - gradle rpc:bootBuildImage

deployDev:
  stage: deploy
  script:
    - ssh $SERVER_USER@$SERVER_IP "docker login --username=$REGISTERY_NAME --password=$REGISTRY_PWD harbor.xxx.com; docker pull $IMAGE_TAG:$IMAGE_VERSION;"
    - ssh $SERVER_USER@$SERVER_IP "docker container rm -f $CONTAINER_NAME || true"
    - ssh $SERVER_USER@$SERVER_IP "docker run -d -p $PORT:$PORT -e JAVA_OPTS='-Xms512m -Xmx512m -Xss256K'  --net=host --name $CONTAINER_NAME $IMAGE_TAG:$IMAGE_VERSION"
  when: manual

这几个步骤什么意思呢?

  • 定义项目级别的变量
  • 定义了 4 个步骤,其中每个步骤中的任务又是可以并行的
    • test:运行项目中的单元测试(项目中没有写单元测试)、集成测试
    • publishJar:发布项目中 protobuf 项目到私有 maven 仓库中
    • bootBuildImage:打包镜像,并根据配置发布到镜像仓库中,这里打包过程需要详细说明
    • deploy:部署镜像到远程服务器中,在此步骤中配置了 when:manual,意思是手动触发此步骤

spring-boot 2.5.4,nacos 作为配置、服务发现中心,Cloud Native Buildpacks 打包镜像,GitLab CI/CD第7张

注意: 这里 SERVER_USERSERVER_IP$REGISTERY_NAME$REGISTRY_PWD 在 Gitlab 中通过超级管理员做了全局配置,即在所有项目中都可以使用。

spring-boot 2.5.4,nacos 作为配置、服务发现中心,Cloud Native Buildpacks 打包镜像,GitLab CI/CD第8张

定义 gitlab CI/CD 变量

CI/CD 变量一共有 4 种定义方式,如下:

  1. .gitlab-ci.yml 文件中定义
    spring-boot 2.5.4,nacos 作为配置、服务发现中心,Cloud Native Buildpacks 打包镜像,GitLab CI/CD第9张
  2. 在项目中定义
  3. 在组中定义
  4. gitlab 全局变量

变量优先级(从高到低)

  1. 触发变量、流水线变量、手动流水线变量
  2. 项目变量
  3. 组变量
  4. 全局变量
  5. 继承变量
  6. .gitlab-ci.yml 文件中,job 中定义的变量
  7. .gitlab-ci.yml 中定义的变量,job 外的变量
  8. 部署变量
  9. 预定义变量

源码地址

免责声明:文章转载自《spring-boot 2.5.4,nacos 作为配置、服务发现中心,Cloud Native Buildpacks 打包镜像,GitLab CI/CD》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇spring secrity添加和去掉x-frame-options deny安全头cas系列-cas登出(四)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

IOC容器的初始化

1、BeanFactory   Spring Bean 的创建是典型的工厂模式,这一系列的Bean 工厂,也即IOC 容器为开发者管理对象间的依赖关系提供了很多便利和基础服务,在Spring 中有许多的IOC 容器的实现供用户选择和使用,其相互关系如下:     其中BeanFactory 作为最顶层的一个接口类,它定义了IOC 容器的基本功能规范,Bea...

apolloJava客户端的使用

参考携程官网提供的https://github.com/ctripcorp/apollo/wiki/Java%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97  整个项目组的代码如下  项目的pom.xml代码如下 <project xmlns="http://maven...

深入理解Spring Redis的使用 (一)、Spring Redis基本使用

关于spring redis框架的使用,网上的例子很多很多。但是在自己最近一段时间的使用中,发现这些教程都是入门教程,包括很多的使用方法,与spring redis丰富的api大相径庭,真是浪费了这么优秀的一个框架。这里,我们就对比之前对spring orm中对hibernate的使用,来理解使用spring redis的使用。(本文章不做redis基本命...

springboot 实时监控 spring-boot-starter-actuator 包

对java工程实时监控方式很多,本文主要讲在springboot框架中的监控。 springboot框架,自带了actuator监控,在pom中引入jar包即可,如下 1.引入jar <dependency> <groupId>org.springframework.boot</groupId> <artifac...

Gradle 实战(1)—— 配置环境变量

背景:Gradle 是一款构建工具,继 Ant 、Maven 之后的现代构建工具,我会在接下来的博文中陆续介绍,我在工作中是如何使用 Gradle 的。 下载 Gradle 下面是 Gradle 的官方网站地址: Gradle l Modern Open-Source Enterprise Build Automation - Gradle http:...

mybatis的知识点总结

1.接口绑定:两种方法,基于注解或者基于xml文档mapper,但要注意mapper的namespace要与接口路径完全一致。 2.orm格式转换:通过设置resultMap和ResultType,将数据库中的记录转换为代码的bean对象。得到list或者对象。 3.通过parameterType接收参数,进行动态sql生成。运用ognl表达式 4.走缓存...