Jenkins+Docker+SpringCloud微服务持续集成

Jenkins+Docker+SpringCloud微服务持续集成

一:Jenkins+Docker+SpringCloud续集成流程说明

image-20220224095626974

大致流程说明:

1) 开发人员每天把代码提交到Gitlab代码仓库

2) Jenkins从Gitlab中拉取项目源码,编译并打成jar包,然后构建成Docker镜像,将镜像上传到Harbor私有仓库。

3) Jenkins发送SSH远程命令,让生产部署服务器到Harbor私有仓库拉取镜像到本地,然后创建容器。

4) 最后,用户可以访问到容器

微服务源码概述

项目架构:前后端分离

后端技术栈:SpringBoot+SpringCloud+SpringDataJpa(Spring全家桶)

微服务项目结构:

image-20220224100020455

  • tensquare_parent: 父 工 程 , 存 放 基 础 配 置
  • tensquare_common:通用工程,存放工具类
  • tensquare_eureka_server:Eureka注册中心
  • tensquare_zuul:SpringCloud 的 网 关 服 务
  • tensquare_admin_service:基础权限认证中心,负责用户认证(WT认证)
  • tensquare_gathering:一个简单的业务模块,负责微服务相关逻辑

数据库结构

image-20220224100118524

  • l tensquare_user:用户认证数据库,存放用户账户数据(登录)。对应tensquare_admin_service微服务
  • l tensquare_gathering:活动微服务数据库。对应tensquare_gathering微服务(用于展示出来)

微服务配置分析:

  • tensquare_eureka (注册中心)
  • tensquare_zuul (网关)
  • tensquare_admin_service (权限管理)
  • tensquare_gathering (业务)

二: 本地部署SpringCloud微服务部署

加载新项目,配置jdk

逐一启动微服务

链接:https://pan.baidu.com/s/1FEuOhlnbKpG-Wabydo4cqQ?pwd=s7b0
提取码:s7b0

微服务项目包解压,将tensquare_parent 项目目录放到idea的项目目录idea project中

然后在idea中点击open,选择项目,将项目在idea中打开,idea会自动下载相关组件插件(等待时间较长)

img

img

img

img

右下角弹窗,点击Always Add

配置jdk

SpringBoot启动类报错can not resolve method 'run(java.lang.class,String [])',则是因为jdk配置问题

img

img

img

img

每一个都要检查jdk配置

img

img

安装配置Maven

链接:https://pan.baidu.com/s/1FlnSgBKpt0AkLveVId4iUQ?pwd=bhde
提取码:bhde

Maven官网:https://maven.apache.org/download.cgi

img

解压maven

image-20220224101345400

配置环境变量

添加两个新环境变量

image-20220224101609874

image-20220224101650025

image-20220224101724444

添加maven命令到path路径

image-20220224102015337

image-20220224101822329

image-20220224102144733

格式如C:\Users\wangxiang\apache-maven-3.8.4\bin;..............

win+r,输入cmd打开命令行界面,输入mvn -v 可以看到maven版本

image-20220224104500391

修改maven 仓库,配置下载源

新建文件夹repo

image-20220224104701004

打开maven的工作目录下的conf目录,打开settins.xml文件

image-20220224104851477

修改组件地址为本地

<localRepository>C:/Users/wangxiang/repo</localRepository>

image-20220224105315417

配置下载源

<mirror>
    <id>aliyunmaven</id>
    <mirrorOf>central</mirrorOf>
    <name>aliyun maven</name>
    <url>https://maven.aliyun.com/repository/public </url>
</mirror>

image-20220224105453672

保存退出

在idea里配置maven设置

自动更新

image-20220224105607434

maven 安装完毕后,每一个都不能有红色波浪线。有红色波浪线,表示有必要的组件没有下载完成。此时,最好删除项目文件 ,重新导入项目,删除缓存,然后,然后maven仓库,重新建立仓库,最后,再重新配置maven,下载组件

三:windows本地安装mysql 数据库,导入库

链接:https://pan.baidu.com/s/1v3T8tudglq_ikJpudbh_Lg?pwd=43uw
提取码:43uw

本地安装mysql

image-20220224110348399

image-20220224110401076

image-20220224110416674

image-20220224110436523

image-20220224110552230

image-20220224110645441

image-20220224110725487

image-20220224110819449

image-20220224110851652

image-20220224110905738

image-20220224110956948

image-20220224111013420

image-20220224111034677

image-20220224111050230

image-20220224111109777

image-20220224111228064

image-20220224111240234

image-20220224111257997

image-20220224111315373

image-20220224111334828

导入数据库

配置mysql的bin目录到系统环境变量

image-20220224111517568

image-20220224111625111

