内容简介:启动provider时,总是报错如下:忘记了添加config依赖! 方法一:修改父工程,用我自己的,不用springboot的方法二:添加config依赖
启动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依赖
2 几种不同的玩法
针对:我在 github 上 修改了配置文件的内容 , 但是不想重启机器 ,想实时读取到而做的几种不同的策略
2.1 低级
重启client端,再刷新就可以了
2.2 高级 手动刷新
缺点: 如果有成百上千个(夸张的讲),那么需要挨个服务刷新,会容易出错,也不简便
搭建过程如下:
1. 添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> 复制代码
2. 非常重要!非常容易忘记!添加配置文件中的监控端点
management: endpoints: web: exposure: include: "*" 开启所有的端点 复制代码
3. 加入@RefreshScope注解
4. 修改github上的配置文件,获取修改的属性
http://localhost:8082/order/testManualRefresh 结果:会发现依然不生效,如下图所示,所以继续第5步
5. 必须要手动刷新
http://localhost:8082/actuator/refresh 注意一定要用 POST请求 !
6. 再次测试
http://localhost:8082/order/testManualRefresh 会发现 要获取的属性是修改后的值了
2.3 顶级——使用BUS
2.3.1 先搭建Rabbitmq
这个比较简单,大家可以自行百度
2.3.2 服务端server-添加BUS依赖
<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> 复制代码
2.3.3 服务端server-添加配置
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 访问的端口,也就是管理端从浏览器打开的端口
2.3.4 服务端server-手动刷新
http://localhost:8888/actuator/bus-refresh
2.3.5 客户端config-client-添加依赖
注意:和服务端添加一样的依赖! 千万不要忘记! 客户端一般有多个,但是每个工程也都要添加这些依赖的,所以我想还是后面可以放到一个父工程中
2.3.6 客户端config-client-添加配置
没有什么特别的,和服务端一样的配置添加 rabbitmq 节点的配置
2.3.7 客户端config-client-测试
-
客户端临时又添加了一个 provider,分别是eureka-provider1 和 eureka-provider2
-
访问地址: http://localhost:8082/order/testManualRefresh http://localhost:8080/order/testManualRefresh
测试过程:
- 先确定当前属性值,比如说是 Y
- 分别用客户端打开,屏幕打印也都是 Y
- 修改配置文件中心的属性值,改成 N
- 刷新客户端地址,依旧是 Y
- 手动刷新 http://localhost:8888/actuator/bus-refresh 服务端地址
- 再次刷新客户端地址, 屏幕打印值会更新为 N 了
2.4 顶级-自己搭建
2.4.1 服务端server-添加依赖
<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> 复制代码
2.4.2 服务端server-添加配置
spring: rabbitmq: host: 127.0.0.1 port: 5672 virtual-host: linRabbit username: guest password: guest connection-timeout: 10000 template: mandatory: true 复制代码
2.4.3 服务端server-添加注解
@EnableConfigServer
@SpringBootApplication @EnableConfigServer // 非常重要 public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } } 复制代码
2.4.4 服务端server-提供刷新接口
提供一个全局刷新接口,往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"; } } 复制代码
2.4.5 客户端client-添加依赖
和服务端类似
<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> 复制代码
2.4.6 客户端client-添加配置
spring: rabbitmq: host: 127.0.0.1 port: 5672 virtual-host: linRabbit username: guest password: guest connection-timeout: 10000 template: mandatory: true 复制代码
2.4.7 客户端client-提供消息消费监听
2.5 解决配置文件放在服务器,但是有数据库明文的问题
2.5.1 加密过程
- jce-policy-8.zip (一定要和jdk 版本一致) 解压,放到指定目录java_home/jre/lib/security/下
- config-server 添加配置
encrypt: key: alin 复制代码
这个配置必须加在配置 叫做bootstrap。yml的文件夹下,没有就新建一个
访问地址 localhost:8888/encrypt/status ,会出现下图:
- 加密密码 与 解密密码 jdk 配置的 JCE加密。对称加密
POST请求 加密: http://localhost:8888/encrypt
访问 http://localhost:8888/config-client-bus-apply-prd.yml, 会发现还是用明文传输的
2.5.2 先加密
2.5.3 再配置到git上
busi: ops: Y pwd: '{cipher}c92b52bdbfe07003d13c0e0132c7240f22885b4dbc6ee3c59ac432d125018f23' 复制代码
2.5.4 最后查看
http://localhost:8888/config-client-bus-apply-prd.yml
2.5.5 加入security控制:
直接访问http://localhost:8888/config-client-bus-apply-prd.yml 会显示明文,我们可以加上 security
2.5.5.1 服务端
第一,加入security依赖
<!--加入安全配置依赖的Jar包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> 复制代码
第二,yml添加配置
spring: security: user: name: root password: 123456 复制代码
以上所述就是小编给大家介绍的《spring-cloud-config 搭建-入门级(二)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- kafka入门+集群搭建
- vue入门(一):项目搭建
- django入门一(搭建开发环境)
- MongoDB分片群集搭建入门详解
- webpack入门(2) - 安装,配置,环境搭建
- Seata 搭建与分布式事务入门
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Go语言学习笔记
雨痕 / 电子工业出版社 / 2016-6 / 89
作为时下流行的一种系统编程语言,Go 简单易学,性能很好,且支持各类主流平台。已有大量项目采用 Go 编写,这其中就包括 Docker 等明星作品,其开发和执行效率早已被证明。本书经四年多逐步完善,内容覆盖了语言、运行时、性能优化、工具链等各层面知识。且内容经大量读者反馈和校对,没有明显的缺陷和错误。上卷细致解析了语言规范相关细节,便于读者深入理解语言相关功能的使用方法和注意事项。下卷则对运行时源......一起来看看 《Go语言学习笔记》 这本书的介绍吧!