1 Star 2 Fork 1

grey-hat / nacos-group

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
NACOS-DISCOVERY-QUICK-START.md 4.13 KB
一键复制 编辑 原始数据 按行查看 历史
format 提交于 2018-12-25 20:40 . update docs

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>

Note: Version 0.2.x.RELEASE is compatible with the Spring Boot 2.x. Version 0.1.x.RELEASE is compatible with the Spring Boot 1.x.

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

nacos.discovery.server-addr=localhost:8848

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 discovery starter also support the implementation of Spring Boot actuator endpoints.

Prerequisite:

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:

Health Checks

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 :

{
"status": "UP",
"details": {
    "nacosDiscovery": {
        "status": "UP"
    },
    "diskSpace": {
        "status": "UP",
        "details": {
            "total": 250140434432,
            "free": 52323680256,
            "threshold": 10485760
        }
    }
}
}
Java
1
https://gitee.com/grey-hat/nacos-group.git
git@gitee.com:grey-hat/nacos-group.git
grey-hat
nacos-group
nacos-group
master

搜索帮助