玩转SpringCloud专题(二十四)-Spring Cloud Config分布式配置中心高可用及刷新配置

Spring Cloud入门2——配置中心Config Spring Cloud Config 配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储、Git以及Subversion。 也就是说,我们可以使用Spring Cloud Config来获取远程服务器上的配置信息。 可以分为两个部分: 服务端: 配置服务端,服务管理配置信息 客户端:客户端调用server端暴露接口获取配置信息 阅读详情

1.前言

上一篇文章已经实现了Spring Cloud Config分布式配置中心的功能,我们已经可以通过Config Server获取Git远程仓库配置文件中的内容,但是在实际项目中,当服务实例数量比较多的情况下,每个服务单元都从配置中心获取配置信息,假如配置中心由于网络等原因挂掉了,这时候会导致那些服务实例请求失败等一系列问题。这时候可以考虑将配置中心做成一个微服务,注册到Eureka中,这样启动多个Config Server就可以实现负载均衡,从而实现配置中心的高可用。
在这里插入图片描述

2.准备工程

springcloud-eureka-server: 服务注册中心,端口7001
springcloud-config-server-8585 高可用配置中心,端口8585
springcloud-config-client-8484 具体服务单元,端口8484

3.新建springcloud-config-server-8585工程

因为要将配置中心作为一个服务注册到eureka中,所以注意引入config-server以及eureka的依赖,具体pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud0310</artifactId>
        <groupId>com.bruceliu.springcloud0310</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-config-server-8585</artifactId>

    <dependencies>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Eureka客户端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


</project>

4.启动类加上@EnableConfigServer以及@EnableDiscoverClient注解

package com.bruceliu;

/**
 * @BelongsProject: springcloud0310
 * @BelongsPackage: com.bruceliu
 * @Author: bruceliu
 * @QQ:1241488705
 * @CreateTime: 2020-03-12 22:03
 * @Description: 实际项目中当服务单元很多时,每个服务单元都从配置中心读取文件,这时可以将配置中心做成一个微服务(注册到eureka),将其集群化(负载均衡),达到高可用
 */

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;


@SpringBootApplication
@EnableEurekaClient // 开启EurekaClient功能
//开启分布式配置功能
@EnableConfigServer
public class SpringcloudHignAvailabilityConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringcloudHignAvailabilityConfigServerApplication.class, args);
    }
}
@EnableConfigServer: 开启分布式配置功能

5.config-server的配置文件application.yml

配置文件需要配置git仓库地址,访问路径,分支,访问用户名,密码等,注意需要配置

#端口号
server:
  port: 8585
#应用名称
spring:
  application:
    name: hign-availability-config-server
  cloud:
    config:
      server:
        git:
          #git远程仓库地址
          uri: https://gitee.com/zhouxingchi12345/SpringCloudConfig.git
          #访问git仓库的用户名(公开仓库不需要配置用户名和密码,私有仓库才需要配置)
          username: zhouxingchi12345@126.com
          #访问git仓库的密码
          password: zhouxingchi54321
          #git仓库配置文件的路径
          search-paths: repository
      #git仓库分支(默认master)
      label: master
#配置注册到eureka服务注册中心的地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://bruceliu:123456@eureka7002.com:7002/eureka,http://bruceliu:123456@eureka7001.com:7001/eureka,http://bruceliu:123456@eureka7003.com:7003/eureka
  instance:
    prefer-ip-address: true
    instanceId: hign-availability-config-server:8585

注意:这里还需要将上篇文章解决中文乱码的问题加到config-server中,防止出现中文乱码问题,具体可以参考上篇文章。
在这里插入图片描述

6.新建config-client工程

注意加入spring-cloud-starter-config、 spring-cloud-starter-eureka依赖,具体pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud0310</artifactId>
        <groupId>com.bruceliu.springcloud0310</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-config-client-8484</artifactId>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Eureka客户端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!--开启监控功能-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

启动类

package com.bruceliu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * @BelongsProject: springcloud0310
 * @BelongsPackage: com.bruceliu
 * @Author: bruceliu
 * @QQ:1241488705
 * @CreateTime: 2020-03-12 16:58
 * @Description: TODO
 */
@SpringBootApplication
@EnableEurekaClient // 开启EurekaClient功能
public class ConfigClientApp {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApp.class, args);
    }
}

7.config-client配置文件bootstrap.yml

注意名称是bootstrap.yml

