spring boot2X集成spring cloud config

Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持

Spring Cloud Config 分为

  Config Server:

    分布式配置中心,是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息

  Config Client:

    通过指定配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息

Spring boot版本2.1.8.RELEASE

服务中心使用Consu

1.配置中心(服务端)

easy-config

(1)添加依赖

<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>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>

(2)配置

  添加bootstrap.properties

server.port=8091
spring.application.name=easy-config
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.service-name=${spring.application.name}
spring.cloud.consul.discovery.instance-id=${spring.application.name}:${server.port}
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

spring.profiles.active=native
spring.cloud.config.server.native.search-locations=classpath:/config/

(3)修改启动类

package com.tydt.easy.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableConfigServer
@SpringBootApplication
public class EasyConfigApplication {

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

}

2.客户端

easy-api

(1)添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

(2)配置

  添加配置bootstrap.properties

server.port=8083
spring.application.name=easy-api
spring.profiles.active=dev
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500

spring.cloud.consul.discovery.health-check-path=/actuator/health
spring.cloud.consul.discovery.service-name=${spring.application.name}
spring.cloud.consul.discovery.heartbeat.enabled=true

management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=easy-config
#设为true,如果无法连接config server,启动时会抛异常,并停止服务
spring.cloud.config.fail-fast=true

3.测试

启动Consu

启动easy-config

浏览器访问 http://localhost:8091/easy-api/dev

返回结果

{
    "name": "easy-api",
    "profiles": [
        "dev"
    ],
    "label": null,
    "version": null,
    "state": null,
    "propertySources": [
        {
            "name": "classpath:/config/easy-api-dev.properties",
            "source": {
                "spring.datasource.url": "jdbc:mysql://localhost:3306/db?serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNull&allowPublicKeyRetrieval=true",
                "spring.datasource.username": "root",
                "spring.datasource.password": "root",
                "spring.datasource.driver-class-name": "com.mysql.cj.jdbc.Driver"
            }
        }
    ]
}

启动easy-api

以前的数据查询可以正常访问

完成

说明:

  如果出现Connect Timeout Exception on Url - http://localhost:8888. Will be trying the next url if available

  无论在 Config Server 中配置什么端口,Config Client 启动时,会去访问都默认的 8888 端口

  出现这种情况可以删掉以前的配置文件

  在resources文件夹下,新建 bootstrap.properties 文件( bootstrap.yml)

  为什么会这样呢?application.properties(application.yml)同bootstrap.properties(bootstrap.yml)的区别是什么呢?查看

Spring官方文档中文版

原文地址:https://www.cnblogs.com/baby123/p/11954919.html

时间: 2024-07-28 18:49:20

spring boot2X集成spring cloud config的相关文章

Spring Boot集成Spring Security

1.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

SpringBoot系列:Spring Boot集成Spring Cache,使用RedisCache

前面的章节,讲解了Spring Boot集成Spring Cache,Spring Cache已经完成了多种Cache的实现,包括EhCache.RedisCache.ConcurrentMapCache等. 这一节我们来看看Spring Cache使用RedisCache. 一.RedisCache使用演示 Redis是一个key-value存储系统,在web应用上被广泛应用,这里就不对其过多描述了. 本章节示例是在Spring Boot集成Spring Cache的源码基础上进行改造.源码地

Spring Boot集成Spring Scheduler和Quartz Scheduler

本文介绍了Spring Boot集成Spring Scheduler和Quartz Scheduler的基础知识,利用ShedLock解决Spring Scheduler多实例运行冲突,介绍了Quartz ScheduleBuilder.Calendar,介绍了动态创建Quartz Job的方法. GitHub源码 Spring Scheduler Spring Framework提供了简单.易用的Job调度框架Spring Scheduler. 示例 在Spring Boot中,只需两步即可启

Spring MVC集成Spring Data Reids和Spring Session实现Session共享

说明:Spring MVC中集成Spring Data Redis和Spring Session时版本是一个坑点,比如最新版本的Spring Data Redis已经不包含Jedis了,需要自行引入.且最新版本的2.0.1会与Spring MVC 4.1.4有冲突,估计写法错了.所以要明确引入的Spring MVC版本和Spring Data Redis和Spring Session版本. 小提示:如果想要官方明确的版本可以参考Spring Boot的版本,比如我使用了1.4.7的Spring

spring seurity集成spring boot使用DelegatingSecurityContextAsyncTaskExecutor类异步授权authentication登录登出退出信息@async

方法1:将SecurityContextHolder的策略更改为MODE_INHERITABLETHREADLOCAL <beans:bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <beans:property name="targetClass" value="org.springframework.security.co

SpringCloud(3-3)Spring Cloud Config 云端存储配置信息

Spring Cloud Config 云端存储配置信息 Spring Cloud Config 具有中心化,版本控制,支持动态更新,平台独立,语言独立等特性.我们的例子:1.真正的数据存在Git等repository中,2.ScConfigServer从git获取相应信息,3.ScConfigClient从ScConfigServer获取相互之间的通信基于HTTP,TCP,UDP等协议. 一.创建并运行一个ScConfigServer应用 1.pom.xml <project xmlns=&quo

开始Spring Cloud Config

什么是Spring Cloud Config Spring Cloud Config项目提供了一个解决分布式系统的配置管理方案.它包含了Client和Server两个部分. Spring Cloud Config Sever的管理git或svn的外部配置,集中配置到所有客户端. Spring Cloud Config Client根据Spring框架的Environment和PropertySource从Spring Cloud Config Sever获取配置. 所有要开始Spring Clo

spring cloud config git配置的坑

不多说了,直接上列子pom.xml的 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> yml配置设置spring:application:name: xxxcloud:config:server:git:uri: git地址searchP

Spring cloud config 使用gitHub或者gitee连接

1. 创建SpringCloud项目,引入对应的Spring-config-server对应的jar <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.eclips