springboot2.X手册:Eureka不更,Consul被禁,启用Nacos

引入包体

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.boots</groupId>
        <artifactId>boots</artifactId>
        <version>1.1.0.RELEASE</version>
    </parent>
    <artifactId>boots-register</artifactId>
    <name>boots-register</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>

        <!-- 公共组件:swagger服务+入参出参+统一异常拦截 -->
        <dependency>
            <groupId>com.boots</groupId>
            <artifactId>module-boots-api</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>

        <!-- nacos服务注册包 -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-discovery-spring-boot-starter</artifactId>
            <version>0.2.7</version>
        </dependency>

        <!-- nacos配置包 -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-config-spring-boot-starter</artifactId>
            <version>0.2.7</version>
        </dependency>



    </dependencies>
</project>

配置类

/**
 * All rights Reserved, Designed By 林溪
 * Copyright:    Copyright(C) 2016-2020
 * Company       溪云阁 .
 */

package com.boots.register.common.config;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;

/**
 * Nacos配置
 * @author:溪云阁
 * @date:2020年6月12日
 */
@Configuration
public class NacosConfig {

    @Value("${server.port}")
    private int serverPort;

    @Value("${spring.application.name}")
    private String applicationName;

    @Value("${nacos.host}")
    private String nacosHost;

    @NacosInjected
    private NamingService namingService;

    @PostConstruct
    public void registerInstance() throws NacosException {
        namingService.registerInstance(applicationName, nacosHost, serverPort);
    }

}

属性文件配置

######配置基本信息######
##配置应用端口号
server.port: 8080
##配置应用名称
spring.application.name: boots-register
##配置时间格式,为了不精度丢失,所有换成字符串
spring.jackson.timeZone: GMT+8
spring.jackson.dateFormat: yyyy-MM-dd HH:mm:ss
spring.jackson.generator.writeNumbersAsStrings: true
##注册及配置中心地址
nacos.host: 127.0.0.1
nacos.port: 8848
nacos.config.serverAddr: ${nacos.host}:${nacos.port}
nacos.discovery.serverAddr: ${nacos.host}:${nacos.port}

启动类配置

package com.boots.register;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;

/**
 * 服务启动类
 * @author:溪云阁
 * @date:2020年5月2日
 */
@SpringBootApplication
@ComponentScan(basePackages = { "com.module""com.boots" })
@NacosPropertySource(dataId = "DATA_BOOTS_ID", groupId="GROUP_BOOTS_ID", autoRefreshed = true)
public class BootsRegisterApplication {

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

}

接口编写

/**
 * All rights Reserved, Designed By 林溪
 * Copyright:    Copyright(C) 2016-2020
 * Company       溪云阁 .
 */

package com.boots.register.view.register.view;

import java.util.List;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.module.boots.api.message.ResponseMsg;
import com.module.boots.api.utils.MsgUtils;
import com.module.boots.exception.CommonRuntimeException;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;

/**
 * Nacos控制器
 * @author:溪云阁
 * @date:2020年6月12日
 */
@Api(tags = { "web服务:查询注册中心上的信息" })
@RestController
@RequestMapping("view/minio")
public class NacosView {

    @NacosInjected
    private NamingService namingService;

    @NacosValue(value = "${project.name:null}", autoRefreshed = true)
    private String projectName;

    /**
     * 获取项目名称
     * @author 溪云阁
     * @return ResponseMsg<String>
     */
    @ApiOperation(value = "获取项目名称")
    @GetMapping(value = "/getProjectName")
    @SneakyThrows(CommonRuntimeException.class)
    public ResponseMsg<String> getProjectName() {
        return MsgUtils.buildSuccessMsg(projectName);
    }

    /**
     * 查询全部服务
     * @author 溪云阁
     * @param serviceName
     * @return ResponseMsg<List<Instance>>
     */
    @ApiOperation(value = "查询全部服务")
    @GetMapping(value = "/getAllServices")
    @SneakyThrows({ CommonRuntimeException.class, NacosException.class })
    public ResponseMsg<List<Instance>> getAllServices(@RequestParam("serviceName") String serviceName) {
        return MsgUtils.buildSuccessMsg(namingService.getAllInstances(serviceName));
    }

}

测试

此时若是你在Nacos上更改配置上面的属性,发布后从新获取数据,也会跟着变动。java

拓展

一、生产中,要把作高可用程序员

好文章,我在看springboot

本文分享自微信公众号 - 程序员闪充宝(cxyscb1024)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。微信

相关文章
相关标签/搜索