<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
server: port: 9100 spring: application: name: study-config-server cloud: config: server: git: uri: https://github.com/mmcLine/cloudconfigTest.git username: mmcLine password: ********
@SpringBootApplication @EnableConfigServer public class StudyConfigApplication { public static void main(String[] args) { SpringApplication.run(StudyConfigApplication.class); } }
spring: profiles: active: test --- spring: profiles: test application: name: config-test server: port:9101 --- spring: profiles: prod application: name: config-prod server: port:9102
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
spring: application: name: study-trade cloud: config: uri: http://localhost:9100 profile: test label: master
注意:git上的配置yml文件名要对应这里配置的spring.application.name
@RestController public class ConfigTestController { @Value("${ordernotiry}") private Boolean ordernotiry; @RequestMapping("/testconfig") public String testconfig(){ return String.valueOf(ordernotiry); } }
到此最简单的配置中心搭建完成!!!
整合bus的方案
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
spring: cloud: bus: enabled: true trace: enabled: true rabbitmq: host: localhost port: 5672 username: guest password: guest management: endpoints: web: exposure: include: "bus-refresh"
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
在读取配置的类上加@RefreshScope注解
@RestController @RefreshScope public class ConfigTestController { @Value("${ordernotiry}") private Boolean ordernotiry; @RequestMapping("/testconfig") public String testconfig(){ return String.valueOf(ordernotiry); } }
https://www.oracle.com/java/technologies/javase-jce8-downloads.html
encrypt: key: trademmc
http://localhost:9100/encrypt/status
返回OK代表配置成功
5. 测试加密方法
http://localhost:9100/decrypt
密文内容前需要写{cipher},并且用单引号括起来
loginpass: '{cipher}941c15446567e8211931cf0d25f81696871f5a5ea74fac46638eed71db24e76b'
避免直接访问配置文件
<!-- 安全配置 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
spring: security: user: name: root password: 123456 basic: enable: true
spring: cloud: config: uri: http://localhost:9100 profile: test label: master username: root password: 123456
git地址: https://github.com/mmcLine/spring-cloud-study