Apollo部署及整合SpringBoot实战

前言:

最近在完成毕业设计,题目是《基于微服务的B2C网上商城系统的设计与实现》,整个项目最早是我在看黑马的培训视频时学习JavaWeb做的一个项目,第一次做完大约在去年过年的时候,这个项目前前后后自己改了差不多5、6次了,使用技术不断更新(还有一些小细节的修改比如最早购物车用Session后来用Redis存储),从最早的Servlet+Filter到SSM+Redis到SpringBoot+Thymeleaf+Redis到SpringBoot+Thymeleaf+Redis+RabbitMQ+Eureka+Spring Cloud Config(这时候已经彻底前后台分离了),现在做的应该是这个项目的最后一个版本了,整体采用SpringBoot+Spring Cloud(用到了Eureka、Ribbon、Fegin后面可能还会加一些但是部署起来要顶不住了)+Redis+RabbitMQ

这次Eureka做了三个节点集群统一部署在阿里云,前台4个服务模块,每个模块两个节点部署在一台虚拟机上,web层调用做了一个节点部署在一台虚拟机上,还用Nginx做了图片服务器(这块参数还可以调整一下,最近也在学习Nginx),目前还没有做的是后台的部署,之前发现的一些小BUG的修改还有集成配置中心(今天在做)

Eureka截图:(小有成就感)

在这里插入图片描述

此次的配置中心采用了携程开源的Apollo,也是我来公司接触公司的新系统中采用的,感觉比SpringCloud提供的ConfigServer好用很多,正好借这次毕业设计的机会好好学习一下Apollo

一、Apollo分布式配置中心简介

Apollo(阿波罗)是携程的分布式配置中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性,适用于微服务配置管理场景

Apollo基础模型:

  • 用户在配置中心对配置进行修改并发布
  • 配置中心通知Apolllo客户端有配置更新
  • Apollo客户端从配置中心拉取最新的配置、更新本地配置并通知到应用

在这里插入图片描述

Apollo对比Spring Cloud Config:

在这里插入图片描述

二、修改配置

官方GitHub:https://github.com/ctripcorp/apollo

1)、从GitHub上源码下载下来

其中几个比较重要的项目:

  • apollo-configservice:提供配置获取接口,提供配置更新推送接口,接口服务对象为Apollo客户端
  • apollo-adminservice:提供配置管理接口,提供配置修改、发布等接口,接口服务对象为Portal以及Eureka
  • apollo-portal:提供Web界面供用户管理配置
  • apollo-client:Apollo提供的客户端程序,为应用提供配置获取、实时更新等功能

在这里插入图片描述

配置发布的大致过程:用户在Portal操作配置发布,Portal调用Admin Service的接口操作发布,Admin Service发布配置后,发送ReleaseMessage给各个Config Service,Config Service收到ReleaseMessage后,通知对应的客户端

2)、数据库初始化

  • ApolloPortalDB:执行apollo-master\scripts\docker-quick-start\sql\apolloportaldb.sql
  • ApolloConfigDB:执行apollo-master\scripts\docker-quick-start\sql\apolloconfigdb.sql

3)、调整配置并打包

修改apollo-master\scripts中的配置文件build.bat或者build.sh

A.数据库配置

# apollo config db info
apollo_config_db_url=jdbc:mysql://xxx:3306/ApolloConfigDB?characterEncoding=utf8
apollo_config_db_username=root
apollo_config_db_password=123456

# apollo portal db info
apollo_portal_db_url=jdbc:mysql://xxx:3306/ApolloPortalDB?characterEncoding=utf8
apollo_portal_db_username=root
apollo_portal_db_password=123456

B.修改环境调用地址

# meta server url, different environments should have different meta server addresses
dev_meta=http://localhost:8080
fat_meta=http://localhost:8080
uat_meta=http://localhost:8080
pro_meta=http://localhost:8080

C.修改数据库数据

在ApolloConfigDB数据库中,找到表ServerConfig中的eureka.service.url配置项修改为http://192.168.126.146:8080/eureka/

D.编译打包

$ ./build.sh

该脚本会依次打包apollo-configservice, apollo-adminservice, apollo-portal和apollo-client

三、部署

1)、部署apollo-configservice

将apollo-master\apollo-configservice\target目录下的apollo-configservice-1.4.0-SNAPSHOT-github.zip上传到服务器解压

修改scripts/startup.sh

SERVICE_NAME=apollo-configservice
## Adjust log dir if necessary
LOG_DIR=/shop-apollo/logs
## Adjust server port if necessary
SERVER_PORT=8080

2)、部署apollo-adminservice

将apollo-master\apollo-adminservice\target目录下的apollo-adminservice-1.4.0-SNAPSHOT-github.zip上传到服务器解压

修改scripts/startup.sh

SERVICE_NAME=apollo-adminservice
## Adjust log dir if necessary
LOG_DIR=/shop-apollo/logs
## Adjust server port if necessary
SERVER_PORT=8090

3)、部署apollo-portal

将apollo-master\apollo-portal\target目录下的apollo-portal-1.4.0-SNAPSHOT-github.zip上传到服务器解压

修改scripts/startup.sh

SERVICE_NAME=apollo-portal
## Adjust log dir if necessary
LOG_DIR=/shop-apollo/logs
## Adjust server port if necessary
SERVER_PORT=8070

执行scripts目录下的startup.sh启动apollo-configservice、apollo-adminservice和apollo-portal

nohup ./startup.sh &

启动成功后访问客户端:192.168.126.146:8070

默认用户名密码为:apollo/admin

四、SpringBoot整合使用Apollo配置中心

1)、添加Apollo依赖

		<!--apollo相关jar包 -->
		<dependency>
			<groupId>com.ctrip.framework.apollo</groupId>
			<artifactId>apollo-client</artifactId>
			<version>1.1.0</version>
		</dependency>
		<dependency>
			<groupId>com.ctrip.framework.apollo</groupId>
			<artifactId>apollo-core</artifactId>
			<version>1.1.0</version>
		</dependency>

2)、编写application.yml文件

spring:
  application:
    name: shop-web
app:
  id: shop-web
# set apollo meta server address, adjust to actual address if necessary
apollo:
  meta: http://192.168.126.146:8080/

3)、主启动类上使用@EnableApolloConfig注解标注

参考:https://blog.csdn.net/z960339491/article/details/80667559

https://blog.csdn.net/m0_37367413/article/details/84074715

猜你喜欢

转载自blog.csdn.net/qq_40378034/article/details/87925546