赞
踩
config Server服务端:
新建引入pom
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-config-server</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-actuator</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
- </dependency>
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- </dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid-spring-boot-starter</artifactId>
- <version>1.1.10</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-jdbc</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-devtools</artifactId>
- <scope>runtime</scope>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>com.smy.springcloud</groupId>
- <artifactId>common-api</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
- </dependencies>

yml配置:
- server:
- port: 3344
- spring:
- application:
- name: CLOUD-CONFIG
- cloud:
- config:
- server:
- git:
- uri: https://gitee.com/rentings/config-server.git
- search-paths:
- - config
- label: master
- datasource:
- type: com.alibaba.druid.pool.DruidDataSource
- driver-class-name: org.gjt.mm.mysql.Driver
- url: jdbc:mysql://127.0.0.1:3306/cloud2020?useUnicode=true&characterEncoding=utf-8&useSSL=false
- username: root
- password: 123456
-
- eureka:
- client:
- register-with-eureka: true
- fetch-registry: true
- service-url:
- defaultZone: http://peer1:7001/eureka,http://peer2:7002/eureka
- instance:
- instance-id: payment8001
- prefer-ip-address: true
- lease-renewal-interval-in-seconds: 1
- lease-expiration-duration-in-seconds: 2
-
-
- mybatis:
- mapper-locations: classpath:mapper/*.xml
- type-aliases-package: com.smy.entity
- logging:
- level:
- com.smy.dao: debug

主类:
- @SpringBootApplication
- @EnableConfigServer
- public class ConfigMain3344 {
- public static void main(String[] args) {
- SpringApplication.run(ConfigMain3344.class,args);
- }
- }
启动访问配置文件:http://127.0.0.1:3344/master/config-dev.yml
config Client客户端
pom配置:
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-config</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-actuator</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-devtools</artifactId>
- <scope>runtime</scope>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>com.smy.springcloud</groupId>
- <artifactId>common-api</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
- </dependencies>

bootstrap.yml配置:
- server:
- port: 3355
- spring:
- application:
- name: CLOUD-CONFIG-CLIENT
- cloud:
- config:
- label: master
- name: config
- profile: dev
- uri: http://127.0.0.1:3344
- eureka:
- client:
- register-with-eureka: true
- fetch-registry: true
- service-url:
- defaultZone: http://peer1:7001/eureka,http://peer2:7002/eureka
- instance:
- instance-id: configclient3355
- prefer-ip-address: true
- lease-renewal-interval-in-seconds: 1
- lease-expiration-duration-in-seconds: 2

主类:
- @SpringBootApplication
- @EnableEurekaClient
- public class ConfigClientMain3355 {
- public static void main(String[] args) {
- SpringApplication.run(ConfigClientMain3355.class,args);
- }
- }
controller:此处@Value("${info1.zs}")对应配置中加载的服务端的配置文件内容必须一致否则在启动时候会报错。
- @RequestMapping("configclient")
- @RestController
- public class ConfigController {
- @Value("${info1.zs}")
- private String str;
- @GetMapping("/getConfigInfo")
- public String getConfigInfo(){
- return str;
- }
- }
访问:http://127.0.0.1:3355/configclient/getConfigInfo 得到8001
注意:bootstrap会优先application文件加载一般对外的配置写在bootstrap中对内的写在application中,如果把服务端的config改成applicatio的内容则会自动读取并加载到项目中。
手动刷新:
bootstrap.yml配置:
include配置成*,要加双引号哦,然后启动先获取一下配置http://localhost:3355/configclient/getConfigInfo嗯没错,然后修改gitee里面的配置,然后刷新3355地址http://localhost:3355/actuator/refresh用postman发post请求,我开始用cmd发-X POST那种的死活无法成功一致提示404,但是访问3355的controller是可以的郁闷,刷新完之后再请求http://localhost:3355/configclient/getConfigInfo发现配置已经修改了。
- server:
- port: 3355
- spring:
- application:
- name: CLOUD-CONFIG-CLIENT
- cloud:
- config:
- label: master
- name: config
- profile: dev
- uri: http://127.0.0.1:3344
- eureka:
- client:
- register-with-eureka: true
- fetch-registry: true
- service-url:
- defaultZone: http://peer1:7001/eureka,http://peer2:7002/eureka
- instance:
- instance-id: configclient3355
- prefer-ip-address: true
- lease-renewal-interval-in-seconds: 1
- lease-expiration-duration-in-seconds: 2
- #暴露监控端口
- management:
- endpoints:
- web:
- exposure:
- include: "*"

@RefreshScope开启刷新。
- @RequestMapping("configclient")
- @RestController
- @RefreshScope
- public class ConfigController {
- @Value("${config.info}")
- private String str;
- @GetMapping("/getConfigInfo")
- public String getConfigInfo(){
- return str;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。