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上更改配置上面的属性,发布后重新获取数据,也会跟着变更。

拓展

1、生产中,要把做高可用


好文章,我在看

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值