Spring Boot+Spring Cloud实现itoken项目(2)

执行多个初始化命令

#ENTRYPOINT [“dockerize”, “-timeout”, “5m”, “-wait”, “tcp://192.168.75.128:8888”, “java”, “-Djava.security.egd=file:/dev/./urandom”, “-jar”, “”]

ENTRYPOINT [“java”, “-Djava.security.egd=file:/dev/./urandom”, “-jar”, “/app/app.jar”, “–spring.profiles.active=prod”]

EXPOSE 8888

复制代码

  • 增加docker-compose.yml文件

Spring Boot+Spring Cloud实现itoken项目

version: ‘3.1’

services:

itoken-config:

restart: always

image: 47.112.215.6:5000/itoken-config

container_name: itoken-config

ports:

  • 8888:8888

复制代码

  • 增加.gitlab-ci.yml文件

Spring Boot+Spring Cloud实现itoken项目

stages:

  • build

  • push

  • run

  • clean

build:

stage: build

script:

  • /usr/local/maven/apache-maven-3.6.3/bin/mvn clean package

  • cp target/itoken-config-1.0.0-SNAPSHOT.jar docker/

  • cd docker

  • docker build -t 47.112.215.6:5000/itoken-config .

push:

stage: push

script:

  • docker push 47.112.215.6:5000/itoken-config

run:

stage: run

script:

  • cd docker

  • docker-compose down

  • docker-compose up -d

clean:

stage: clean

script:

  • docker rmi $(docker images -q -f dangling=true)

复制代码

  • 将上述文件提交到gitLab即可触发脚本

  • 启动多个runner项目需要更改networks

version: ‘3.1’

services:

itoken-eureka:

restart: always

image: 47.112.215.6:5000/itoken-eureka

container_name: itoken-eureka

ports:

  • 8761:8761

networks:

  • eureka-network

networks:

eureka-network:

复制代码

生产环境项目秒速回滚

=============================================================================

  • 使用linux的ln软链接命令

  • 使用docker的镜像重启回滚到以前版本

使用微服务开发管理员服务

===============================================================================

  1. 添加druid连接池和mysql数据库配置
  • 添加pom.xml依赖

<spring-boot-alibaba-druid.version>1.1.10</spring-boot-alibaba-druid.version>

<mysql.version>5.1.46</mysql.version>

com.alibaba

druid-spring-boot-starter

${spring-boot-alibaba-druid.version}

mysql

mysql-connector-java

${mysql.version}

runtime

复制代码

  • 增加application.yml的datasource配置

datasource:

druid:

url: jdbc:mysql://ip:port/dbname?useUnicode=true&characterEncoding=utf-8&useSSL=false

username: root

password: 123456

initial-size: 1

min-idle: 1

max-active: 20

test-on-borrow: true

driver-class-name: com.mysql.jdbc.Driver

复制代码

Spring Boot+Spring Cloud实现itoken项目

  1. 配置tk.mybatis简化mybatis开发
  • 添加依赖包

tk.mybatis

mapper-spring-boot-starter

2.0.2

复制代码

  1. 添加mybatis配置

Spring Boot+Spring Cloud实现itoken项目

  1. 添加MyMapper接口

  2. 添加PageHelper类方法

  3. 配置代码自动生成插件

测试驱动编程的方式进行敏捷开发(TDD)

=======================================================================================

  1. 编写测试用例(登陆和注册的测试用例)

  2. 根据用例方法编写实现

静态文件部署方式(nginx-CDN内容分发网络)

============================================================================================

  • 基于端口的虚拟主机配置
  1. 添加docker-compose.yml文件

version: ‘3.1’

services:

nginx:

restart: always

image: nginx

container_name: nginx

ports:

  • 81:80

  • 9000:9000

volumes:

  • ./conf/nginx.conf:/etc/nginx/nginx.conf

  • ./wwwroot:/usr/share/nginx/wwwroot

复制代码

  1. 添加nginx.conf配置文件

worker_processes 1;

events {

worker_connections 1024;

}

