部门微服务和注册中心微服务接入SpringCloud Config配置实战
1. Git配置文件本地配置 创建配置文件并上传至Github私有仓库 microservicecloud-config-eureka-client.yml
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 spring: profiles: active: - dev --- server: port: 7001 spring: profiles: dev application: name: microservicecloud-config-eureka-client eureka: instance: hostname: eureka7001.com client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://eureka7001.com:7001/eureka/ --- server: port: 7001 spring: profiles: test application: name: microservicecloud-config-eureka-client eureka: instance: hostname: eureka7001.com client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://eureka7001.com:7001/eureka/
microservicecloud-config-dept-client.yml
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 spring: profiles: active: - dev --- server: port: 8001 spring: profiles: dev application: name: microservicecloud-config-dept-client datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: org.gjt.mm.mysql.Driver url: jdbc:mysql://localhost:3306/cloudDB01 username: root password: 123456 dbcp2: min-idle: 5 initial-size: 5 max-total: 5 max-wait-millis: 200 mybatis: config-location: classpath:mybatis/mybatis.cfg.xml type-aliases-package: com.hellodev.entities mapper-locations: - classpath:mybatis/mapper/**/*.xml eureka: client: service-url: defaultZone: http://eureka7001.com:7001/eureka instance: instance-id: dept-8001.com prefer-ip-address: true info: app.name: atguigu-microservicecloud-springcloudconfig01 company.name: www.atguigu.com build.artifactId: ${project.artifactId} build.version: ${project.version} --- server: port: 8001 spring: profiles: test application: name: microservicecloud-config-dept-client datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: org.gjt.mm.mysql.Driver url: jdbc:mysql://localhost:3306/cloudDB02 username: root password: 123456 dbcp2: min-idle: 5 initial-size: 5 max-total: 5 max-wait-millis: 200 mybatis: config-location: classpath:mybatis/mybatis.cfg.xml type-aliases-package: com.hellodev.entities mapper-locations: - classpath:mybatis/mapper/**/*.xml
eureka: client: service-url: defaultZone: http://eureka7001.com:7001/eureka instance: instance-id: dept-8001.com prefer-ip-address: true info: app.name: atguigu-microservicecloud-springcloudconfig02 company.name: www.atguigu.com build.artifactId: ${project.artifactId} build.version: ${project.version}
2. Config版的eureka服务端 2.1 新建工程 创建microservicecloud-config-eureka-client-7001工程,步骤省略.
2.2 POM 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 <?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 > <parent > <groupId > com.hellodev</groupId > <artifactId > atguigu-microservicecloud</artifactId > <version > 0.0.1</version > </parent > <artifactId > microservicecloud-config-eureka-client-7001</artifactId > <version > 0.0.1</version > <dependencies > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-config</artifactId > </dependency > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-eureka-server</artifactId > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > springloaded</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-devtools</artifactId > </dependency > </dependencies > </project >
2.3 bootstrap.yml 1 2 3 4 5 6 7 8 9 spring: cloud: config: name: microservicecloud-config-eureka-client profile: dev label: master uri: http://config-3344.com:3344
2.4 application.yml 1 2 3 spring: application: name: microservicecloud-config-eureka-client
2.5 主启动类 1 2 3 4 5 6 7 8 @SpringBootApplication @EnableEurekaServer public class ConfigEureka7001Application { public static void main (String[] args) { SpringApplication.run(ConfigEureka7001Application.class , args ) ; } }
2.6 验证
先启动microservicecloud-config-3344微服务,保证Config总配置是OK的
再启动microservicecloud-config-eureka-client-7001微服务
访问url http://eureka7001.com:7001/
出现eureak主页表示成功启动
3. Config版的dept微服务 3.1 创建工程 参考之前的8001拷贝后新建工程microservicecloud-config-dept-client-8001
3.2 POM 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 81 82 83 84 85 86 <?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 > com.hellodev</groupId > <artifactId > atguigu-microservicecloud</artifactId > <version > 0.0.1</version > </parent > <artifactId > microservicecloud-config-dept-client-8001</artifactId > <version > 0.0.1</version > <dependencies > <dependency > <groupId > com.hellodev</groupId > <artifactId > microservicecloud-api</artifactId > <version > ${project.version}</version > </dependency > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-config</artifactId > </dependency > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-eureka</artifactId > </dependency > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-config</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-actuator</artifactId > </dependency > <dependency > <groupId > junit</groupId > <artifactId > junit</artifactId > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > </dependency > <dependency > <groupId > com.alibaba</groupId > <artifactId > druid</artifactId > </dependency > <dependency > <groupId > ch.qos.logback</groupId > <artifactId > logback-core</artifactId > </dependency > <dependency > <groupId > org.mybatis.spring.boot</groupId > <artifactId > mybatis-spring-boot-starter</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-jetty</artifactId > </dependency > <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 > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > springloaded</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-devtools</artifactId > </dependency > </dependencies > </project >
3.3 bootstrap.yml 1 2 3 4 5 6 7 8 9 10 11 spring: cloud: config: name: microservicecloud-config-dept-client profile: dev label: master uri: http://config-3344.com:3344
3.4 application.yml 1 2 3 spring: application: name: microservicecloud-config-dept-client
3.5 主启动类 1 2 3 4 5 6 7 8 9 10 @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient public class ConfigClientProviderDept8001Application { public static void main (String[] args) { SpringApplication.run(ConfigClientProviderDept8001Application.class , args ) ; } }
3.6 验证 本地换配置成dev
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 curl http://localhost:8001/dept/list [ { "deptno": 1, "dname": "开发部", "db_source": "cloudDB01" }, { "deptno": 2, "dname": "人事部", "db_source": "cloudDB01" }, { "deptno": 3, "dname": "财务部", "db_source": "cloudDB01" }, { "deptno": 4, "dname": "市场部", "db_source": "cloudDB01" }, { "deptno": 5, "dname": "运维部", "db_source": "cloudDB01" }, { "deptno": 6, "dname": "AI", "db_source": "cloudDB01" } ]
可以看到数据库配置是01
test配置默认访问
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 curl http://localhost:8001/dept/list [ { "deptno": 1, "dname": "开发部", "db_source": "cloudDB02" }, { "deptno": 2, "dname": "人事部", "db_source": "cloudDB02" }, { "deptno": 3, "dname": "财务部", "db_source": "cloudDB02" }, { "deptno": 4, "dname": "市场部", "db_source": "cloudDB02" }, { "deptno": 5, "dname": "运维部", "db_source": "cloudDB02" } ]
可以看到数据库配置是02
资源获取 公众号回复 : SpringCloud Config配置实战 获取本节代码
公众号回复 : SpringCloud思维导图
Tips 欢迎收藏和转发,感谢你的支持!(๑•̀ㅂ•́)و✧
欢迎关注我:后端小哥,专注后端开发,希望和你一起进步!