5 Apollo应用于分布式系统

5 Apollo应用于分布式系统

在微服务架构模式下,项目往往会切分成多个微服务,下面将以万信金融P2P项目为例演示如何在项目中使用。

5.1 项目场景介绍

5.1.1 项目概述

万信金融是一款面向互联网大众提供的理财服务和个人消费信贷服务的金融平台,依托大数据风控技术,为用户提供方便、快捷、安心的P2P金融服务。本项目包括交易平台和业务支撑两个部分,交易平台主要实现理财服务.

包括:借钱、出借等模块,业务支撑包括:标的管理、对账管理、风控管理等模块。项目采用先进的互联网技术进行研发,保证了P2P双方交易的安全性、快捷性及稳定性。

5.1.2 各微服务介绍

本章节仅仅是为了演示配置中心,所以摘取了部分微服务,如下:

用户中心服务(consumer-service):为借款人和投资人提供用户账户管理服务,包括:注册、开户、充值、提现等UAA认证服务(uaa-service):为用户中心的用户提供认证服务

统一账户服务(account-service):对借款人和投资人的登录平台账号进行管理,包括:注册账号、账号权限管理等交易中心(transaction-service):负责P2P平台用户发标和投标功能

5.2 Spring Boot应用集成

下面以集成统一账户服务(account-service)为例

5.2.1 导入工程

参考account-service、transaction-service、uaa-service、consumer-service工程,手动创建这几个微服务。

每个工程必须添加依赖:

<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo‐client</artifactId>
<version>1.1.0</version>
</dependency>

下边是account-service依赖,其它工程参考“资料”下的“微服务”。

<?xml version="1.0" encoding="UTF‐8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven‐4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!‐‐ lookup parent from repository ‐‐>
</parent>
<groupId>cn.itcast</groupId>
<artifactId>account‐service</artifactId>
<version>0.0.1‐SNAPSHOT</version>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐logging</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐log4j2</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐configuration‐processor</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo‐client</artifactId>
<version>1.1.0</version>
</dependency>

</dependencies>

</project>

5.2.2 必选配置

  1. AppId:在Spring Boot application.properties或application.yml中配置 application.properties

app.id=account‐service

application.yml

app:
id: account‐service
  1. apollo.bootstrap 集成springboot,开启apollo.bootstrap,指定namespace 示例:

apollo.bootstrap.enabled = true
apollo.bootstrap.namespaces = application,micro_service.spring‐boot‐http,spring-rocketmq,micro_service.spring‐boot‐druid
  1. Apollo Meta Server

Apollo支持应用在不同的环境有不同的配置,常用的指定方式有如下两种:

  • 第一种:通过Java System Property的apollo.meta: -Dapollo.meta=http://localhost:8080

  • 第二种:在resources目录下新建apollo-env.properties文件

dev.meta=http://localhost:8080
pro.meta=http://localhost:8081
  1. 本地缓存路径 Apollo客户端会把从服务端获取到的配置在本地文件系统缓存一份,用于在遇到服务不可用,或网络不通的 时候,依然能从本地恢复配置,不影响应用正常运行。本地配置文件会以下面的文件名格式放置于配置的本 地缓存路径下:{appId}+{cluster}+{namespace}.properties

image-20210508093228181

可以通过如下方式指定缓存路径,通过Java System Property的apollo.cacheDir:

‐Dapollo.cacheDir=/opt/data/apollo‐config
  1. Environment 通过Java System Property的env来指定环境:-Denv=DEV

  2. Cluster(集群) 通过Java System Property的apollo.cluste来指定集群:-Dapollo.cluster=DEFAULT 也可以选择使用之前新建的SHA JQ集群: -Dapollo.cluster=SHAJQ

  3. 完整的VM Options如下:

‐Denv=DEV ‐Dapollo.cacheDir=/opt/data/apollo‐config ‐Dapollo.cluster=DEFAULT

image-20210508093303599

5.2.3 启用配置

在咱们应用的启动类添加@EnableApolloConfig注解即可:

@SpringBootApplication(scanBasePackages = "cn.itcast.account")
@EnableApolloConfig
public class AccountApplication {

public static void main(String[] args) {
SpringApplication.run(AccountApplication.class, args);
}
}

