spring cloud 搭建(配置中心)

Posted 正怒月神

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring cloud 搭建(配置中心)相关的知识,希望对你有一定的参考价值。

创建配置中心:

选择Spring Initializr模板

 

选择Config Server

 

pom文件

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.test</groupId>
    <artifactId>cloud-config</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>cloud-config</name>
    <description>Demo project for Spring Boot</description>
 
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
    </properties>
 
    <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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <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>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
 
</project>

 

添加服务注解:

@EnableConfigServer
@EnableEurekaClient

 

创建配置文件(切换配置文件的示例,在这里一并展示)

config文件夹:

dev:测试环境

prod:正式环境

 

config-dev.yml

registry:
  url: http://${host:localhost}:9010/eureka/
 
# 测试属性
isDebug: true

config-prod.yml

registry:
  url: http://${host:localhost}:9010/eureka/
 
# 测试属性
isDebug: false

bootstrap.yml

spring:
  application:
    name: cloud.config
  profiles:
    #native表示本地方式
    active: native
  cloud:
    config:
      server:
        native:
          # 配置文件存放路径
          search-locations: classpath:/config/

application.yml

server:
  port: 9020
 
eureka:
  client:
    service-url:
      defaultZone: http://localhost:9010/eureka/

 

我们启动  注册中心  和  配置中心

 

以上是关于spring cloud 搭建(配置中心)的主要内容,如果未能解决你的问题,请参考以下文章

Spring Cloud 入门教程 - 搭建配置中心服务

SpringBoot + Spring Cloud +Vue 管理系统后台搭建(十六配置中心ConfigBus)

Spring Cloud 如何搭建Config

spring cloud 搭建注册中心Eureka(集群模式)

以zookeeper为注册中心搭建spring cloud环境bug

以zookeeper为注册中心搭建spring cloud环境bug