SrpingCloud 之SrpingCloud config分布式配置中心搭建

1、搭建git环境   目的:持久化存储配置文件信息

   采用码云

  

创建后

继续创建文件夹  用来区分不同的项目

下面就是git上存放配置文件了。环境的区分 dev  sit pre prd   开发  测试 预发布  准生产

sit  和 prd 环境

ConfigServer环境搭建:

   注意Config server也需要注册到注册中心的   client通过别名去读取 而不是实际地址哦

  

分别去对应:

###服务注册到eureka地址
eureka:
  client:
    service-url:
           defaultZone: http://localhost:8100/eureka
spring:
  application:
    ####注册中心应用名称
    name: config-server
  cloud:
    config:
      server:
        git:
          ###git环境地址
          uri: https://gitee.com/toov5/distributed_configuration_file.git
          ####目录文件(区分项目的)
          search-paths:
            - Test    
      ####读取分支环境        
      label: master
####服务端口号      
server:
  port: 8888

 启动类:

package com.toov5;

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 AppConfigServer {
  public static void main(String[] args) {
      SpringApplication.run(AppConfigServer.class, args);
}
}

 

开启Eureka 开启 ConfigServer 

 下面验证配置文件:

 把配置文件存放在git

在git环境上创建配置文件命名规范

   服务名称-配置名称-环境

 

提交以后,启动项目,访问:

 

 下面就是 搭建client 去使用配置文件

pom:

<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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.toov5</groupId>
  <artifactId>SpringCloud-Configclient</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.1.RELEASE</version>
	</parent>
	<!-- 管理依赖 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.M7</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>

		</dependencies>
	</dependencyManagement>
	<dependencies>
		<!-- SpringBoot整合Web组件 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-client</artifactId>
		</dependency>
		<!-- SpringBoot整合eureka客户端 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
	</dependencies>
	<!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/libs-milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>
  
</project>

  bootstrap.yml  (注意一定要utf-8格式的!)

spring:
  application:
    ####注册中心应用名称  git上的前缀
    name:  test-configClient
  cloud:
    config:
    ####读取后缀  prd是对应的后缀
      profile: prd
      ####读取config-server注册地址  confit-server的注册名称 别名
      discovery:
        service-id: config-server
        enabled: true
#####    eureka服务注册地址    
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8100/eureka    
server:
  port: 8882

  controller

package com.toov5.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @Value("${motto}")   //配置的key
  private String motto;
    
    @RequestMapping("/getMotto")
    public String getMotto() {
        return motto;
    }
}

启动

package com.toov5.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

bootstrap.yml文件一定要 utf-8格式的! LZ在写次博文时候就遇到了这个问题 折腾了一会才解决的!

 

猜你喜欢

转载自www.cnblogs.com/toov5/p/9966341.html