server:
  port: 8484
#注意application-name(配置文件前缀)需要对应git仓库中 config-client-dev、config-client-test、config-client-prod为名称的配置文件
spring:
  application:
    name: config-client
  cloud:
    config:
      #指定当前所属环境
      profile: prod
      discovery:
        #开启通过服务访问config-server的功能
        enabled: true
        #指定配置中心注册到eureka的serviceId(即config-server的application-name)
        serviceId: hign-availability-config-server
      #git仓库分支
      label: master
#配置注册到eureka服务注册中心的地址
eureka:
  client:
    registry-fetch-interval-seconds: 5 # 获取服务列表的周期:5s
    service-url:
      defaultZone: http://bruceliu:123456@eureka7002.com:7002/eureka,http://bruceliu:123456@eureka7001.com:7001/eureka,http://bruceliu:123456@eureka7003.com:7003/eureka
  instance:
    prefer-ip-address: true
    ip-address: 127.0.0.1
spring.cloud.config.discovery.enabled:true 表示开启通过服务访问config-server功能

spring.cloud.config.discovery.serviceId:hign-availability-config-server 表示指定配置中心注册到eureka的serviceId(即config-server的application-name)

8.新建GetPropertyFromConfigServerController

主要用于测试从git仓库读取配置文件内容

package com.bruceliu.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试客户端从配置服务中心获取配置文件中的内容
 * @date 2018/9/17 22:05
 */
@RestController
//@RefreshScope注解的作用: 如果刷新了bean,那么下一次访问bean(即执行一个方法)时就会创建一个新实例。
@RefreshScope
public class GetPropertyFromConfigServerController {

    private static Logger logger = LoggerFactory.getLogger(GetPropertyFromConfigServerController.class);

    @Value("${com.springcloud.bruceliu.message}")
    String message;

    @RequestMapping("/getPropertyFromConfigServer")
    public String getPropertyFromConfigServer() {
        System.out.println(message);
        String msg = "hello, i am " + message + ", i'm come from config server";
        logger.info(msg);
        return msg;
    }

}
注意:需要加上@RefreshScope注解,主要是用于后面配置刷新用到。
@RefreshScope注解的作用: 如果刷新了bean,那么下一次访问bean(即执行一个方法)时就会创建一个新实例。

9.测试

分别启动eureka-server、config-server,config-client项目,如图
在这里插入图片描述
可以看到,config-server以及config-client都成功注册到eureka。

这时我们访问 http://localhost:8484/getPropertyFromConfigServer
在这里插入图片描述
可以看到,我们成功从git仓库中获取到配置文件中的内容。我们可以将config-server启动多个实例,然后访问 http://localhost:8484/getPropertyFromConfigServer,可以看到成功获取配置信息,然后我们可以停掉其中一个config-server实例,我们继续访问 http://localhost:8484/getPropertyFromConfigServer,还是可以成功获取git远程仓库的配置信息,这就实现了config-server的高可用。

下面我们聊聊配置刷新的问题,在实际项目中,假如我们修改了git仓库中的配置文件的信息,这时候是不会立即生效的,下面我们先讲下手动刷新的方法,实际项目中是通过Spring Cloud Bus消息总线的方式自动刷新远程仓库配置文件,这个有时间后续讲解。

10.改造config-client

首先我们做一个测试,我们尝试使用Git工具修改当前配置的内容,比如,将repository/config-client-prod.properties中的com.springcloud.wsh.message的值修改为Spring Cloud Config分布式配置中心(正式生产环境)-----HELLO,
在这里插入图片描述
这时候我们访问http://localhost:8484/getPropertyFromConfigServer,
在这里插入图片描述
可以看到返回的配置内容还是旧的,并没有同步更新。

下面我们开始改造config-client,在config-client的pom.xml中加入监控的依赖:spring-boot-starter-actuator

<!--开启监控功能-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

acutator包含了/refresh刷新API,我们可以调用它来实现配置文件的刷新。

bootstrap.properties

#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

a、重新启动config-client,访问一次http://localhost:3333/getPropertyFromConfigServer,可以看到当前的配置值:
在这里插入图片描述
b、修改Git仓库配置文件中的com.springcloud.wsh.message值
在这里插入图片描述
c、再次访问一次http://localhost:8484/getPropertyFromConfigServer,可以看到配置值没有改变:
在这里插入图片描述