http {

include /etc/nginx/mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

配置虚拟主机 192.168.75.145

server {

监听的ip和端口,配置 192.168.75.145:80

listen 80;

虚拟主机名称这里配置ip地址

server_name 192.168.75.145;

所有的请求都以 / 开始,所有的请求都可以匹配此 location

location / {

使用root 指令指定虚拟主机目录即网页存放目录

比如访问 http://ip/index.html 将找到 /usr/local/docker/nginx/wwwroot/html80/index.html

比如访问 http://ip/item/index.html 将找到 /usr/local/docker/nginx/wwwroot/html80/item/index.html

root /usr/share/nginx/html80;

指定欢迎页面,按从左到右顺序查找

index index.html index.html

}

#}

配置虚拟主机 192.168.75.245

server {

listen 8080;

server_name 192.168.75.145;

location / {

root /usr/share/nginx/wwwroot/html8080;

index index.html index.html;

}

}

}

复制代码

  • nginx的反向代理配置

server {

listen 80;

server_name itoken.mrchen.com;

location / {

proxy_pass http://192.168.75.128:9090;

index index.html index.htm;

}

}

复制代码

  • nginx的负载均衡

upstream myapp1 {

server 192.168.94.132:9090 weight=10;

server 192.168.94.132:9091 weight=10;

}

server {

listen 80;

server_name 192.168.94.132

location / {

proxy_pass http://myapp1;

index index.jsp index.html index.htm;

}

}

复制代码

  • nginx反向代理负载均衡实现伪CDN服务器

使用redis做数据缓存

===============================================================================

  • 缓存(数据查询、短连接、新闻内容、商品内容等等)

  • 通过Redis HA(Redis高可用)的方式部署Redis服务

Redis HA高可用实现的技术

  1. keepalived

  2. zookeeper

  3. sentinel(官方推荐)

复制代码

  1. 搭建redis集群 编辑docker-compose.yml文件

version: ‘3.1’

services:

master:

image: redis

container_name: redis-master

ports:

  • 6379:6379

slave1:

image: redis

container_name: redis-slave-1

ports:

  • 6380:6379

command: redis-server --slaveof redis-master 6379

slave2:

image: redis

container_name: redis-slave-2

ports:

  • 6381:6379

command: redis-server --slaveof redis-master 6379

复制代码

  1. 搭建sentinel高可用监控服务 编辑docker-compose.yml配置

version: ‘3.1’

services:

sentinel1:

image: redis

container_name: redis-sentinel-1

ports:

  • 26379:26379

command: redis-sentinel /usr/local/etc/redis/sentinel.conf

values:

  • ./sentinel1.conf:/usr/local/etc/redis/sentinel.conf

sentinel2:

image: redis

container_name: redis-sentinel-2

ports:

  • 26380:26379

command: redis-sentinel /usr/local/etc/redis/sentinel.conf

values:

  • ./sentinel2.conf:/usr/local/etc/redis/sentinel.conf

sentinel3:

image: redis

container_name: redis-sentinel-3

ports:

  • 26381:26379

command: redis-sentinel /usr/local/etc/redis/sentinel.conf

values:

  • ./sentinel3.conf:/usr/local/etc/redis/sentinel.conf

复制代码

  • 添加sentinel.conf配置复制代码

port 26379

dir /tmp

自定义集群名,其中 127.0.0.1 为 redis-master 的 ip, 6379 redis-master 的端口,2 为最小投票数 (因为有 3 台 Sentinel 所以可以设置成 2)

sentinel monitor mymaster 127.0.0.1 6379 2

sentinel down-after-milliseconds mymaster 30000

sentinel parallel-syncs mymaster 1

sentinel failover-timeout mymaster 180000

sentinel deny-scripts-reconfig yes

复制代码

  • 使用lettuce连上redis服务

搭建redis的服务提供者(提供缓存服务)

========================================================================================

  1. 添加redis的pom配置

org.apache.commons

spring-boot-starter-data-redis

org.springframework.boot

spring-boot-starter-data-redis

复制代码

  1. 添加redis的application.yml配置

spring:

redis:

lettuce:

pool:

max-active: 8

max-idle: 8

max-wait: -1ms

min-idle: 0

sentinel:

master: mymaster

nodes: 47.112.215.6:26379,47.112.215.6:26380,47.112.215.6:26381

复制代码

使用redis实现单点登录服务(需要解决同源策略)

============================================================================================

通过Cookie共享的方式

  • 首先,应用群域名要统一

  • 其次,应用群各系统使用的技术要相同

  • 第三,cookie本身不安全

通过Redis记录登陆信息的方式

复制代码

解决跨域问题(nginx请求文字会跨域)

=======================================================================================

  1. 使用CORS(跨资源共享)解决跨域问题
  • IE浏览器版本不能低于10

服务器端实现CORS接口即可,在header中设置:Access-Control-Allow-Origin

复制代码

  1. 使用JSONP解决跨域问题

  2. 使用Nginx反向代理解决跨域问题

worker_processes 1;

events {

worker_connections 1024;

}

http {

include /etc/nginx/mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

配置虚拟主机 192.168.75.245

server {

listen 80;

server_name 47.112.215.6;

location / {

add_header Access-Control-Allow-Origin *;

add_header Access-Control-Allow-Headers X-Requested-With;

add_header Access-Control-Allow-Methods GET,POST,OPTIONS;

root /usr/share/nginx;

index index.html index.html;

}

}

}

复制代码

Spring Boot MyBatis Redis 实现二级缓存(数据不常变化,减轻数据库压力)

===================================================================================================================

  1. MyBatis缓存介绍

一级缓存

MyBatis会在表示会话的sqlSession对象中建立一个简单的缓存,将每次查询到的结果缓存起来,当下次查询的时候如果判断先前有个完全一样的查询会直接从缓存返回数据(内存级别)

二级缓存(第三方缓存)-缓存共享

复制代码

  1. 配置 MyBatis二级缓存
  • 开启MyBatis 二级缓存

mybatis:

configuration:

cache-enabled: true

复制代码

  • idea增加serialVersionUID提示设置

Spring Boot+Spring Cloud实现itoken项目

  • 实体类实现序列化接口并声明序列号

private static final long serialVersionUID = 8461546412131L;复制代码

  • 创建相关工具类

实现Spring ApplicationContextAware接口,用于手动注入BeanApplicationContextHolder复制代码

  • 实现MyBatis Cache接口,用于自定义缓存为Redis

  • Mapper接口中增加注解

Spring Boot配置Swagger2接口文档引擎

==============================================================================================

  1. 添加maven包配置

io.springfox

springfox-swagger2

2.8.0

io.springfox

springfox-swagger-ui

2.8.0

复制代码

  1. 配置Swagger2(Java配置方式)

  2. 启动类添加注解

Spring Boot+Spring Cloud实现itoken项目

访问链接:

http://ip:/swagger-ui.html

FastDFS的使用

=============================================================================

  1. 创建docker-compose.yml文件

version: ‘3.1’

services:

fastdfs:

build: environment

restart: always

container_name: fastdfs

volumes:

  • ./storage:/fastdfs/storage

network_mode: host

复制代码

  1. 创建Dockerfile文件

FROM ubuntu:xenial

MAINTAINER mrchen@qq.com

更新数据源

WORKDIR /etc/apt

RUN echo ‘deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse’ > /etc/apt/sources.list

RUN echo ‘deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse’ >> /etc/apt/sources.list

RUN echo ‘deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse’ >> /etc/apt/sources.list

RUN echo ‘deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse’ >> /etc/apt/sources.list

RUN apt-get update

安装依赖

RUN apt-get install make gcc libpcre3-dev zlib1g-dev --assume-yes

复制工具包

ADD fastdfs.tar.gz /usr/local/src

ADD fastdfs-nginx-module.tar.gz /usr/local/src

ADD libfastcommon.tar.gz /usr/local/src

ADD nginx-1.15.4.tar.gz /usr/local/src

安装 libfastcommon

WORKDIR /usr/local/src/libfastcommon

RUN ./make.sh && ./make.sh install

安装 FastDFS

WORKDIR /usr/local/src/fastdfs

RUN ./make.sh && ./make.sh install

安装 FastDFS跟踪器

ADD tracker.conf /etc/fdfs

RUN mkdir -p /fastdfs/tracker

配置 FastDFS 存储

ADD storage.conf /etc/fdfs

RUN mkdir -p /fastdfs/storage

配置 FastDFS 客户端

ADD client.conf /etc/fdfs

配置 fastdfs-nignx-module

ADD config /usr/local/src/fastdfs-nginx-module/src

FastDFS 与 Nginx 集成

WORKDIR /usr/local/src/nginx-1.15.4

写在最后

还有一份JAVA核心知识点整理(PDF):JVM,JAVA集合,JAVA多线程并发,JAVA基础,Spring原理,微服务,Netty与RPC,网络,日志,Zookeeper,Kafka,RabbitMQ,Hbase,MongoDB,Cassandra,设计模式,负载均衡,数据库,一致性哈希,JAVA算法,数据结构,加密算法,分布式缓存,Hadoop,Spark,Storm,YARN,机器学习,云计算…

image

buntu/ xenial-backports main restricted universe multiverse’ >> /etc/apt/sources.list

RUN apt-get update

安装依赖

RUN apt-get install make gcc libpcre3-dev zlib1g-dev --assume-yes

复制工具包

ADD fastdfs.tar.gz /usr/local/src

ADD fastdfs-nginx-module.tar.gz /usr/local/src

ADD libfastcommon.tar.gz /usr/local/src

ADD nginx-1.15.4.tar.gz /usr/local/src

安装 libfastcommon

WORKDIR /usr/local/src/libfastcommon

RUN ./make.sh && ./make.sh install

安装 FastDFS

WORKDIR /usr/local/src/fastdfs

RUN ./make.sh && ./make.sh install

安装 FastDFS跟踪器

ADD tracker.conf /etc/fdfs

RUN mkdir -p /fastdfs/tracker

配置 FastDFS 存储

ADD storage.conf /etc/fdfs

RUN mkdir -p /fastdfs/storage

配置 FastDFS 客户端

ADD client.conf /etc/fdfs

配置 fastdfs-nignx-module

ADD config /usr/local/src/fastdfs-nginx-module/src

FastDFS 与 Nginx 集成

WORKDIR /usr/local/src/nginx-1.15.4

写在最后

还有一份JAVA核心知识点整理(PDF):JVM,JAVA集合,JAVA多线程并发,JAVA基础,Spring原理,微服务,Netty与RPC,网络,日志,Zookeeper,Kafka,RabbitMQ,Hbase,MongoDB,Cassandra,设计模式,负载均衡,数据库,一致性哈希,JAVA算法,数据结构,加密算法,分布式缓存,Hadoop,Spark,Storm,YARN,机器学习,云计算…

[外链图片转存中…(img-Gz76rvPG-1714451251739)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值