Digilife-Config Server

本文详细介绍了使用Spring Cloud Config作为微服务配置中心的实践过程,包括配置服务器与客户端的搭建,实现外部化配置,以及通过Actuator进行刷新,确保应用能够实时获取到最新的配置信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Server Side

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.hsbc.ins.digilife</groupId>
    <artifactId>config</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
        <spring.cloud.version>2.1.1.RELEASE</spring.cloud.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>  //Add Config Server
            <version>${spring.cloud.version}</version>
        </dependency>
    </dependencies>

</project>
package com.hsbc.ins.digilife.config;

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

@SpringBootApplication
@EnableConfigServer  //add config server
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

(applicationg)-(profile).yml

a
application.yml

server:
  port: 8888
spring:
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          searchLocations: classpath:/config/{application}

Client Side

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-client</artifactId>
    <version>${spring.cloud.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
server:
  port: 8080
spring:
  application:
    name: gateway
  profiles:
    active: default
  cloud:
    config:
      url: http://localhost:8888

management:
  endpoints:
    web:
      exposure:
        include: refresh  //actuator
package com.hsbc.ins.digilife.gateway.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope
@ConfigurationProperties
public class AppTest {

    private static Logger log = LoggerFactory.getLogger(AppTest.class);

    @Value("${user}")
    private  String user;

    @GetMapping("/letter/{lettername}")
    public String getLetter(@PathVariable("lettername") String letterName){
        return "letter" + letterName;
    }

    @GetMapping("/")
    public String home() {
        log.info("Access /");
        return "Hi!" +  user;
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值