d、通过POST请求发送到http://localhost:8484/refresh,这时候不能通过浏览器,注意是post请求,否则会报错
在这里插入图片描述
这里我们使用postman发送post请求,如下图:
在这里插入图片描述
e、我们再次访问http://localhost:8484/getPropertyFromConfigServer,可以看到配置值已经是更新后的值
在这里插入图片描述
至此,我们通过actuator实现了手动刷新git远程仓库中的配置文件的内容。注意一下在config-client项目中的controller上需要加上@RefreshScope注解, 否则调用/refresh接口看不到更新的内容,笔者就是踩了这个坑,需要注意下。

如果在执行post请求时出现以下提示
在这里插入图片描述
postman拦截了我们的post请求,这是因为springboot的安全拦截,由于我们需要测试是否刷新成功,我们可以在config-client的配置文件application.yml中加入

#忽略权限拦截
management:
  security:
    enabled: false

11.总结

本文主要讲解了如何实现了高可用的Config Server配置中心以及利用Actuator暴露的refresh接口来实现配置的动态更新。通过前面几篇文章的学习,我们主要学了Eureka服务注册中心、Zuul网关路由、Ribbon负载均衡、Feign声明式服务调用、Config服务配置中心,这时候我们可以抽象一个比较简单的微服务架构图:
在这里插入图片描述

springcloud config配置中心概述 SpringCloudConfig简介 SpringCloudConfig是SpringCloud提供的分布式配置管理方案,包含ConfigServer(配置中心)和Client两部分。Server以接口形式提供配置文件的存储与访问,Client从Server获取配置初始化应用。相比直接使用本地配置文件,它能实现配置的集中管理、动态更新(无需重启服务)和版本控制(默认集成Git存储)。虽然功能上不如Apollo等专业配置中心强大,但Config与SpringCloud项目集成简单,适合微服务架构下大量服务的 阅读详情

相关推荐

spring cloud config整合gitlab搭建分布式的配置中心

在前面的博客中,我们都是将配置文件放在各自的服务中,但是这样做有一个缺点,一旦配置修改了,那么我们就必须停机,然后修改配置文件后再进行上线,服务少的话,这样做还无可厚非,但是如果是成百上千的服务了,这个时候,就需要用到分布式的配置管理了。而spring cloud config正是用来解决这个问题而生的。下面就结合gitlab来实现分布式配置中心的搭建。spring cloud config配置中

牛奋lch 1万+

总结:Spring Cloud Config

Spring Cloud Config总结

w2009211777的专栏 1210

深入解析Spring Cloud Config:多样配置中心的实现与高可用策略

这篇文章探讨了配置中心的重要性及其在分布式系统中的应用,特别关注Spring Cloud Config。文章首先介绍了配置中心的由来、功能及选择标准,然后详细阐述了Spring Cloud Config的基本实现方法,包括结合Git、关系型数据库(MySQL)和非关系型数据库(MongoDB)的方案。此外,还讨论了配置中心的高可用性、客户端自动刷新机制及安全认证等扩展功能。最后,文章对比了Spring Cloud Config与Apollo的特性,强调Apollo在企业级应用中的优势。

曾经“等你生日那天”都遥远得像未来,如今却可欢愉的挥手说“下个十年见” 168万+

Spring Cloud从入门到精通(六):配置中心

在使用Spring Boot的时候,我们往往会在application.properties配置文件中写一些值,供应用使用,这样做的好处是可以在代码中引用这些值,当这些值需要作出修改的时候,可以直接修改配置文件就重启生效了(如果你部署的是war格式)。但是在Spring Cloud这种大量微服务的架构中,这么做很容易出错,增加了运维的成本。 本篇要介绍的配置中心就是要把所有微服务需要使用的配置值...

mikaell的专栏 350

SpringCloud系列之分布式配置中心急速入门教程

分布式微服务项目,就会有很多的模块,比如微服务A、微服务B等等,每个工程都有一套配置文件,随着业务增长,肯定会有很多配置,分散管理,不能实现统一的管理,所以就有了微服务的配置文件统一管理组件,比如spring cloud官方的spring cloud config、携程的 Apollo,还有最近比较火的阿里 nacos,每款产品各有自己的特点,不过本博客只介绍spring cloud config

Nicky's blog 4537

机器学习系列(1)_逻辑回归初步

