安装包
下载安装包
1 2 3 4 5
| apollo-configservice-x.x.x-github.zip
apollo-adminservice-x.x.x-github.zip
apollo-portal-x.x.x-github.zip
|
网址: https://github.com/ctripcorp/apollo/releases
获取sql
1 2 3
| apolloconfigdb.sql
apolloportaldb.sql
|
网址: https://github.com/ctripcorp/apollo/tree/master/scripts/sql
jdk
- Apollo服务端:1.8+
- Apollo客户端:1.7+
Mysql:
部署
目录:
D:\java\other\apollo
如图:
执行SQL,会生成两个库:
1 2 3
| ApolloPortalDB
ApolloConfigDB
|
如图:
配置:
打开\apollo-configservice\scripts\startup.sh,将端口改为:8111,
如下:
1
| SERVER_PORT=${SERVER_PORT:=8111}
|
打开\apollo-configservice\config\application-github.properties,修改mysql数据库连接字符串,
如下:
1 2 3 4
| # DataSource spring.datasource.url = jdbc:mysql://127.0.0.1:3306/ApolloConfigDB?characterEncoding=utf8&serverTimezone=UTC spring.datasource.username = root spring.datasource.password = 123456
|
打开\apollo-configservice\scripts\startup.sh,
打开cmd,输入:
如图:
打开\apollo-adminservice\config\application-github.properties,修改mysql数据库连接字符串, 如下:
1 2 3 4
| # DataSource spring.datasource.url = jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8&serverTimezone=UTC spring.datasource.username = root spring.datasource.password = 123456
|
打开\apollo-adminservice\scripts\startup.sh,
打开cmd,输入:
打开\apollo-portal\config\application-github.properties,修改mysql数据库连接字符串, 如下:
1 2 3 4
| # DataSource spring.datasource.url = jdbc:mysql://127.0.0.1:3306/ApolloPortalDB?characterEncoding=utf8&serverTimezone=UTC spring.datasource.username = root spring.datasource.password = 123456
|
默认当前项目中,可用环境为env,
如图:
打开\apollo-portal\config\apollo-env.properties,内容如下:
1 2 3 4 5 6
| local.meta=http://localhost:8111 dev.meta=http://localhost:8111 fat.meta=http://fill-in-fat-meta-server:8080 uat.meta=http://fill-in-uat-meta-server:8080 lpt.meta=${lpt_meta} pro.meta=http://fill-in-pro-meta-server:8080
|
打开\apollo-portal\scripts\startup.sh,
打开cmd,输入:
登录:
打开:http://127.0.0.1:8070/
输入账号/密码:apollo/admin
如图:
系统参数
点击“管理员工具”-“系统参数”,菜单进入应用配置页面,通过organizations关键字查询部门信息,如图:
修改value值,追加一个部门[mstest,微服务测试部门],修改完一分钟实时生效,内容如下:
1
| [{"orgId":"TEST1","orgName":"样例部门1"},{"orgId":"TEST2","orgName":"样例部门2"},{"orgId":"mstest","orgName":"微服务测试部门"}]
|
如图:
用户管理
填写用户信息,点击提交,一分钟实时生效,新增用户[msuser,msuser],
如图:
创建项目
现在创建项目,就可以选择刚才添加用户。在项目分配了负责人 msuser,如果使msuser登录的话,则只能看到他自己负责的项目。而 apollo 用户是超级管理员所以可以看到所有项目。
1 2 3 4 5 6 7
| 部门:指定的部门
AppId:应用唯一标识
应用名称: 应用的名称,建议格式 xx-yy-zz
应用负责人:指定的用户
|
如图:
Namespace
创建一个类型为private的Namespace,
如图:
权限管理
可以给指定用户添加对该应用的修改权和发布权。
如图:
新增配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/ApolloPortalDB?characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
如图:
客户端接入和操作
创建一个基础工程 Spring Boot,命名: apollodemo,增加依赖:
1 2 3 4 5 6 7 8 9 10
| <!-- 网址为:
https://mvnrepository.com/artifact/com.ctrip.framework.apollo/apollo-client
--> <dependency> <groupId>com.ctrip.framework.apollo</groupId> <artifactId>apollo-client</artifactId> <version>1.7.0</version> </dependency>
|
pom.xml 内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://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> <!-- springboot版本与springcloud须匹配 --> <version>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.test</groupId> <artifactId>apollodemo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>apollodemo</name> <description>Demo project for Spring Boot</description>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <!-- springboot版本与springcloud须匹配 --> <spring.boot.version>2.1.4.RELEASE</spring.boot.version> <spring.cloud.version>Greenwich.SR1</spring.cloud.version> <spring.maven.version>2.4.1</spring.maven.version> </properties>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
<dependency> <groupId>com.ctrip.framework.apollo</groupId> <artifactId>apollo-client</artifactId> <version>1.7.0</version> </dependency>
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.16</version> <scope>provided</scope> </dependency>
</dependencies>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring.cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring.maven.version}</version> </plugin> </plugins> </build>
</project>
|
application.yml,内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| server: port: 9111 # 端口
spring: application: name: msproj-demo # 应用名称
# apollo 相关配置 app: id: msproj # 与 Apollo 配置中心中的 AppId 一致
apollo: meta: http://localhost:8111 # Apollo 中的 Eureka 注册中心地址 #cluster: # 指定 Apollo 集群,相同集群实例使用对应集群的配置 #cacheDir: # 配置缓存目录,网络不可用时任然可提供配置服务 bootstrap: enable: true # 启用 apollo
env: DEV # 指定环境
|
ApollodemoApplication类,内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.test.apollodemo;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableApolloConfig({"application", "test1"})//注入 @SpringBootApplication public class ApollodemoApplication {
public static void main(String[] args) { SpringApplication.run(ApollodemoApplication.class, args); }
}
|
ConfigProperties类,内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.test.apollodemo.config;
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;
@Component @Data @NoArgsConstructor @AllArgsConstructor public class ConfigProperties {
@Value("${spring.datasource.url}") private String url; @Value("${spring.datasource.username}") private String username; @Value("${spring.datasource.password}") private String password; }
|
ConfigController类,内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| package com.test.apollodemo.controller;
import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.ConfigService; import com.test.apollodemo.config.ConfigProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap; import java.util.Map;
@RestController public class ConfigController {
@Autowired private ConfigProperties configProperties;
/** * 注入方式 * @return */ @GetMapping("/getDB") public Map<Object, Object> getDB() {
Map<Object, Object> map = new HashMap<Object, Object>(); map.put("url", configProperties.getUrl()); map.put("username", configProperties.getUsername()); map.put("password", configProperties.getPassword());
return map; }
/** * 用API方式获取 * @return */ @GetMapping("/getDBAPI") public Map<Object, Object> getDBAPI() {
Config config = ConfigService.getConfig("test1"); // 获取配置信息,第一个参数为配置项的 key,第二个参数为默认值(读取不到配置就会使用默认值,建议都加上默认值)
String url = config.getProperty("spring.datasource.url", null); String username = config.getProperty("spring.datasource.username", null); String password = config.getProperty("spring.datasource.password", null);
Map<Object, Object> map = new HashMap<Object, Object>(); map.put("url", url); map.put("username", username); map.put("password", password);
return map; } }
|
运行后,输入:http://localhost:9111/getDB
如图:
http://localhost:9111/getDBAPI
如图:
切换到apollo界面,修改Key为spring.datasource.url,点击发布,可以看看是否成功,
如图: