配置中心

一、配置中心服务端

  1. 新建项目study-config-server
  2. 引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
  1. 加配置文件
server:
port: 9100
spring:
application:
name: study-config-server
cloud:
config:
server:
git:
uri: https://github.com/mmcLine/cloudconfigTest.git
username: mmcLine
password: ********
  1. 加注解 @EnableConfigServer
@SpringBootApplication
@EnableConfigServer
public class StudyConfigApplication { public static void main(String[] args) {
SpringApplication.run(StudyConfigApplication.class);
}
}
  1. 创建一个git项目,并在根目录下新建一个application.yml文件
spring:
profiles:
active: test
---
spring:
profiles: test
application:
name: config-test server:
port:9101
---
spring:
profiles: prod
application:
name: config-prod server:
port:9102
  1. 访问规则

二、配置中心客户端

  1. 引入依赖
 <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
  1. 配置文件,新增bootstrap.yml文件
spring:
application:
name: study-trade
cloud:
config:
uri: http://localhost:9100
profile: test
label: master

注意:git上的配置yml文件名要对应这里配置的spring.application.name

  1. git配置文件演示

  1. 测试配置是否生效
@RestController
public class ConfigTestController { @Value("${ordernotiry}")
private Boolean ordernotiry; @RequestMapping("/testconfig")
public String testconfig(){
return String.valueOf(ordernotiry);
}
}

到此最简单的配置中心搭建完成!!!

三、升级不重启修改配置文件

整合bus的方案

服务端修改:

  1. 加依赖
<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>
  1. 启动一个rabbitmq
  2. 配置文件
spring:
cloud:
bus:
enabled: true
trace:
enabled: true
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest management:
endpoints:
web:
exposure:
include: "bus-refresh"

客户端:

  1. 加依赖
<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>
  1. 加注解

在读取配置的类上加@RefreshScope注解

@RestController
@RefreshScope
public class ConfigTestController { @Value("${ordernotiry}")
private Boolean ordernotiry; @RequestMapping("/testconfig")
public String testconfig(){
return String.valueOf(ordernotiry);
}
}
  1. git上修改配置文件后,post方式调用http://localhost:9100/actuator/bus-refresh即可

四、配置加密

  1. 下载jce文件

https://www.oracle.com/java/technologies/javase-jce8-downloads.html

  1. 放入Java\jdk1.8.0_112\jre\lib\security目录下替换掉原有jar包
  2. 在config-server的bootstrap.yml(application.yml里不行)文件里加配置
encrypt:
key: trademmc
  1. 测试

http://localhost:9100/encrypt/status

返回OK代表配置成功

5. 测试加密方法

  1. 解密方法

http://localhost:9100/decrypt

  1. 在配置文件中就可以写密文了

密文内容前需要写{cipher},并且用单引号括起来

loginpass: '{cipher}941c15446567e8211931cf0d25f81696871f5a5ea74fac46638eed71db24e76b'
  1. 加上安全配置

避免直接访问配置文件

  • 在config-server里加依赖
 <!-- 安全配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
  • bootstrap.yml配置文件里开启
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