1、总述 逻辑回归是应用非常广泛的一个分类机器学习算法,它将数据拟合到一个logit函数(或者叫做logistic函数)中,从而能够完成对事件发生的概率进行预测。 2、由来     要说逻辑回归,我们得追溯到线性回归,想必大家对线性回归都有一定的了解,即对于多维空间中存在的样本点,我们用特征的线性组合去拟合空间中点的分布和轨迹。如下图所示:     线性回归能对连续值结果进行预测,

寒小阳 13万+

笔记--Spring Cloud(六):配置中心(服务化与高可用)

Spring Cloud(六):配置中心(服务化与高可用) 先来回顾一下,在前文中我们完成了什么: 构建了 config-server-git,连接到 Git 仓库 在 Gitee 上创建了一个 config-repo 目录,用来存储配置信息 构建了 config-client,来获取 Git 中的配置信息 在 config-client 中开启了 Refresh,动态刷新配置信息 在本文中,我们继续来看看 Spring Cloud Config 的一些其他能力。 高可用问题 传统作法 通常在生产环境,

qq_44534541的博客 428

20个闭环渗透测试项目:从漏洞发现到简历突围

渗透测试不仅是工具使用,更是对系统设计缺陷与实现偏差的深度推演。理解OWASP Top 10、Burp Suite等基础能力只是起点,真正具备工程价值的是能复现、可验证、可协作的完整攻击链。本文聚焦真实业务场景下的SSRF协议绕过、JWT签名机制缺陷、Redis未授权利用、Kong网关配置风险及etcd存储结构分析等核心热词,揭示漏洞本质在于架构决策与配置偏差的叠加态。适用于红队实战、甲方安全建设与乙方渗透审计等多类岗位,帮助从业者将技术动作升维为技术叙事。

weixin_32234493的博客 417

【微服务架构】SpringCloud(七):配置中心 Spring Cloud Config

存放配置的地方:git ,本地文件 等。从 1 读取配置。是 config server 的客户端 消费配置。

专注上位机开发、机器视觉、机器学习、嵌入式软硬件开发 1万+

FPGA+滨淞CCD实战:手把手教你搭建S7031/S10142成像电路(附避坑指南)

本文详细介绍了使用FPGA驱动滨淞CCD传感器(S7031/S10142)构建高精度成像系统的完整实战流程。内容涵盖核心器件选型、系统架构规划、电路板(焦面板与接口板)设计要点、FPGA Verilog逻辑实现,并提供了从电源管理、信号调理到数据采集的详细避坑指南,帮助读者从零搭建稳定可靠的成像电路。

weixin_29271263的博客 230

Windows10内核逆向-2-KiSystemCall64的调用流程剖析

本文深入剖析了Windows 10 x64系统中KiSystemCall64函数的完整调用流程。从用户态触发syscall指令开始,详细讲解了如何通过swapgs切换内核栈、构建KTRAP_FRAME陷阱帧、进行SMAP/CFG等安全检查,到最终通过SSDT寻址并执行目标系统服务的全过程,揭示了系统调用从Ring 3到Ring 0切换的核心机制与安全设计。

weixin_29209805的博客 84

【spring cloud】(五)配置中心——springcloud Config

简单来说,Spring Cloud Config就是我们通常意义上的配置中心,也就是微服务项目中,每一个微服务都需要配置相应的配置,如果不同服务的配置文件有相同的配置,如果这些相同配置需要修改的话就可以在配置中心上进行修改。

扎哇太枣糕的笔记博客 1773

Workbuddy+DeepSeek V4傻瓜流配置全指南:API接入、模型切换与Agent实战

AI编程助手的核心价值不在于模型本身,而在于如何稳定、安全、低门槛地调用其能力。Agent作为智能体范式,依赖可靠的运行时环境与标准化API通信;DeepSeek V4系列凭借高吞吐、低延迟和强工具调用支持,成为当前生产级Agent的理想引擎。Workbuddy则通过本地配置管理、环境变量注入与UTF-8无BOM容错等工程设计,将复杂部署简化为可复现、可版本化、可团队协同的实践流程。本文聚焦API接入路径下的真实卡点——从Key获取验证、models.json字段语义到沙箱权限控制,覆盖开发者在Window

weixin_33702545的博客 2403

SpringCloud - 配置中心

微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。

m0_66689823的博客 5972

【WRF-Urban】使用 LCZ 替换 WRF 运行中的 LUCC 数据

使用 LCZ 替换 WRF 运行中的 LUCC 数据

