SpringCloud之Config

配置中心,也就是SpringCloud中的Config组件,主要应用在哪些方面?

  • 配置文件方便维护
  • 配置文件内容安全和权限
  • 更新项目配置不需要重启

本文主要围绕两个方面,一个是Config Server,另一个是Config Client。还是以我个人博客系统其中的一个模块为例。

一、搭建Config Server

1.Maven依赖

复制代码
<dependencies>
       <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-web</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-config-server</artifactId>
       </dependency>
   </dependencies>
复制代码

2.添加主类

复制代码
package com.springcloud.blog;

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
@EnableConfigServer
@EnableDiscoveryClient
@EnableEurekaClient
public class BlogConfigServerApplication {


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


}
复制代码

3.application.yml配置文件修改

spring.application.name=blog-config-server
server.port=8771
spring.cloud.config.server.git.uri=https://github.com/developers-youcong/blog-springcloud-config
spring.cloud.config.server.git.searchPaths=respo
spring.cloud.config.label=master
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

4.启动测试

 

 

二、搭建Config Client

1.Maven依赖

复制代码
<dependencies>
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-config</artifactId>
      </dependency>

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

      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      </dependency>
  </dependencies>
复制代码

2.添加主类

复制代码
package com.springcloud.blog;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@RestController
@RefreshScope
public class BlogConfigClientApplication {


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

    @Value("${version}")
    String version;

    @RequestMapping("/getVersion")
    public String getVersion() {
        return version;
    }

}
复制代码

3.bootstrap.properites配置文件

spring.application.name=blog-config-client
spring.cloud.config.label=master
spring.cloud.config.profile=dev
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
spring.cloud.config.discovery.enabled=true
spring.cloud.config.uri= http://localhost:8771/
spring.cloud.config.discovery.serviceId=blog-config-server
server.port=8772

4.测试验证

浏览器输入:http://localhost:8772/getVersion   输出结果如下:

get version 1

 

posted @   挑战者V  阅读(265)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
历史上的今天:
2019-06-08 VsCode插件与Node.js交互通信
2019-06-08 request:fail url not in domain list
2019-06-08 安卓进阶开发资料之分享
2019-06-08 算法图解之散列表
2018-06-08 shiro实战系列(五)之Authentication(身份验证)
点击右上角即可分享
微信分享提示