SpringCloud-Spring Cloud Config分
2019-04-16 本文已影响3人
zpwd63
分布式配置中心可以理解成 注册中心+配置中心的整合。具体实现思路:搭建一个注册中心,在注册中心的基础上添加配置中心。
注册中心搭建
- maven配置
<groupId>com.wk.sc</groupId>
<artifactId>springcloud-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</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>
</plugin>
</plugins>
</build>
- application.yml配置
server:
port: 6062
eureka:
instance:
hostname: localhost
client:
fetch-registry: false # 禁止拉取服务注册列表
register-with-eureka: false # 禁止当前服务注册
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # 定义服务注册地址
- 启动类
@EnableEurekaServer //开启服务注册
@SpringBootApplication
public class ConfigServerCluster1Application {
public static void main(String[] args) {
SpringApplication.run(ConfigServerCluster1Application.class, args);
}
}
配置中心
- maven配置
<groupId>com.wk.sc</groupId>
<artifactId>springcloud-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</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>
</plugin>
</plugins>
</build>
- application.yml配置
server:
port: 6063
spring:
application:
name: config-server-2
cloud:
config:
server:
git:
uri: https://gitee.com/umeone/springcloud-config.git # 配置文件存放git地址
search-paths: pro-test # 配置文件存放路径(文件夹),如果配置文件在项目根目录,则不需要配置该值
username: zpwd63@qq.com # git账号 当git项目创建为public时,不需要username和password
password: gi891023881225 # git密码
label: master # git配置文件所在分支
eureka:
client:
service-url:
defaultZone: http://localhost:6062/eureka/ # 配置注册中心地址
- 启动类
@EnableEurekaClient //注意此处需要同时开启注册中心和配置服务
@EnableConfigServer
@SpringBootApplication
public class ConfigServerCluster2Application {
public static void main(String[] args) {
SpringApplication.run(ConfigServerCluster2Application.class, args);
}
}
客户端配置
- maven配置
<groupId>com.wk.sc</groupId>
<artifactId>springcloud-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--配置中心集群需要添加以下依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</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>
</plugin>
</plugins>
</build>
- bootstrap.yml配置
# 注意此配置文件名为bootstrap.yml,而不是application.yml 后者不是默认读取文件名
server:
port: 6061
spring:
application:
name: config-client
cloud:
config:
label: master # 指定分支
profile: dev # 指定环境, 和配置文件后缀dev一致
uri: http://localhost:6063/ # 配置服务中心地址
name: config-client
# spring.cloud.config.name 值和配置文件的名称保持一定规则。如此处名称,在git中的配置文件名应为config-client-dev.properties ,前缀保持一致,后面的dev为不同环境的区分,这个规则和Springboot配置文件的环境区分类似。
# 如果该值没有配置,项目启动时会取spring.application.name值作为默认值(这仅仅是个猜测,通过更改name值进行测试,没有查看源码实现,要想确定是否猜测正确,需要从源码进行确认)
# URL与配置文件的映射关系:
# /{application}/{profile}[/{label}]
# /{application}-{profile}.yml
# /{label}/{application}-{profile}.yml
# /{application}-{profile}.properties
# /{label}/{application}-{profile}.properties
## 以下为配置高可用配置中心配置信息
discovery:
enabled: true # 启用从配置中心读取数据
service-id: config-server-2 # 服务名称
eureka:
client:
service-url:
defaultZone: http://localhost:6062/eureka/ # 服务注册地址
同样需要这里的配置文件名为:bootstrap.yml
- 启动类
@EnableEurekaClient //开启服务发现
@RestController
@SpringBootApplication
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
@Value("${file}") //如果配置中心获取失败则项目启动报错
private String file;
@Value("${word}")
private String word;
@RequestMapping(value = "/test")
public String test() {
return file + ":" + word;
}
}
分布式配置中心在配置信息的时候需要保持思路清新,特别是在配置对应配置中心地址和注册中心地址时,不要混淆这些地址。