windows命令行登录进入数据库

mysql -uroot -p密码

image-20220224111851621

创建两个数据库tensquare_gathering, tensquare_user

create database tensquare_gathering;
 create database tensquare_user;
 show databases;

image-20220224112345174

导入数据库

use tensquare_user;

 source c:/Users/wangxiang/tensquare_user.sql;

image-20220224113046771

use tensquare_gathering;
source c:/Users/wangxiang/tensquare_gathering.sql;

image-20220224113315452

四:修改微服务的配置文件,启动微服务

eureka_server

修改配置文件

在eureka_server 项目----->src---->main--->resources---->application.yml

# 单机版
server:
  port: 10086

#基本服务器信息
spring:
  application:
    name: eureka-server  #服务ID

#enreka服务器配置
eureka:
  client:
    fetch-registry: false    #单机版关闭enreka相互注册
    register-with-eureka: false
    service-url:
      defaultZone:  http://localhost:${server.port}/eureka #暴露eureka服务访问地址
  server:
    enable-self-preservation: false #关闭自我保护

启动服务,浏览器访问

在eureka_server 项目----->src0---->main--->java---com.tensquare.eureka-->EurekaServerApplication--->右击,点击run....

image-20220228100428787

配置都正确后,启动后会启动10086端口,使用浏览器访问localhost:10086

image-20220228100610008

image-20220228100850383

启动zuul 网关服务

修改zuul 服务的配置文件aaplication.yaml

image-20220228101156117

server:
  port: 10020 # 端口
  
# 基本服务信息
spring:
  application:
    name: tensquare-zuul # 服务ID
    
# Eureka配置
eureka:
  client:
    service-url:  
      defaultZone: http://localhost:10086/eureka # Eureka访问地址
  instance:
    prefer-ip-address: true
  
# 修改ribbon的超时时间
ribbon:
  ConnectTimeout: 1500 # 连接超时时间,默认500ms
  ReadTimeout: 3000  # 请求超时时间,默认1000ms
  
  
# 修改hystrix的熔断超时时间
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMillisecond: 2000 # 熔断超时时长,默认1000ms
            

