1.配置eureka注册中心

EureKaSpringApplication:

package com.crow.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @SpringBootApplication
@EnableEurekaServer
public class EureKaSpringApplication {
public static void main(String[] args) {
SpringApplication.run(EureKaSpringApplication.class, args);
} /**
* 然后会发现其他路径访问不了的,比如向eureka注册服务,注册不进来。
* 那是因为springboot在这个版本默认开启了CSRF攻击防御,2.x其他版本很多也会存在此问题。
* 网上的解决办法是禁用CSRF防御,禁用之后部分版本会出现在登录eureka界面的时候又没有安全登录验证了,要注册服务禁用/eureka即可,而不是直接禁用CSRF
*
* @author Administrator
*
*/
@Configuration
@EnableWebSecurity
static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// // Spring Security 默认开启了所有 CSRF 攻击防御,需要禁用 /eureka 的防御
http.csrf().ignoringAntMatchers("/eureka/**");
// 访问eureka控制台和/actuator时能做安全控制
super.configure(http);
// http.csrf().disable();//禁用CSRF
// http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
    }
} }

  application.properties配置:

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

  pom.xml配置:

<!-- eureka 信息面板安全验证 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- actuator监控信息完善 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

  2.配置config-service

ConfigSpringBootApplication:

package com.crow.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigSpringBootApplication.class, args);
}
}

  application.properties:

server.port=7072

# 优先注册IP地址而不是hostname
eureka.instance.prefer-ip-address=true
eureka.client.serviceUrl.defaultZone=http://admin:admin123456@localhost:7070/eureka/ #配置GitHub 私有仓库 HTTP 克隆地址
spring.cloud.config.server.git.uri={你的仓库地址}
#配置你的 github帐号
spring.cloud.config.server.git.username={用户名或邮箱}
#配置你的github帐号密码
spring.cloud.config.server.git.password={密码}
#克隆配置文件存储地址(注意会项目清空这个目录,设置目录需谨慎!!!!)
spring.cloud.config.server.git.basedir=C:/Users/Administrator/Desktop/config
#git仓库配置文件分支(默认即为master)
spring.cloud.config.label=master
#Git仓库路径下搜索路径(多个路径用逗号分隔),即你的仓库目录
spring.cloud.config.server.git.search-paths=safe-service,safe-gateway # 配置中心通过git从远程git库,有时本地的拷贝被污染,这时配置中心无法从远程库更新本地配置,设置force-pull=true,则强制从远程库中更新本地库
spring.cloud.config.server.git.force-pull=true spring.cloud.bus.trace.enabled= true #注意得自己安装rabbitmq
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=crow9527 #支持动态刷新
#打开bus/refresh刷新开关
#actuator配置
management.endpoints.enabled-by-default=true
management.endpoint.health.show-details=always
#management.endpoints.web.exposure.include=refresh,info,health
management.endpoints.web.exposure.include=*
management.endpoints.web.base-path=/actuator #本机服务名
spring.application.name=safe-config-service #服务信息
info.app.name= safe-config-service
info.app.message=config
info.company.name= abzykj
info.build.artifactId= @project.artifactId@
info.build.version= @project.version@

  pom.xml:

          <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency> <!--配置中心监控 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-monitor</artifactId>
</dependency>
        <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        </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

ConfigClientSpringBootApplication:
package com.crow.client;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication
@EnableEurekaClient
public class ConfigClientSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientSpringBootApplication.class, args);
}
}

  application.properties:

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

  pom.xml:

                <!--Spring Cloud Config 客户端依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> <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>

  

ConfigController:    测试查看属性文件

package com.crow.client.controller;

import java.nio.charset.StandardCharsets;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; /**
* <p></p>
* @author:Crow
* @Date: 2019年10月14日 下午4:13:34
*/
@Controller
//开启更新功能
@RefreshScope
@RequestMapping("/config")
public class ConfigController { @Value("${user.name}")
private String value; @RequestMapping("/get")
@ResponseBody
public String getValue() {
       //因为如果配置文件中,要是有中文的话这里转换一下
  //return new String(value.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);

        return new String(value);
     } 
}

  客户端访问测试:

测试在线刷新 :

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. kubeadm安装kubernetes(v18.8.8)

    1. 前言 kubernetes版本更新迭代非常快,上一篇写kubernetes搭建时,版本还是v1.15.0,现在已经更新到v1.18.看kubernetes在github的官方仓库,8月14日小版 ...

  2. 第四篇Scrum冲刺博客--Interesting-Corps

    第四篇Scrum冲刺博客 站立式会议 1.会议照片 2.队友完成情况 团队成员 昨日完成 今日计划 鲍鱼铭 搜索页面跳转.设计及布局实现 音乐详情页面跳转.设计及布局实现设计 叶学涛 编写设置页面 编 ...

  3. 区块链入门到实战(6)之区块链 – 哈希(Hash)

    密码学中,最重要的函数之一是哈希函数.哈希函数将任意大小的数据(内容)映射到固定大小的数据(哈希值). 哈希函数是单向的,从内容生成哈希值很容易,但从哈希值映射到内容很难. 比特币使用SHA-256哈 ...

  4. 哇,ElasticSearch多字段权重排序居然可以这么玩

    背景 读者提问:ES 的权重排序有没有示列,参考参考? 刚好之前也稍微接触过,于是写了这篇文章,可以简单参考下. 在很多复杂的业务场景下,排序的规则会比较复杂,单一的降序,升序无法满足日常需求.不过 ...

  5. Java中解析wav音频文件信息:音频声道数,采样频率,采样位数、声音尺寸

    前言:请各大网友尊重本人原创知识分享,谨记本人博客:南国以南i 音频解析方法: public static int toInt(byte[] b) { return ((b[3] << 2 ...

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

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

  7. rust 模块组织结构

    rust有自己的规则和约定用来组织模块,比如一个包最多可以有一个库crate,任意多个二进制crate.导入文件夹内的模块的两种约定方式... 知道这些约定,就可以快速了解rust的模块系统. 先把一 ...

  8. 揭秘!containerd 镜像文件丢失问题,竟是镜像生成惹得祸

    导语 作者李志宇,腾讯云后台开发工程师,日常负责集群节点和运行时相关的工作,熟悉 containerd.docker.runc 等运行时组件.近期在为某位客户提供技术支持过程中,遇到了 contain ...

  9. 【Flutter 实战】全局点击空白处隐藏键盘

    老孟导读:为什么要实现点击空白处隐藏键盘?因为这是 iOS 平台的默认行为,Android 平台由于其弹出的键盘右上角默认带有关闭键盘的按钮,所以点击空白处不会隐藏键盘. 对于单个页面来说,通过为 T ...

  10. The Data Warehouse Toolkit 阅读笔记

    前言 这篇笔记的主要内容来至于The Data Warehouse Toolkit,该书可以称为数仓建模的圣经 什么是星型模型 以一个业务实时为主表.比如一笔订单就是一个业务事实.订单有商品的SKU信 ...