1.配置eureka注册中心

EureKaSpringApplication:

  1. package com.crow.eureka;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  8. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  9. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  10.  
  11. @SpringBootApplication
  12. @EnableEurekaServer
  13. public class EureKaSpringApplication {
  14. public static void main(String[] args) {
  15. SpringApplication.run(EureKaSpringApplication.class, args);
  16. }
  17.  
  18. /**
  19. * 然后会发现其他路径访问不了的,比如向eureka注册服务,注册不进来。
  20. * 那是因为springboot在这个版本默认开启了CSRF攻击防御,2.x其他版本很多也会存在此问题。
  21. * 网上的解决办法是禁用CSRF防御,禁用之后部分版本会出现在登录eureka界面的时候又没有安全登录验证了,要注册服务禁用/eureka即可,而不是直接禁用CSRF
  22. *
  23. * @author Administrator
  24. *
  25. */
  26. @Configuration
  27. @EnableWebSecurity
  28. static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  29. @Override
  30. protected void configure(HttpSecurity http) throws Exception {
  31. // // Spring Security 默认开启了所有 CSRF 攻击防御,需要禁用 /eureka 的防御
  32. http.csrf().ignoringAntMatchers("/eureka/**");
  33. // 访问eureka控制台和/actuator时能做安全控制
  34. super.configure(http);
  35. // http.csrf().disable();//禁用CSRF
  36. // http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
  37.     }
  38. }
  39.  
  40. }

  application.properties配置:

  1. #在默认设置下,Eureka服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为。
  2. #服务注册中心端口号
  3. server.port=7070
  4. #服务注册中心实例的主机名
  5. eureka.instance.hostname=localhost
  6. #是否向服务注册中心注册自己
  7. eureka.client.register-with-eureka=false
  8. #是否检索服务
  9. eureka.client.fetch-registry=false
  10. #服务注册中心的配置内容,指定服务注册中心的位置
  11. eureka.client.serviceUrl.defaultZone=http://localhost:7070/eureka/
  12.  
  13. # 安全认证的配置
  14.  
  15. #热部署生效
  16. spring.devtools.restart.enabled=false
  17.  
  18. #用户名
  19. spring.security.user.name=admin
  20. # 用户密码
  21. spring.security.user.password=admin123456
  22.  
  23. spring.application.name= eureka
  24. spring.devtools.restart.enabled=false
  25. #本机测试下关闭自我保护机制
  26. eureka.server.enableSelfPreservation=false

  pom.xml配置:

  1. <!-- eureka 信息面板安全验证 -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-security</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.cloud</groupId>
  8. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  9. </dependency>
  10. <!-- actuator监控信息完善 -->
  11. <dependency>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-actuator</artifactId>
  14. </dependency>
  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>

  2.配置config-service

ConfigSpringBootApplication:

  1. package com.crow.config;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.config.server.EnableConfigServer;
  6. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  7.  
  8. @SpringBootApplication
  9. @EnableEurekaClient
  10. @EnableConfigServer
  11. public class ConfigSpringBootApplication {
  12. public static void main(String[] args) {
  13. SpringApplication.run(ConfigSpringBootApplication.class, args);
  14. }
  15. }

  application.properties:

  1. server.port=7072
  2.  
  3. # 优先注册IP地址而不是hostname
  4. eureka.instance.prefer-ip-address=true
  5. eureka.client.serviceUrl.defaultZone=http://admin:admin123456@localhost:7070/eureka/
  6.  
  7. #配置GitHub 私有仓库 HTTP 克隆地址
  8. spring.cloud.config.server.git.uri={你的仓库地址}
  9. #配置你的 github帐号
  10. spring.cloud.config.server.git.username={用户名或邮箱}
  11. #配置你的github帐号密码
  12. spring.cloud.config.server.git.password={密码}
  13. #克隆配置文件存储地址(注意会项目清空这个目录,设置目录需谨慎!!!!)
  14. spring.cloud.config.server.git.basedir=C:/Users/Administrator/Desktop/config
  15. #git仓库配置文件分支(默认即为master)
  16. spring.cloud.config.label=master
  17. #Git仓库路径下搜索路径(多个路径用逗号分隔),即你的仓库目录
  18. spring.cloud.config.server.git.search-paths=safe-service,safe-gateway
  19.  
  20. # 配置中心通过git从远程git库,有时本地的拷贝被污染,这时配置中心无法从远程库更新本地配置,设置force-pull=true,则强制从远程库中更新本地库
  21. spring.cloud.config.server.git.force-pull=true
  22.  
  23. spring.cloud.bus.trace.enabled= true
  24.  
  25. #注意得自己安装rabbitmq
  26. spring.rabbitmq.host=localhost
  27. spring.rabbitmq.port=5672
  28. spring.rabbitmq.username=guest
  29. spring.rabbitmq.password=crow9527
  30.  
  31. #支持动态刷新
  32. #打开bus/refresh刷新开关
  33. #actuator配置
  34. management.endpoints.enabled-by-default=true
  35. management.endpoint.health.show-details=always
  36. #management.endpoints.web.exposure.include=refresh,info,health
  37. management.endpoints.web.exposure.include=*
  38. management.endpoints.web.base-path=/actuator
  39.  
  40. #本机服务名
  41. spring.application.name=safe-config-service
  42.  
  43. #服务信息
  44. info.app.name= safe-config-service
  45. info.app.message=config
  46. info.company.name= abzykj
  47. info.build.artifactId= @project.artifactId@
  48. info.build.version= @project.version@

  pom.xml:

  1.           <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-config-server</artifactId>
  4. </dependency>
  5.  
  6. <dependency>
  7. <groupId>org.springframework.cloud</groupId>
  8. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  9. </dependency>
  10.  
  11. <dependency>
  12. <groupId>org.springframework.cloud</groupId>
  13. <artifactId>spring-cloud-starter-bus-amqp</artifactId>
  14. </dependency>
  15.  
  16. <!--配置中心监控 -->
  17. <dependency>
  18. <groupId>org.springframework.cloud</groupId>
  19. <artifactId>spring-cloud-config-monitor</artifactId>
  20. </dependency>
  1.         <dependency>
  2.         <groupId>org.springframework.boot</groupId>
  3.         <artifactId>spring-boot-starter-web</artifactId>
  4.         </dependency>

