itoken專案簡介開發環境作業系統: Windows 10 Enterprise開發工具: Intellij IDEA資料庫: MySql 5.7.22Java SDK: Oracle JAK 1.8.152
部署環境
作業系統: Linux Ubuntu Server 16.04 X64虛擬化技術: VMware + Docker專案管理工具
專案構建: Maven + Nexus程式碼管理: Git + GitLab映象管理: Docker Registry後臺主要技術棧
核心框架: Spring Boot + Spring Cloud檢視框架: Spring MVC頁面引擎: ThymeleafORM框架: tk.mybatis 簡化 MyBatis 開發資料庫連線池: Alibaba Druid資料庫快取: Redis Sentinel訊息中介軟體: RabbitMQ介面文件引擎: Swagger2 RESTful 風格 API 文件生成全文檢索引擎: ELasticSearch分散式鏈路追中: ZipKin分散式檔案系統: Alibaba FastDFS分散式服務監控: Spring Boot Admin分散式協調系統: Spring Cloud Eureka分散式配置中心: Spring Cloud Config分散式日誌系統: ELK(ElasticSearch + Logstash + Kibana)反向代理負載均衡: Nginx前段主要技術棧
前端框架: Bootstrap + JQurey前端模板: AdminLTE自動化運維
持續整合: GitLab持續交付: Jenkins容器編排: Kubernetes專案架構
建立專案組和專案
stages: - build - push - run - cleanbuild: 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-configrun: stage: run script: - cd docker - docker-compose down - docker-compose up -dclean: 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-networknetworks: eureka-network:複製程式碼
生產環境專案秒速回滾使用linux的ln軟連結命令使用docker的映象重啟回滾到以前版本使用微服務開發管理員服務新增druid連線池和mysql資料庫配置新增pom.xml依賴<spring-boot-alibaba-druid.version>1.1.10</spring-boot-alibaba-druid.version><mysql.version>5.1.46</mysql.version> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${spring-boot-alibaba-druid.version}</version></dependency><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> <scope>runtime</scope></dependency>複製程式碼
增加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複製程式碼
配置tk.mybatis簡化mybatis開發新增依賴包<dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.0.2</version></dependency>複製程式碼新增mybatis配置新增MyMapper介面新增PageHelper類方法配置程式碼自動生成外掛測試驅動程式設計的方式進行敏捷開發(TDD)編寫測試用例(登陸和註冊的測試用例)根據用例方法編寫實現靜態檔案部署方式(nginx-CDN內容分發網路)基於埠的虛擬主機配置新增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複製程式碼新增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. keepalived2. zookeeper3. sentinel(官方推薦)複製程式碼搭建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複製程式碼搭建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 26379dir /tmp# 自定義叢集名,其中 127.0.0.1 為 redis-master 的 ip, 6379 redis-master 的埠,2 為最小投票數 (因為有 3 臺 Sentinel 所以可以設定成 2)sentinel monitor mymaster 127.0.0.1 6379 2sentinel down-after-milliseconds mymaster 30000sentinel parallel-syncs mymaster 1sentinel failover-timeout mymaster 180000sentinel deny-scripts-reconfig yes複製程式碼使用lettuce連上redis服務搭建redis的服務提供者(提供快取服務)新增redis的pom配置<dependency> <groupId>org.apache.commons</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency>複製程式碼新增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請求文字會跨域)使用CORS(跨資源共享)解決跨域問題* IE瀏覽器版本不能低於10伺服器端實現CORS介面即可,在header中設定:Access-Control-Allow-Origin複製程式碼使用JSONP解決跨域問題使用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 實現二級快取(資料不常變化,減輕資料庫壓力)MyBatis快取介紹## 一級快取MyBatis會在表示會話的sqlSession物件中建立一個簡單的快取,將每次查詢到的結果快取起來,當下次查詢的時候如果判斷先前有個完全一樣的查詢會直接從快取返回資料(記憶體級別)## 二級快取(第三方快取)-快取共享複製程式碼配置 MyBatis二級快取開啟MyBatis 二級快取mybatis: configuration: cache-enabled: true複製程式碼idea增加serialVersionUID提示設定實體類實現序列化介面並宣告序列號
private static final long serialVersionUID = 8461546412131L;複製程式碼
建立相關工具類
實現Spring ApplicationContextAware介面,用於手動注入BeanApplicationContextHolder複製程式碼
實現MyBatis Cache介面,用於自定義快取為RedisMapper介面中增加註解Spring Boot配置Swagger2介面文件引擎新增maven包配置
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.8.0</version></dependency><dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.8.0</version></dependency>複製程式碼
配置Swagger2(Java配置方式)啟動類添加註解訪問連結: http://ip:/swagger-ui.html
FastDFS的使用建立docker-compose.yml檔案version: '3.1'services: fastdfs: build: environment restart: always container_name: fastdfs volumes: - ./storage:/fastdfs/storage network_mode: host複製程式碼
建立Dockerfile檔案
FROM ubuntu:xenialMAINTAINER [email protected]# 更新資料來源WORKDIR /etc/aptRUN echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse' > /etc/apt/sources.listRUN echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse' >> /etc/apt/sources.listRUN echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse' >> /etc/apt/sources.listRUN echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse' >> /etc/apt/sources.listRUN apt-get update# 安裝依賴RUN apt-get install make gcc libpcre3-dev zlib1g-dev --assume-yes# 複製工具包ADD fastdfs.tar.gz /usr/local/srcADD fastdfs-nginx-module.tar.gz /usr/local/srcADD libfastcommon.tar.gz /usr/local/srcADD nginx-1.15.4.tar.gz /usr/local/src# 安裝 libfastcommonWORKDIR /usr/local/src/libfastcommonRUN ./make.sh && ./make.sh install# 安裝 FastDFSWORKDIR /usr/local/src/fastdfsRUN ./make.sh && ./make.sh install# 安裝 FastDFS跟蹤器ADD tracker.conf /etc/fdfsRUN mkdir -p /fastdfs/tracker# 配置 FastDFS 儲存ADD storage.conf /etc/fdfsRUN mkdir -p /fastdfs/storage# 配置 FastDFS 客戶端ADD client.conf /etc/fdfs# 配置 fastdfs-nignx-moduleADD config /usr/local/src/fastdfs-nginx-module/src# FastDFS 與 Nginx 整合WORKDIR /usr/local/src/nginx-1.15.4RUN ./configure --add-module=/usr/local/src/fastdfs-nginx-module/srcRUN make && make installADD mod_fastdfs.conf /etc/fdfsWORKDIR /usr/local/src/fastdfs/confRUN cp http.conf mine.types /etc/fdfs/# 配置 NginxADD nginx.conf /usr/local/nginx/confCOPY entrypoint.sh /usr/local/bin/ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]WORKDIR /EXPOSE 8888CMD ["/bin/bash"]複製程式碼
搭建fastDFS客戶端安裝fastDFS客戶端(新增fastdfs-client-java依賴)<dependency> <groupId>org.csource</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.29-SNAPSHOT</version></dependency>複製程式碼
新增雲配置
fastdfs.base.url: http://192.168.94.132:8887/storage: type: fastdfs fastdfs: tracker_server: 192.168.94.132:22122複製程式碼
RabbitMQ的使用RabbitMQ的安裝編寫docker-compose.yml檔案
version: '3.1'services: rabbitmq: restart: always image: rabbitmq:management container_name: rabbitmq ports: - 5672:5672 - 15672:15672 environment: TZ: Asia/Shanghai RABBITMQ_DEFAULT_USER: rabbit RABBITMQ_DEFAULT_PASS: 123456 volumes: - ./data:/var/lib/rabbitmq複製程式碼
RabbitMQ的使用作者:H-YuanC連結:https://juejin.im/post/5ea242e051882573663f7601
最新評論