微服务学习笔记九 Spring Cloud Config本地配置中心

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
简介: 微服务学习笔记九 Spring Cloud Config本地配置中心

Spring Cloud Config,通过服务端可以为多个客户端提供配置服务。

Spring Cloud Config可以将配置文件存储在本地,也可以将存储文件存储到远程

Git仓库。

创建Config Serve,通过它管理所有的配置文件。



**本地文件系统**

nativeconfigserver -> application  声明这是存放本地配置文件系统的模块,

具体的存放文件为:nativeconfigserver->configclient-dev.yml。

nativeconfigclient->bootstrap.yml获取configclient-dev.yml中的配置信息

![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20200716152444798.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0OTY5NjQz,size_16,color_FFFFFF,t_70)

![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20200716152459218.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0OTY5NjQz,size_16,color_FFFFFF,t_70)

创建maven工程,pom.xml


```yaml

<dependencies>

   <dependency>

       <groupId>org.springframework.cloud</groupId>

       <artifactId>spring-cloud-config-server</artifactId>

       <version>2.0.2.RELEASE</version>

   </dependency>

</dependencies>

```


创建application.yml


```yaml

server:

 port: 8762

spring:

 application:

   name: nativeconfigserver

 profiles:

   active: native

 cloud:

   config:

     server:

       native:

         search-locations: classpath:/shared

```


注解说明:

profiles.active:表示的是文件的获取方式

cloud.config.server.native.search-location:本地配置文件存放的路径


resources路径下创建shared文件夹,并在此路径下创建configclient-dev.yml。


```yaml

server:

 port: 8070

foo: foo version 1

```


创建启动类


```java

package com.shuang;


import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.config.server.EnableConfigServer;


@SpringBootApplication

@EnableConfigServer

public class NativeConfigServerApplication {

   public static void main(String[] args) {

       SpringApplication.run(NativeConfigServerApplication.class,args);

   }

}

```


注解说明:

@EnableConfigServer:声明配置中心

创建客户端,来读取本地配置中心的配置文件

创建maven工程,pom.xml


```yaml

<dependencies>

   <dependency>

       <groupId>org.springframework.cloud</groupId>

       <artifactId>spring-cloud-starter-config</artifactId>

       <version>2.0.2.RELEASE</version>

   </dependency>

</dependencies>

```


创建 bootstrap.yml,配置读取本地配置中心的相关信息。


```yaml

spring:

 application:

   name: configclient

 profiles:

   active: dev

 cloud:

   config:

     uri: http://localhost:8762

     fail-fast: true

```


注解说明;

cloud.config.uri:本地Config Server 的访问路径

cloud.config.fail-fast:设置客户端的优先判断Config Server获取是否正常。

通过 spring.application.name 结合 spring.profiles.active拼接目标配置

文件名,configclient-dev.yml,去Config Server 中查找该文件


创建客户端的启动类:


```java

package com.shuang;


import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication

public class NativeConfigClientApplication {

   public static void main(String[] args) {

       SpringApplication.run(NativeConfigClientApplication.class,args);

     

   }

}

```


Handler


```java

package com.shuang.controller;


import org.springframework.beans.factory.annotation.Value;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;


@RestController

@RequestMapping ("/native")

public class NativeConfigHandler {


   @Value("${server.port}")

   private String port;


   @Value("${foo}")

   private String foo;


   @GetMapping("/index")

   public String index(){

       return this.port+"-"+this.foo;

   }

}

```

依次启动:

![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20200716152705432.png)

![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20200716152732747.png)

configclient通过读取configserver的文件,得知端口号为:8070

目录
相关文章
|
1天前
|
Java 数据安全/隐私保护 Sentinel
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
|
1天前
|
运维 监控 Java
SpringCloud&认识微服务
SpringCloud&认识微服务
|
2天前
|
Java API Nacos
第十二章 Spring Cloud Alibaba Sentinel
第十二章 Spring Cloud Alibaba Sentinel
12 0
|
2天前
|
监控 Java 微服务
第八章 Spring Cloud 之 Hystrix
第八章 Spring Cloud 之 Hystrix
|
2天前
|
监控 Java API
第七章 Spring Cloud 之 GateWay
第七章 Spring Cloud 之 GateWay
|
2天前
|
负载均衡 前端开发 Java
第六章 Spring Cloud 之 OpenFeign
第六章 Spring Cloud 之 OpenFeign
|
2天前
|
消息中间件 Java Nacos
第三章 Spring Cloud简介
第三章 Spring Cloud简介
|
2天前
|
Java Nacos 开发者
Java从入门到精通:4.2.1学习新技术与框架——以Spring Boot和Spring Cloud Alibaba为例
Java从入门到精通:4.2.1学习新技术与框架——以Spring Boot和Spring Cloud Alibaba为例
|
28天前
|
Java 应用服务中间件 Maven
SpringBoot 项目瘦身指南
SpringBoot 项目瘦身指南
43 0
|
1月前
|
缓存 安全 Java
Spring Boot 面试题及答案整理,最新面试题
Spring Boot 面试题及答案整理,最新面试题
111 0