Code Monkey home page Code Monkey logo

nacos-spring-boot-project's Introduction

Nacos Spring Boot Project

Build Status

Alibaba Nacos ships main core features of Cloud-Native application, including:

  • Service Discovery and Service Health Check
  • Dynamic Configuration Management
  • Dynamic DNS Service
  • Service and MetaData Management

Nacos Spring Boot Project is based on it and embraces Spring Boot ECO System so that developers could build Spring Boot application rapidly.

Nacos Spring Boot Project consist of two parts: nacos-config-spring-boot and nacos-discovery-spring-boot.

nacos-config-spring-boot module is using for Dynamic Configuration Management and Service and MetaData Management.

nacos-discovery-spring-boot module is using for Service Discovery, Service Health Check and Dynamic DNS Service.

Samples

Dependencies & Compatibility

master branch

Dependencies Compatibility
Java 1.8+
Spring Boot 2.0.3.RELEASE

1.x branch

Dependencies Compatibility
Java 1.7+
Spring Boot 1.4.1.RELEASE

Quick Start

Nacos Config

First, you have to start a Nacos Server in backend , If you don't know steps, you can learn about quick start.

Suppose your Nacos Server is startup, you would add nacos-config-spring-boot-starter in your Spring application's dependencies :

    <dependencies>
        ...
        
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-config-spring-boot-starter</artifactId>
            <version>${latest.version}</version>
        </dependency>
        
        ...
    </dependencies>

After that, you could define some configurations in application.properties:

nacos.config.server-addr=localhost

nacos.config.server-addr attribute configures "${host}:${port}" of your Nacos Server

Then you could using @SpringBootApplication to annotate main class like normal SpringBoot Application and startup:

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

If you'd like to use "Distributed Configuration" features, ConfigService is a core service interface to get or publish config, you could use "Dependency Injection" to inject ConfigService instance in your Spring Beans.

@Service
public class ConfigServiceDemo {

    @NacosInjected
    private ConfigService configService;
    
    public void demoGetConfig() {
        try {
            String dataId = "{dataId}";
            String group = "{group}";
            String content = configService.getConfig(dataId, groupId, 5000);
        	System.out.println(content);
        } catch (NacosException e) {
            e.printStackTrace();
        }
    }
    ...
}

above code equals below one:

try {
    // Initialize the configuration service, and the console automatically obtains the following parameters through the sample code.
    String serverAddr = "{serverAddr}";
    String dataId = "{dataId}";
    String group = "{group}";
    Properties properties = new Properties();
    properties.put("serverAddr", serverAddr);
    ConfigService configService = NacosFactory.createConfigService(properties);
    // Actively get the configuration.
    String content = configService.getConfig(dataId, group, 5000);
    System.out.println(content);
} catch (NacosException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Nacos Discovery

First, you have to start a Nacos Server in backend , If you don't know steps, you can learn about quick start.

Suppose your Nacos Server is startup, you would add nacos-discovery-spring-boot-starter in your Spring application's dependencies :

    <dependencies>
        ...
        
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-discovery-spring-boot-starter</artifactId>
            <version>${latest.version}</version>
        </dependency>
        
        ...
    </dependencies>

After that, you could define some configurations in application.properties:

nacos.discovery.server-addr=localhost

nacos.discovery.server-addr attribute configures "${host}:${port}" of your Nacos Server

Then you could using @SpringBootApplication to annotate main class like normal SpringBoot Application and startup:

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

If you'd like to use "Service Registry" features, NamingService is a core service interface to get or publish config, you could use "Dependency Injection" to inject NamingService instance in your Spring Beans.

@Service
public class NamingServiceDemo {

    @NacosInjected
    private NamingService namingService;
    
    public void demoRegisterService() {
        try {
            namingService.registerInstance("test-service", "1.1.1.1", 8080);
        } catch (NacosException e) {
            e.printStackTrace();
        }
    }
    ...
}

above code equals below one:

try {
    // Initialize the naming service, and the console automatically obtains the following parameters through the sample code.
    String serverAddr = "{serverAddr}";
    Properties properties = new Properties();
    properties.put("serverAddr", serverAddr);
    NamingService naming = NamingFactory.createNamingService(properties);
namingService.registerInstance("test-service", "1.1.1.1", 8080);
} catch (NacosException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

For more information about Nacos Spring, see Nacos Spring Project.

Endpoint

Nacos config starter and Nacos discovery starter also support the implementation of Spring Boot actuator endpoints.

Prerequisite:

Adding nacos-config-spring-boot-actuator to your pom.xml in Nacos Config Project.

Adding nacos-discovery-spring-boot-actuator to your pom.xml in Nacos Discovery Project.

Then Configure your endpoint security strategy.

  • Spring Boot1.x
management.security.enabled=false
  • Spring Boot2.x
management.endpoints.web.exposure.include=*

To view the endpoint information, visit the following URLS:

Nacos Config Project :

Nacos Discovery Project:

Health Checks

nacos-config-spring-boot-actuator and nacos-discovery-spring-boot-actuator support the standard Spring Boot HealthIndicator as a production-ready feature , which will be aggregated into Spring Boot's Health and exported on HealthEndpoint that works MVC (Spring Web MVC) if it is available.

Suppose a Spring Boot Web application did not specify management.server.port(SpringBoot1.x using management.port), you can access http://localhost:{port}/actuator/health(SpringBoot1.x visit http://localhost:{port}/health) via Web Client and will get a response with JSON format is like below :

Nacos Config Project:

{
"status": "UP",
"details": {
    "nacosConfig": {
        "status": "UP"
    },
    "diskSpace": {
        "status": "UP",
        "details": {
            "total": 250140434432,
            "free": 52323512320,
            "threshold": 10485760
        }
    }
}
}

Nacos Discovery Project:

{
"status": "UP",
"details": {
    "nacosDiscovery": {
        "status": "UP"
    },
    "diskSpace": {
        "status": "UP",
        "details": {
            "total": 250140434432,
            "free": 52323680256,
            "threshold": 10485760
        }
    }
}
}

For more information about Nacos Spring, see Nacos Spring Project.

Relative Projects

nacos-spring-boot-project's People

Contributors

fangjian0423 avatar flystar32 avatar mercyblitz avatar

Watchers

 avatar  avatar  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.