Code Monkey home page Code Monkey logo

spring-cloud-config-client's Introduction

spring-cloud-config-client

NodeJS-client for Spring Cloud Config Server

version

Getting Started

npm i @day1co/spring-cloud-config-client
const { getConfig } = require('@day1co/spring-cloud-config-client');

How to get config from Spring Cloud server

Asynchronous(preferred)

const { getConfig } = require('@day1co/spring-cloud-config-client');

getConfig({ endpoint, application, profile, label });

Synchronous

const { getConfigSync } = require('@day1co/spring-cloud-config-client');

getConfigSync({ endpoint, application, profile, label });

Example

const client = require('@day1co/spring-cloud-config-client');

const config = client.getConfigSync({
  endpoint: 'http://localhost:8888',
  application: 'foo',
  profile: 'production',
});

Arguments

  • endpoint (string; default='http://localhost:8888') : The endpoint of spring cloud config server.
  • application (string; default='application') : The name of client application that you would like to get.
  • profile (string; default='default') : The name of client application's environment like D1_ENV or APP_ENV.
  • label (string; default='main') : The name of config-repo's git branch.

System Environments

You can override above arguments by following system environment variables:

  • SPRING_CLOUD_CONFIG_URI for default endpoint.
  • SPRING_CLOUD_CONFIG_NAME for default application.
  • SPRING_CLOUD_CONFIG_PROFILE for default profile.
  • SPRING_CLOUD_CONFIG_LABEL. for default label.

Getting config as nested object

const client = require('@day1co/spring-cloud-config-client');

const config = client.getConfigSync({
  endpoint: 'http://localhost:8888',
  application: 'foo',
  profile: 'production',
});

console.log(config.all);

result

{
  database: {
    host: 'localhost',
    port: 3306,
  },
}

Getting config by its key

const client = require('@day1co/spring-cloud-config-client');

const config = client.getConfigSync({
  endpoint: 'http://localhost:8888',
  application: 'foo',
  profile: 'production',
});

console.log(config.getByKey('database.host'));

result

'localhost';

Overriding config values

You can override configuration value on client system environment variables.

const client = require('@day1co/spring-cloud-config-client');

process.env.DATABASE_HOST = 'overridden';

const config = client.getConfigSync({
  endpoint: 'http://localhost:8888',
  application: 'foo',
  profile: 'production',
});

config.getByKey('database.host');

result

'overridden';

Note that DATABASE_HOST environment key must

  • match the structure of your config file.
  • use snake case with capital letters. In this case, for example, your config file would have lines like,
database:
  host: 'localhost'

Mock Spring Cloud Config Server

Description

You can use a mock Spring-Cloud-Config-Server on localhost:8888 without running "real" Spring-Cloud-Config server on your local PC. It is provided for your local/CI environment.

How to use

  1. Prepare a config file in .js or .json extension.
  • Note that the config file must have a structure same as your own "real" config files that "real" Spring-Cloud-Config server would read.

Here's an example.

application.yml (real config file)

foo:
  bar: 'real'

/Users/me/Desktop/my-project/fake-config.json (fake config file)

{
  "foo": {
    "bar": "real"
  }
}
  1. Use startMockServer function.
  • Arguments
    • filePath (string;) : a relative path of a fake config file
$ pwd
/Users/me/Desktop/my-project
const { startMockServer } = require('@day1co/spring-cloud-config-client');

startMockServer(`fake-config.json`);

Reference

This repository is based on https://github.com/victorherraiz/cloud-config-client

See also https://docs.spring.io/spring-cloud-config/docs/current/reference/html/#_spring_cloud_config_client

spring-cloud-config-client's People

Contributors

moonimooni avatar soomtong avatar jo-seongjun avatar iolo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.