启动provider时,总是报错如下:
2019-05-08 15:21:36.723 ERROR 14424 --- [ main] o.s.boot.SpringApplication : Application run failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.orderController': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'myconfigtest' in value "${myconfigtest}" at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:380) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE] 复制代码
忘记了添加config依赖! 方法一:修改父工程,用我自己的,不用springboot的
方法二:添加config依赖
针对:我在 github 上 修改了配置文件的内容 , 但是不想重启机器 ,想实时读取到而做的几种不同的策略
重启client端,再刷新就可以了
缺点: 如果有成百上千个(夸张的讲),那么需要挨个服务刷新,会容易出错,也不简便
搭建过程如下:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> 复制代码
management: endpoints: web: exposure: include: "*" 开启所有的端点 复制代码
http://localhost:8082/order/testManualRefresh 结果:会发现依然不生效,如下图所示,所以继续第5步
http://localhost:8082/actuator/refresh 注意一定要用 POST请求 !
http://localhost:8082/order/testManualRefresh 会发现 要获取的属性是修改后的值了
这个比较简单,大家可以自行百度
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> 复制代码
spring: rabbitmq: host: 127.0.0.1 port: 5672 virtual-host: linRabbit username: guest password: guest connection-timeout: 10000 template: mandatory: true 复制代码
这里需要特别注意一点,rabbitmq的端口问题: 5672 是 用TLS访问的端口,也就是我们配置文件中需要配置的端口 15672 是 HTTP 访问的端口,也就是管理端从浏览器打开的端口
http://localhost:8888/actuator/bus-refresh
注意:和服务端添加一样的依赖! 千万不要忘记! 客户端一般有多个,但是每个工程也都要添加这些依赖的,所以我想还是后面可以放到一个父工程中
没有什么特别的,和服务端一样的配置添加 rabbitmq 节点的配置
客户端临时又添加了一个 provider,分别是eureka-provider1 和 eureka-provider2
访问地址: http://localhost:8082/order/testManualRefresh http://localhost:8080/order/testManualRefresh
测试过程:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <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>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.56</version> </dependency> 复制代码
spring: rabbitmq: host: 127.0.0.1 port: 5672 virtual-host: linRabbit username: guest password: guest connection-timeout: 10000 template: mandatory: true 复制代码
@EnableConfigServer
@SpringBootApplication @EnableConfigServer // 非常重要 public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } } 复制代码
提供一个全局刷新接口,往mq broker上发送刷新消息 http://localhost:8888/autoRefresh
@RestController public class AutoRefreshController { @Autowired private RabbitTemplate rabbitTemplate; @RequestMapping("/autoRefresh") public Object autoRefresh() { Map<String,Object> sendMap = new HashMap<String,Object>(); sendMap.put("isNew","Y"); rabbitTemplate.convertAndSend("auto.refresh.topic","auto.refresh.all", JSON.toJSONString(sendMap)); return "OK"; } } 复制代码
和服务端类似
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> 复制代码
spring: rabbitmq: host: 127.0.0.1 port: 5672 virtual-host: linRabbit username: guest password: guest connection-timeout: 10000 template: mandatory: true 复制代码
www.oracle.com/technetwork…
encrypt: key: alin 复制代码
这个配置必须加在配置 叫做bootstrap。yml的文件夹下,没有就新建一个
访问地址 localhost:8888/encrypt/status ,会出现下图:
POST请求 加密: http://localhost:8888/encrypt
解密: http://localhost:8888/decrypt访问 http://localhost:8888/config-client-bus-apply-prd.yml, 会发现还是用明文传输的
http://localhost:8888/encrypt
busi: ops: Y pwd: '{cipher}c92b52bdbfe07003d13c0e0132c7240f22885b4dbc6ee3c59ac432d125018f23' 复制代码
http://localhost:8888/config-client-bus-apply-prd.yml
直接访问http://localhost:8888/config-client-bus-apply-prd.yml 会显示明文,我们可以加上 security
第一,加入security依赖
<!--加入安全配置依赖的Jar包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> 复制代码
第二,yml添加配置
spring: security: user: name: root password: 123456 复制代码