WW、forever的博客 995

HanLP的分词

HanLP的分词

sikh_0529的博客 1万+

springCloud08—分布式配置中心:Spring Cloud Config

其中囊括了JVM、锁、并发、Java反射、Spring原理、微服务、Zookeeper、数据库、数据结构等大量知识点。

2401_83329718的博客 760

Ollama本地大模型-个性化AI:训练自己的风格模型和数据投喂打造专属智能体验

之前我们使用Ollama下载了deepseek模型,现在我想将其训练成自己风格的模型,应该怎么做?

瑶山的博客 1784

SpringCloud Config 分布式配置中心

介绍 Config分布式配置中心。

快乐的小三菊的博客 1158

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

使用IDEA开发Spring Cloud项目(九)分布式配置中心Config

概述 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在 Spring Cloud 中,有分布式配置中心组件 Spring Cloud Config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程 Git 仓库中。在 Spring Cloud Config 组件中,分两个角色,一是 Config Server,二是 Config...

Micro麦可乐的博客 2030

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

SpringCloud Config(配置中心)实现配置自动刷新总结

我们将这个接口配置到远程的git的webhook上,当git上的文件内容发生变动时,就会自动调用/bus-refresh接口。1、打破了微服务的职责单一性。引入:配置文件统一放在配置中心,配置中心文件明文存在不安全,容易泄露比如数据库用户名、密码等,如何实现git仓库配置文件为密文时,通过配置中心在Config-Server端进行解密。备注:这里给出方式二配置方法,方式一的区别在:因为是某个微服务承担配置刷新的职责,所以Server端不需要配置 Rabbitmq和添加bus-amqp的依赖。

m0_67392661的博客 3787

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

SpringCloud Config配置中心的高可用的简单案例

一、cloud-config配置 1、启动三台cloud-config三台这样的服务:端口号分别设置为8081,8082,8083 2、现在把三台cloud-config三台一起部署 3、重启下CloudProviderApplication的客户端 可以看到控制台上有它发现了多个服务的Url地址的链表,最终选择了8082这台配置中心来拉去我们的配置。 4、现在把8082这台给宕机掉。相当...

Brave_heart4pzj的博客 3651

Spring Cloud: 配置中心 Config、Bus

配置中心作用 分布式系统中,项目越来越大,子项目越来越多,配置文件也协同零零散散的子项目星罗棋布,往往更改一个基础服务的配置要导致一些列重启,造成运维困难,维护成本高而且容易出错。为了解决这一问题,配置中心应运而生。配置中心开源项目比较多,这里主要介绍Cloud全家桶的Spring Cloud Config。 Consul Consul也可以作为配置中心,之前文章有介绍:Spring Cloud: 注册中心Consul使用, 但是功能较少,没有远程服务 Spring Cloud Config SpringC

泛泛之素 6580

springcloud-config配置中心

微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的(配置指的是springboot项目当中的application.properties/application.yml)

怪咖@的博客 3268

《Spring Cloud 入门》Spring Cloud Config 基于JDBC 的分布式配置中心

《Spring Cloud 入门》Spring Cloud Config 基于JDBC 的分布式配置中心 标签:spring cloud 入门,spring cloud config,spring cloud config jdbc, spring cloud 配置中心基于数据库,spring cloud config 入门,spring cloud config 底层,spring cloud...

弱弱的猿 2593

Spring Cloud Config:分布式配置中心(附配置中心和客户端代码)

Spring Cloud Config 提供了一种集中管理微服务配置的解决方案,通过配置中心(Server)和客户端(Client)实现。配置中心从Git等远程仓库获取配置,客户端启动时获取并加载配置。搭建步骤包括:1)创建Config Server项目并配置Git仓库地址;2)创建Config Client项目并连接配置中心;3)测试配置加载。文中提供了完整代码示例,并解释了配置动态刷新机制。这种架构解决了微服务配置分散的问题,提高了系统可维护性。

fykam的博客 1206
上一篇: 玩转SpringCloud专题(二十三)-Spring Cloud Config分布式配置中心
下一篇: Docker专题(十一)-Docker-Compose
熊猫IT技术
熊猫IT技术 优质创作者: Java技术领域 优质创作者: Java技术领域 领域专家: 后端开发技术领域 领域专家: 后端开发技术领域
博客等级 码龄10年 6021粉丝 1312原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

熊猫IT技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值