我是在码云上新建的属性文件  

服务端访问测试:

http://192.168.50.100:7072/safe-test.properties

或 http://192.168.50.100:7072/safe-test.json

或 http://192.168.50.100:7072/safe-test.yml

或 http://192.168.50.100:7072/safe/test  (这个是根据你属性文件的命名来的  例如:${application}-${profiles}.properties  safe-dev.properties   safe-test.properties)

 3.配置config-client

  1. ConfigClientSpringBootApplication:
  1. package com.crow.client;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  6.  
  7. @SpringBootApplication
  8. @EnableEurekaClient
  9. public class ConfigClientSpringBootApplication {
  10. public static void main(String[] args) {
  11. SpringApplication.run(ConfigClientSpringBootApplication.class, args);
  12. }
  13. }

  application.properties:

  1. #springboot 在启动时候. 会先去加载bootstrap.properties 中的配置, 从gtihub上加载配置下来. 再启动
  2. server.port= 7073
  3. #服务名
  4. spring.application.name=safe-config-client
  5. #方式一:通过服务名访问服务器
  6. #Config服务端服务名
  7. #spring.cloud.config.discovery.service-id=safe-config-service
  8. #支持注册中心访问Config服务端
  9. #spring.cloud.config.discovery.enabled=true
  10.  
  11. #方式二:配置中文件的地址 通过uri访问配置服务
  12. spring.cloud.config.uri=http://localhost:7072/
  13. #git仓库配置文件分支(默认即为master)
  14. spring.cloud.config.label=master
  15. spring.cloud.config.name=safe-test
  16. #git仓库配置文件环境信息
  17. spring.cloud.config.profile=test
  18.  
  19. #得自己安装rabbitmq
  20. spring.rabbitmq.host=localhost
  21. spring.rabbitmq.port=5672
  22. spring.rabbitmq.username=guest
  23. spring.rabbitmq.password=crow9527
  24.  
  25. spring.cloud.bus.trace.enabled= true

  pom.xml:

  1. <!--Spring Cloud Config 客户端依赖 -->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-config</artifactId>
  5. </dependency>
  6.  
  7. <dependency>
  8. <groupId>org.springframework.cloud</groupId>
  9. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  10. </dependency>
  11.  
  12. <dependency>
  13. <groupId>org.springframework.cloud</groupId>
  14. <artifactId>spring-cloud-starter-bus-amqp</artifactId>
  15. </dependency>
  16.  
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-actuator</artifactId>
  20. </dependency>

  

ConfigController:    测试查看属性文件

  1. package com.crow.client.controller;
  2.  
  3. import java.nio.charset.StandardCharsets;
  4.  
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.cloud.context.config.annotation.RefreshScope;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.ResponseBody;
  10.  
  11. /**
  12. * <p></p>
  13. * @author:Crow
  14. * @Date: 2019年10月14日 下午4:13:34
  15. */
  16. @Controller
  17. //开启更新功能
  18. @RefreshScope
  19. @RequestMapping("/config")
  20. public class ConfigController {
  21.  
  22. @Value("${user.name}")
  23. private String value;
  24.  
  25. @RequestMapping("/get")
  26. @ResponseBody
  27. public String getValue() {
           //因为如果配置文件中,要是有中文的话这里转换一下
  28.   //return new String(value.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);

            return new String(value);
  1.      }
    }

  客户端访问测试:

测试在线刷新 :

1.更改码云上属性文件类容(记得要提交)

2.通过actuator手动刷新(POST请求)http://localhost:7073/actuator/bus-refresh(可以在码云上的WebHooks中配置自动刷新)

3.重新访问客户端 http://192.168.50.100:7072/safe-test.properties

OK