# 网关路由配置
zuul:
  routes:
    admin:
      path: /admin/**
      serviceId: tensquare-admin-service
    gathering:
          path: /gathering/**
          serviceId: tensquare-gathering

 # jwt参数
jwt:
  config:
    key: itcast
    ttl: 1800000

启动服务

启动 ZuulApplication

image-20220228101239536

在eureka界面,可以看到zuul 已经注册了

image-20220228101436510

启动权限中心admin_service

修改配置文件

image-20220228102348670

server: 
  port: 9001
spring: 
  application:  
    name: tensquare-admin-service #指定服务名
  datasource:  
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/tensquare_user?characterEncoding=UTF8   #jdbc连接数据库
    username: root       #数据库用户名和密码
    password: abc123
  jpa: 
    database: mysql
    show-sql: true

#Eureka配置
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka,http://192.168.66.104:10086/eureka
  instance:
    lease-renewal-interval-in-seconds: 5 # 每隔5秒发送一次心跳
    lease-expiration-duration-in-seconds: 10 # 10秒不发送就过期
    prefer-ip-address: true


 # jwt参数
jwt:
  config:
    key: itcast
    ttl: 1800000

启动AdminApplication

image-20220228102506450

image-20220228102600098

在eureka界面,可以看到admin-service 已经注册

image-20220228102719764

开启业务微服务gathering

修该配置文件

image-20220228103013774

server: 
  port: 9002
spring: 
  application:  
    name: tensquare-gathering #指定服务名
  datasource:  
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/tensquare_gathering?characterEncoding=UTF8
    username: root
    password: abc123
  jpa: 
    database: mysql
    show-sql: true
#Eureka客户端配置
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka
  instance:
    lease-renewal-interval-in-seconds: 5 # 每隔5秒发送一次心跳
    lease-expiration-duration-in-seconds: 10 # 10秒不发送就过期
    prefer-ip-address: true

启动GatheringApplication

image-20220228103143204

image-20220228103224550

image-20220228103306224

五:安装postman,使用postman进行测试

链接:https://pan.baidu.com/s/1rI97F8RHnOs-OoslBmN_GA?pwd=duyp
提取码:duyp

登录用户名,存放在导入的数据库中,登录微服务时,微服务去数据库中验证,只用数据库中的用户名和密码,才允许登录

image-20220228110645055

安装postman

image-20220228103735539

使用post 方式提交, 访问localhost:10020 ,对网关zuul 进行访问

http://localhost:10020/admin/admin/login

在body中,使用json 方式,以raw 行为添加,添加用户名密码

{
    "loginname":"admin",
    "password":"123456"
}

eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIxMTk0MjgxNTMzMjMwNDE5OTY4Iiwic3ViIjoiYWRtaW4iLCJpYXQiOjE2NDYwMTY3MzQsInJvbGVzIjoiYWRtaW4iLCJleHAiOjE2NDYwMTg1MzR9.HOA1SkO1se23nx0FqudRQGz5kAbL-K8fDD8-4NOQPsY

image-20220228111709240

六:将微服务打包,本地运行

在整个项目的配置pom.xml 文件中,添加配置

image-20220228113352506

<build>
    <plugins>
        <plugin>
            <!--提供打包(将应用打包成可执行的jar包)-->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <!-- 指定maven编译的jdk版本 -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <verbose>true</verbose>
                <fork>true</fork>
                <!--jdk地址-->
                <executable>C:/Program Files/Java/jdk1.8.0_152/bin/javac</executable>
            </configuration>
        </plugin>
    </plugins>
</build>

在idea里先停止EurekaServerApplication的运行

image-20220228112932381

image-20220228113134928

在弹出的命令行界面,输入打包命令mvn clean package

image-20220228113829160

打包成功后,生成了jar包,放在项目eureka的target目录中

image-20220228114007256

进入到target 目录

image-20220228114156706

在弹出的命令行, 使用java -jar tensquare_eureka_server-1.0-SNAPSHOT.jar 进行运行(idea里的eureka 需要先关闭)

image-20220228114251797

运行成功后,使用浏览器访问localhost:10086端口

image-20220228114536368

image-20220228114619752

至此,微服务后端部分连接数据数据库配置都完成。后续将配置前端方面NodeJS+VueJS+ElementUI

七:安装Nodejs和python2

链接:https://pan.baidu.com/s/16ohy8f4hx0whWNkOSJinMA?pwd=dgw0
提取码:dgw0

image-20220228114913059

image-20220228114925823

image-20220228114944398

image-20220228114954227

image-20220228115002519

安装完毕后,将nodejs安装目录将npm命令,加入到系统path 环境变量中在命令行使用npm -v 命令

image-20220228115109255

进入前端项目目录,启动cmd

前端项目:

链接:https://pan.baidu.com/s/1z58VWbMHSS6VWZmcqC4hPg?pwd=0xzn
提取码:0xzn

image-20220228120256429

使用npm run dev 启动,此时会报错,原因是没有安装 python2

image-20220228120455696

npm 启动报错

error in ./src/views/dashboard/index.vue

Module build failed: Error: Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime (93)

image-20220228120614190

安装python2.7,全部默认即可

链接:https://pan.baidu.com/s/1AtI8-rIJnOfyKKS_kTdJ7g?pwd=7ldo
提取码:7ldo

安装好Python2.7后,将python2.7安装目录,加入到path环境变量中

来到前端项目目录,输入cmd,在当前前端项目目录打开命令行

image-20220228223018773

image-20220228223039812

使用命令安装cnpm 命令。 由于npm 的服务器在国外,所以在使用npm执行时,可能会因为网的问题报错

npm install -g cnpm --registry=https://registry.npm.taobao.org

image-20220228224038955

在tensquareAdmin目录下执行cnpm uninstall node-sass

img

执行cnpm install node-sass 命令

image-20220228224158345

此时,在执行cnpm run dev 命令

image-20220228224411384

启动后,出现这个登录页面。用户名admin 密码123456

此时还无法登录,因为前面的几个微服务关闭了。按照顺序,依次启动eureka,zuul,admin_service ,gathering 服务

image-20220228224632516

image-20220228225223288

image-20220228225237803

八,安装运行VSCode

安装

image-20220228112032120

image-20220228112104768

image-20220228112117454

image-20220228112131795

image-20220228112141081

在安装完毕,启动进入后,可以选择安装中文包

image-20220228112304062

使用VSConde 打开前端项目

image-20220228115415286

image-20220228115710295

打开项目后,在config文件夹下,index.js就是项目的首页文件

image-20220228230050771

打开项目的终端。在顶部选择终端--->新建终端

image-20220228230128861

此时可以在终端,vsconde的终端,使用npm run dev 命令,也可以运行前端项目

image-20220301000240820

image-20220301000250758

注:vscode无法直接执行cnpm命令,需要以管理员身份打开才行(选择vscode图标,右击,以管理员身份运行)

在vscode 中直接运行 cnpm run dev 命报错如下:

cnpm : 无法加载文件 C:\Users\wangxiang\AppData\Roaming\npm\cnpm.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。

image-20220301001311241

image-20220301001757318

此时需要打开权限

image-20220301001527227

此时再运行cnpm run dev 命令成功

image-20220301001600562

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值