从零搭建一个SpringCloud项目之Config(五)的更多相关文章

  1. 从零搭建一个SpringCloud项目之Feign搭建

    从零搭建一个SpringCloud项目之Feign搭建 工程简述 目的:实现trade服务通过feign调用user服务的功能.因为trade服务会用到user里的一些类和接口,所以抽出了其他服务需要 ...

  2. 从零搭建一个SpringCloud项目之Zuul(四)

    整合Zuul 为什么要使用Zuul? 易于监控 易于认证 减少客户端与各个微服务之间的交互次数 引入依赖 <dependency> <groupId>org.springfra ...

  3. 从零搭建一个IdentityServer——项目搭建

    本篇文章是基于ASP.NET CORE 5.0以及IdentityServer4的IdentityServer搭建,为什么要从零搭建呢?IdentityServer4本身就有很多模板可以直接生成一个可 ...

  4. 使用Vue脚手架(vue-cli)从零搭建一个vue项目(包含vue项目结构展示)

    注:在搭建项目之前,请先安装一些全局的工具(如:node,vue-cli等) node安装:去node官网(https://nodejs.org/en/)下载并安装node即可,安装node以后就可以 ...

  5. 从零搭建一个IdentityServer——目录(更新中...)

    从零搭建一个IdentityServer--项目搭建 从零搭建一个IdentityServer--集成Asp.net core Identity 从零搭建一个IdentityServer--初识Ope ...

  6. vue-用Vue-cli从零开始搭建一个Vue项目

    Vue是近两年来比较火的一个前端框架(渐进式框架吧). Vue两大核心思想:组件化和数据驱动.组件化就是将一个整体合理拆分为一个一个小块(组件),组件可重复使用:数据驱动是前端的未来发展方向,释放了对 ...

  7. 基于 Express 搭建一个node项目 - 起步

    一,如何基于 Express 搭建一个node项目 什么是Express 借用官方的介绍,Express是一个基于Node.js平台的极简.灵活的web应用开发框架,它提供了一系列强大的特性,帮助你创 ...

  8. 从零开始搭建一个react项目

    Nav logo 120 发现 关注 消息 4 搜索 从零开始搭建一个react项目 96 瘦人假噜噜 2017.04.23 23:29* 字数 6330 阅读 32892评论 31喜欢 36 项目地 ...

  9. 完整搭建一个vue项目

    目录 一. 搭建一个vue项目的完整步骤 二. 卸载vue-cli 三. 完全卸载webpack 一. 搭建一个vue项目的完整步骤 1.安装node.js 下载地址 # 检查是否安装成功 node ...

随机推荐

  1. java 知识点总结(转)

    转载自 https://www.cnblogs.com/wl310538259/p/5218251.html (一)Java 1.接口和抽象类的区别 ①抽象类里可以有构造方法,而接口内不能有构造方法. ...

  2. 【转】Java包管理器Maven学习

    Maven 引入Jar包的几种情况 1.通过pom.xml中导入的jar包 (1)链接打开http://mvnrepository.com/ (2)搜索罐包,比如我想要的是servlet的api.ja ...

  3. 【面试QA】Attention

    目录 Attention机制的原理 Attention机制的类别 双向注意力 Self-Attention 与 Soft-Attention 的区别 Transformer Multi-Head At ...

  4. 洛谷 P3909 异或之积 题解

    原题链接 本人看了其它解法,发现本人的解法还是 首创 ! 而且我的解法好像和 \(\times 6\) 没什么关系 -- (如果没 \(\times 6\),我没还不用算逆元) 别人的思路呢,大都是从 ...

  5. 图-搜索-BFS-DFS-126. 单词接龙 II

    2020-03-19 13:10:35 问题描述: 给定两个单词(beginWord 和 endWord)和一个字典 wordList,找出所有从 beginWord 到 endWord 的最短转换序 ...

  6. Python 趣题

    如何优雅判断list为空 list_temp = [] if list_temp: # 存在值即为真 else: # list_temp是空的 在Python中,False,0,'',[],{},() ...

  7. Python第六章-函数01-函数的概念和使用

    函数 为了便于程序的维护和更好的实现模块化,好的程序都会分解为很多函数. 可以这么说,对于任何的编程语言,函数都是一个非常重要的概念. python 不仅简化了函数的定义过程,而且还大量借鉴了其他函数 ...

  8. 大规模机器学习(Large Scale Machine Learning)

    本博客是针对Andrew Ng在Coursera上的machine learning课程的学习笔记. 目录 在大数据集上进行学习(Learning with Large Data Sets) 随机梯度 ...

  9. Building Applications with Force.com and VisualForce (DEV401)(五):Application Essential: Introducing Business Logic

    Dev 401-005 Application Essential: Introducing Business Logic Module Agenda1.Custom object Queues2.W ...

  10. Thread -- Volatile

    1,可见性也就是说一旦某个线程修改了该被volatile修饰的变量,它会保证修改的值会立即被更新到主存,当有其他线程需要读取时,可以立即获取修改之后的值. 在Java中为了加快程序的运行效率,对一些变 ...