springcloud config
分布式配置中心(Spring Cloud Config)(Finchley版本)
在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。
在原有项目的基础上 新增model,cloud-config-server,引入依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zhaowb.springcloud</groupId> <artifactId>cloud-config-server</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>cloud-config-server</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.example</groupId> <artifactId>cloud-demo</artifactId> <version>0.0.1-SNAPSHOT</version> </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.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置application.yml
server:
port: 8769
spring:
application:
name: cloud-config-server
cloud:
config:
server:
git:
uri: https://gitee.com/JiShiMoWang_admin/cloud-demo-config.git
default-label: master
我的不能使用SSH ,暂未解决。
在启动类上添加@EnableConfigServer
package com.zhaowb.springcloud.cloudconfigserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class CloudConfigServerApplication { public static void main(String[] args) { SpringApplication.run(CloudConfigServerApplication.class, args); } }
gitee上的配置为
spring: profiles: active: - dev --- spring: profiles: dev application: name: cloud-config-server-dev --- spring: profiles: test application: name: cloud-config-server-test
spring: application: name: cloud-config-server-test profiles: active: - dev
spring: application: name: cloud-config-server-dev profiles: active: - dev
说明配置生效。
http请求地址和资源文件映射如下:
-
/{application}/{profile}[/{label}]
-
/{application}-{profile}.yml
-
/{label}/{application}-{profile}.yml
-
/{application}-{profile}.properties
-
/{label}/{application}-{profile}.properties
都可以获取配置信息,看个人喜好选择。
高可用的分布式配置中心(Spring Cloud Config)
当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用,创建model,cloud-config-eureka-server、cloud-config-dept-client,
在server中引入依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zhaowb.springcloud</groupId> <artifactId>cloud-config-eureka-server</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>cloud-config-eureka-server</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.example</groupId> <artifactId>cloud-demo</artifactId> <version>0.0.1-SNAPSHOT</version> </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-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置bootstrap.yml
spring: cloud: config: name: cloud-config-eureka-server uri: http://localhost:8769/ label: master
配置application.yml
spring: application: name: cloud-config-eureka-server eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://localhost:8770/eureka/ server: port: 8770
原计划是将application.yml 放到git上,但是在实际操作中未能实现,暂未找到原因。可能是eureka-server 无法从git获取信息,或我配置文件错误。
在启动类上加上注解
package com.zhaowb.springcloud.cloudconfigeurekaserver; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableEurekaServer @RestController public class CloudConfigEurekaServerApplication { @Value("${file.name:mojhap}") private String fileName; public static void main(String[] args) { SpringApplication.run(CloudConfigEurekaServerApplication.class, args); } @RequestMapping(value = "/getFileName",method = RequestMethod.GET) public String getFileName(){ return fileName; } }
cloud-config-dept-client ,引入依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zhaowb.springcloud</groupId> <artifactId>cloud-config-dept-client</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>cloud-config-dept-client</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.example</groupId> <artifactId>cloud-demo</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>com.zhaowb.springcloud</groupId> <artifactId>cloud-api</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <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> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置bootstrap.yml
spring:
cloud:
config:
name: cloud-config-dept-client # 需要从github上读取的资源名称,注意没有yml后缀名
uri: http://localhost:8769/ # SpringCloudConfig获取的服务地址
label: master
配置application.yml
spring:
application:
name: cloud-config-dept-client
启动类加上注解
package com.zhaowb.springcloud.cloudconfigdeptclient; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient public class CloudConfigDeptClientApplication { public static void main(String[] args) { SpringApplication.run(CloudConfigDeptClientApplication.class, args); } }
其余的将eureka-hi的复制过来,修改一下使用。
git上配置
server:
port: 8771
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径
type-aliases-package: com.zhaowb.springcloud.cloudapi.entities # 所有Entity别名类所在包
mapper-locations:
- classpath:/mybatis/mapper/**/*.xml # mapper映射文件
spring:
application:
name: cloud-config-dept-client
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: com.mysql.jdbc.Driver # mysql驱动包
url: jdbc:mysql://localhost:3306/cloudDB02 # 数据库名称
username: root
password: 308539393
dbcp2:
min-idle: 5 # 数据库连接池的最小维持连接数
initial-size: 5 # 初始化连接数
max-total: 5 # 最大连接数
max-wait-millis: 200 # 等待连接获取的最大超时时间
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8770/eureka/
instance:
instance-id: cloud-config-dept-client
prefer-ip-address: true
info:
app.name: cloud-config-dept-client
company.name: www.zhaowb.com
build.artifactId: $project.artifactId$ # 这样写在idea 中运行出来不显示版本号,直接显示 $project.artifactId$ 字符串,可以自己修改
build.version: $project.version$
[{"deptno":1,"dname":"部门1","db_source":"clouddb02"},{"deptno":2,"dname":"部门2","db_source":"clouddb02"},{"deptno":3,"dname":"部门3","db_source":"clouddb02"},{"deptno":4,"dname":"部门4","db_source":"clouddb02"},{"deptno":5,"dname":"部门5","db_source":"clouddb02"},{"deptno":6,"dname":"部门6","db_source":"clouddb02"}]
修改数据库, 改为url: jdbc:mysql://localhost:3306/cloudDB01 # 数据库名称
重新启动,只要显示的"db_source":"clouddb01",即为成功。