5.2.4 应用配置

  1. 将local-config/account.properties中的配置添加到apollo中

swagger.enable=true
sms.enable=true

spring.http.encoding.charset=UTF‐8
spring.http.encoding.force=true
spring.http.encoding.enabled=true
server.use‐forward‐headers=true
server.tomcat.protocol_header=x‐forwarded‐proto
server.servlet.context‐path=/account‐service
server.tomcat.remote_ip_header=x‐forwarded‐for

spring.datasource.driver‐class‐name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.stat‐view‐servlet.allow=127.0.0.1,192.168.163.1
spring.datasource.druid.web‐stat‐filter.session‐stat‐enable=false
spring.datasource.druid.max‐pool‐prepared‐statement‐per‐connection‐size=20
spring.datasource.druid.max‐active=20
spring.datasource.druid.stat‐view‐servlet.reset‐enable=false
spring.datasource.druid.validation‐query=SELECT 1 FROM DUAL
spring.datasource.druid.stat‐view‐servlet.enabled=true
spring.datasource.druid.web‐stat‐filter.enabled=true
spring.datasource.druid.stat‐view‐servlet.url‐pattern=/druid/*
spring.datasource.druid.stat‐view‐servlet.deny=192.168.1.73
spring.datasource.url=jdbc\:mysql\://127.0.0.1\:3306/p2p_account?useUnicode\=true
spring.datasource.druid.filters=config,stat,wall,log4j2
spring.datasource.druid.test‐on‐return=false
spring.datasource.druid.web‐stat‐filter.profile‐enable=true
spring.datasource.druid.initial‐size=5
spring.datasource.druid.min‐idle=5
spring.datasource.druid.max‐wait=60000
spring.datasource.druid.web‐stat‐filter.session‐stat‐max‐count=1000
spring.datasource.druid.pool‐prepared‐statements=true
spring.datasource.druid.test‐while‐idle=true
spring.datasource.password=itcast0430
spring.datasource.username=root
spring.datasource.druid.stat‐view‐servlet.login‐password=admin
spring.datasource.druid.stat‐view‐servlet.login‐username=admin
spring.datasource.druid.web‐stat‐filter.url‐pattern=/*
spring.datasource.druid.time‐between‐eviction‐runs‐millis=60000
spring.datasource.druid.min‐evictable‐idle‐time‐millis=300000
spring.datasource.druid.test‐on‐borrow=false
spring.datasource.druid.web‐stat‐filter.principal‐session‐name=admin
spring.datasource.druid.filter.stat.log‐slow‐sql=true
spring.datasource.druid.web‐stat‐filter.principal‐cookie‐name=admin
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.aop‐patterns=cn.itcast.wanxinp2p.*.service.*
spring.datasource.druid.filter.stat.slow‐sql‐millis=1
spring.datasource.druid.web‐statfilter.exclusions=*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*
  1. spring-http命名空间在之前已通过关联公共命名空间添加好了,现在来添加spring-boot-druid命名空间

image-20210508093433937

  1. 添加本地文件中的配置到对应的命名空间,然后发布配置

image-20210508093446699

  1. 在account-service/src/main/resources/application.properties中配置apollo.bootstrap.namespaces需要 引入的命名空间

app.id=account‐service
apollo.bootstrap.enabled = true
apollo.bootstrap.namespaces = application,micro_service.spring‐boot‐http,spring-rocketmq,
spring‐boot‐druid

server.port=63000

5.2.5 读取配置

  1. 启动应用

image-20210508093548226

  1. 访问:http://127.0.0.1:63000/account-service/hi,确认Spring Boot中配置的context-path是否生效

image-20210508093557190

通过/account-service能正常访问,说明apollo的配置已生效

image-20210508113413551

  1. 确认spring-boot-druid配置

  • 为了快速确认可以在AccountController中通过@Value获取来验证

        @GetMapping("/db‐url")
    public String getDBConfig(@Value("${spring.datasource.url}") String url) {
    return url;
    }
  • 访问http://127.0.0.1:63000/account-service/db-url,显示结果

image-20210508093720289

5.3.6 创建其它项目

参考account-service将其它项目也创建完成。

5.4 生产环境部署

当一个项目要上线部署到生产环境时,项目的配置比如数据库连接、RocketMQ地址等都会发生变化,这时候就需要通过Apollo为生产环境添加自己的配置。

5.4.1 企业部署方案

在企业中常用的部署方案为:Apollo-adminservice和Apollo-configservice两个服务分别在线上环境(pro),仿真环境(uat)和开发环境(dev)各部署一套,Apollo-portal做为管理端只部署一套,统一管理上述三套环境。 具体如下图所示:

image-20210508093804903

下面以添加生产环境部署为例

5.4.2 创建数据库

创建生产环境的ApolloConfigDB:每添加一套环境就需要部署一套ApolloConfgService和ApolloAdminService source apollo/ApolloConfigDB_PRO__initialization.sql

5.4.3 配置启动参数

  1. 设置生产环境数据库连接

  2. 设置ApolloConfigService端口为:8081,ApolloAdminService端口为8091

#!/bin/sh

url="localhost:3306"
username="root"
password="123456"

java -Xms256m -Xmx256m -Dapollo_profile=github -Dspring.datasource.url=jdbc:mysql://${url}/ApolloConfigDBPRO?characterEncoding=utf8 -Dspring.datasource.username=${username} -Dspring.datasource.password=${password} -Dlogging.file=./logs/apollo-configservice.log -Dserver.port=8081 -jar apollo-configservice-1.3.0.jar &
java -Xms256m -Xmx256m -Dapollo_profile=github -Dspring.datasource.url=jdbc:mysql://${url}/ApolloConfigDBPRO?characterEncoding=utf8 -Dspring.datasource.username=${username} -Dspring.datasource.password=${password} -Dlogging.file=./logs/apollo-adminservice.log -Dserver.port=8091 -jar apollo-adminservice-1.3.0.jar
  1. 运行runApollo-PRO.sh

5.4.4 修改Eureka地址

更新生产环境Apollo的Eureka地址:

USE ApolloConfigDBPRO;

UPDATE ServerConfig SET `Value` = "http://localhost:8081/eureka/" WHERE `key` =
"eureka.service.url";

5.4.5 调整ApolloPortal服务配置

服务配置项统一存储在ApolloPortalDB.ServerConfig表中,可以通过管理员工具 - 系统参数页面进行配置: apollo.portal.envs - 可支持的环境列表

image-20210508094149833

默认值是dev,如果portal需要管理多个环境的话,以逗号分隔即可(大小写不敏感),如:

dev,pro

5.4.6 启动ApolloPortal

Apollo Portal需要在不同的环境访问不同的meta service(apollo-configservice)地址,所以我们需要在配置中提供这些信息。

‐Ddev_meta=http://localhost:8080/ ‐Dpro_meta=http://localhost:8081/
  1. 关闭之前启动的ApolloPortal服务,使用runApolloPortal.sh启动多环境配置

#!/bin/sh

url="localhost:3306"
username="root"
password="123456"

java -Xms256m -Xmx256m -Dapollo_profile=github,auth -Ddev_meta=http://localhost:8080/ -Dpro_meta=http://localhost:8081/ -Dserver.port=8070 -Dspring.datasource.url=jdbc:mysql://${url}/ApolloPortalDB?characterEncoding=utf8 -Dspring.datasource.username=${username} -Dspring.datasource.password=${password} -jar apollo-portal-1.3.0.jar
  1. 启动之后,点击account-service服务配置后会提示环境缺失,此时需要补全上边新增生产环境的配置

image-20210508094425806

  1. 点击补缺环境

image-20210508094437455

  1. 补缺过生产环境后,切换到PRO环境后会提示有Namespace缺失,点击补缺

image-20210508094447712

image-20210508094452218

  1. 从dev环境同步配置到pro

5.4.7 验证配置

  1. 同步完成后,切换到pro环境,修改生产环境rocketmq地址后发布配置

image-20210508094514563

  1. 配置项目使用pro环境,测试配置是否生效

image-20210508094615636

 

posted @ 2021-05-08 13:19  孤独的小人物  阅读(308)  评论(0)    收藏  举报