记一次springboot(2.1.6)+springcloud(Greenwich.SR2) 配置中心搭建,支持在线刷新的更多相关文章

  1. springcloud(七):配置中心svn示例和refresh

    上一篇springcloud(六):配置中心git示例留了一个小问题,当重新修改配置文件提交后,客户端获取的仍然是修改前的信息,这个问题我们先放下,待会再讲.国内很多公司都使用的svn来做代码的版本控 ...

  2. springcloud之config配置中心-Finchley.SR2版

    本篇和大家分享的是springcloud-config配置中心搭建,写到这里突然想起自己曾今开源过基于Redis发布订阅编写的一个配置中心,刚看了git星数有点少哈哈,这里顺势发个连接欢迎大侠们点赞: ...

  3. 19.SpringCloud实战项目-SpringCloud整合Alibaba-Nacos配置中心

    SpringCloud实战项目全套学习教程连载中 PassJava 学习教程 简介 PassJava-Learning项目是PassJava(佳必过)项目的学习教程.对架构.业务.技术要点进行讲解. ...

  4. springcloud(六):配置中心(一)

    随着线上项目变的日益庞大,每个项目都散落着各种配置文件,如果采用分布式的开发模式,需要的配置文件随着服务增加而不断增多.某一个基础服务信息变更,都会引起一系列的更新和重启,运维苦不堪言也容易出错.配置 ...

  5. springcloud(八):配置中心服务化和高可用

    在前两篇的介绍中,客户端都是直接调用配置中心的server端来获取配置文件信息.这样就存在了一个问题,客户端和服务端的耦合性太高,如果server端要做集群,客户端只能通过原始的方式来路由,serve ...

  6. springcloud(九):配置中心和消息总线(配置中心终结版)

    我们在springcloud(七):配置中心svn示例和refresh中讲到,如果需要客户端获取到最新的配置信息需要执行refresh,我们可以利用webhook的机制每次提交代码发送请求来刷新客户端 ...

  7. springcloud(六):配置中心git示例

    随着线上项目变的日益庞大,每个项目都散落着各种配置文件,如果采用分布式的开发模式,需要的配置文件随着服务增加而不断增多.某一个基础服务信息变更,都会引起一系列的更新和重启,运维苦不堪言也容易出错.配置 ...

  8. [转]springcloud(九):配置中心和消息总线(配置中心终结版)

    https://www.cnblogs.com/ityouknow/p/6931958.html springcloud(九):配置中心和消息总线(配置中心终结版) 我们在springcloud(七) ...

  9. springcloud费话之配置中心server修改

    目录: springcloud费话之Eureka基础 springcloud费话之Eureka集群 springcloud费话之Eureka服务访问(restTemplate) springcloud ...

随机推荐

  1. Mybatis进阶使用-一级缓存与二级缓存

    简介 缓存是一般的ORM 框架都会提供的功能,目的就是提升查询的效率和减少数据库的压力.跟Hibernate 一样,MyBatis 也有一级缓存和二级缓存,并且预留了集成第三方缓存的接口. 一级缓存 ...

  2. springboot整合websocket后打包报错:javax.websocket.server.ServerContainer not available

    项目整合了websocket以后,打包多次都没有成功,原来是报错了,报错内容如下: Error starting ApplicationContext. To display the conditio ...

  3. latex三种标准文类book, report, article的章节命令与层次深度

    Latex有三种标准文类:book, report, article. 每种文类的章节命令和层次深度如下: 三种标准文类的章节命令与层次深度 层次深度 层次名 book report article ...

  4. Redis主从复制(读写分离)

    主从复制(读写分离):读在从库读,写在主库写. 主从复制的好处:避免redis单点故障构建读写分离架构,满足读多写少的需求. 主从架构: 操作(启动实例,在一台机器上启动不同的实例,进行伪主从复制): ...

  5. 分享一个操作pdf文件的js文件-pdfObject.js(文件预览、下载、打印等操作都具备)

    获取相关资料或者源码的朋友可以关注下公众号,回复关键字pdf20200518即可

  6. idea,git操作

    原文地址:https://blog.csdn.net/lzx2018/article/details/91414591 1新建分支 点击New Branch 2切换分支

  7. WPF实现飞控姿态仪表盘控件Attitude dashboard

    一.概要 近期项目当中需要用到飞机控制仪表盘的姿态仪,一开始去各大网站搜索解决方案要么就是winfrom要么就是很老的代码根本不能运行更甚者是居然有的还要下载积分. 只能自己手动从0开始写一个控件.这 ...

  8. 程序员小哥教你秋招拿大厂offer

    快要到秋招了,对于应届生来说,秋招是一个特别重要的机会.对于社招同学来说,金九银十也是一个很好的跳槽窗口. 而我呢,因为是从上海到广州工作,就没有提前先把工作定下来.刚好也趁这个机会出去旅游了两个月. ...

  9. 深入探究.Net Core Configuration读取配置的优先级

    前言     在之前的文章.Net Core Configuration源码探究一文中我们曾解读过Configuration的工作原理,也.Net Core Configuration Etcd数据源 ...

  10. webdriver入门之环境准备

    1.安装ruby 下载ruby的安装包,很简单,不解释.装好之后打开cmd输入以下命令验证是否安装成功 ruby -v 2.安装webdriver 确保机器联网,用gem命令安装是在有网络的情况下进行 ...