首页 > 其他分享 >springboot整合nacos注册中心和配置中心

springboot整合nacos注册中心和配置中心

时间:2024-03-08 14:45:39浏览次数:21  
标签:springboot 中心 spring nacos springframework alibaba org cloud

我的命名空间是centos

效果图

 

 

 

可以是yml格式,名称不要变

springboot版本2.1.6

pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ip</groupId>
    <artifactId>nacos-ip</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
        <skipTests>true</skipTests>
        <springcloud.version>Greenwich.RELEASE</springcloud.version>
        <alibaba.version>2.1.0.RELEASE</alibaba.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!--spring cloud依赖管理,引入了Spring Cloud的版本-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${springcloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>


            <!--SCA -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--SCA -->
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- Spring Boot Starter Web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

  配置文件application.properties

server.port=8081

#nacos注册
spring.application.name=nacos-service
spring.cloud.nacos.discovery.server-addr=192.168.31.50:8850
spring.cloud.nacos.discovery.namespace=7dd82e29-1903-4e70-b59b-40b9a59ebd3e
management.endpoints.web.exposure.include=*

# 设置Nacos客户端日志级别为DEBUG
logging.level.com.alibaba.nacos=DEBUG



logging.level.com.ip=debug
logging.level.web=debug

  配置文件bootstrap.properties

server.port=8081

#nacos配置
#2024.3.1 13:56 add
spring.cloud.nacos.config.server-addr=192.168.31.50:8850
spring.cloud.nacos.config.namespace=7dd82e29-1903-4e70-b59b-40b9a59ebd3e
spring.cloud.nacos.config.group=DEFAULT_GROUP
spring.cloud.nacos.config.file-extension=properties

# 设置Nacos客户端日志级别为DEBUG
logging.level.com.alibaba.nacos=DEBUG



logging.level.com.ip=debug
logging.level.web=debug

  代码部分

package com.ip;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @author yourheart
 * @Description
 * @create 2021-06-06 10:34
 */
@SpringBootApplication
@EnableDiscoveryClient
public class CloudPingAnApplication {
    public static void main(String[] args) {
        SpringApplication.run(CloudPingAnApplication.class,args);
    }
}

  

package com.ip.controller.front;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author yourheart
 * @Description
 * @create 2021-06-06 10:59
 */
@Controller
@RequestMapping("/queryIp")
@RefreshScope
public class QueryIpController {


    @Value("${test}")
    private String test;

    /**
     * 根据ip查询ip信息
     * @param ip
     * @return
     */
    @RequestMapping("/queryIpCity/{ip}")
    @ResponseBody
    public String queryIpCity(@PathVariable String ip){
        System.out.println("test="+test);
        return "success";
    }
}

  注册中心配置关联的版本

springboot版本2.6.13

完整pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ip</groupId>
    <artifactId>nacos-ip</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.13</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
        <skipTests>true</skipTests>
        <springcloud.version>2021.0.5</springcloud.version>
        <alibaba.version>2021.0.5.0</alibaba.version>
    </properties>



    <dependencyManagement>
        <dependencies>
            <!--spring cloud依赖管理,引入了Spring Cloud的版本-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${springcloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>


            <!--SCA -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--SCA -->
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- Spring Boot Starter Web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

  配置文件application.properties

server.port=8081

#nacos注册
spring.application.name=nacos-service
spring.cloud.nacos.discovery.server-addr=192.168.31.50:8850
spring.cloud.nacos.discovery.namespace=7dd82e29-1903-4e70-b59b-40b9a59ebd3e
management.endpoints.web.exposure.include=*

# 设置Nacos客户端日志级别为DEBUG
logging.level.com.alibaba.nacos=DEBUG

logging.level.com.ip=debug
logging.level.web=debug

  配置文件bootstrap.properties

server.port=8081

#nacos配置
#2024.3.1 13:56 add
spring.cloud.nacos.config.server-addr=192.168.31.50:8850
spring.cloud.nacos.config.namespace=7dd82e29-1903-4e70-b59b-40b9a59ebd3e
spring.cloud.nacos.config.group=DEFAULT_GROUP
spring.cloud.nacos.config.file-extension=properties

# 设置Nacos客户端日志级别为DEBUG
logging.level.com.alibaba.nacos=DEBUG



logging.level.com.ip=debug
logging.level.web=debug

  

请记得在bootstrap.properties文件中新增配置

spring.cloud.nacos.config.refreshable-dataids=nacos-service.properties,true

 

 后面又找了找,找到了没过时的配置

三者搭配

# 配置支持共享的 Data Id
spring.cloud.nacos.config.shared-configs[0].data-id=nacos-service.properties

# 配置 Data Id 所在分组,缺省默认 DEFAULT_GROUP
spring.cloud.nacos.config.shared-configs[0].group=DEFAULT_GROUP

# 配置Data Id 在配置变更时,是否动态刷新,缺省默认 false
spring.cloud.nacos.config.shared-configs[0].refresh=true

  

 环境隔离

如果之前使用的是eureka,

使用

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
替换
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>就可以了
注解使用@EnableDiscoveryClient

 

 消费者springboot使用2.4.2版本时,pom依赖加入

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>

  消费者springboot使用2.6.13版本,和上面引入一样的依赖

不引入报错如下

 好了关于nacos注册中心和配置中心使用就暂时到这里了

 

标签:springboot,中心,spring,nacos,springframework,alibaba,org,cloud
From: https://www.cnblogs.com/q202105271618/p/18059115

相关文章

  • docker安装nacos
    如下,目录配置自行创建、调整dockerrun--namenacos-2.1.0\-p8848:8848\-p9848:9848\-p9849:9849\--privileged=true\--restart=always\-eJVM_XMS=256m\-eJVM_XMX=256m\-eMODE=standalone\-eTIME_ZONE='Asia/Shanghai'\-ePREFER_HOST_......
  • nacos成功注册却报异常(http://localhost:8848)
    报错日志:[NACOSHTTP-POST]Themaximumnumberoftolerableserverreconnectionerrorshasbeenreached[fixed-localhost_8848][check-update]getchangeddataIdexception[NACOSConnectExceptionhttpPost]currentServerAddr:http://localhost:8848,err:Connec......
  • dolphinscheduler master实现去中心化源码分析
    dolphinschedulerMaster服务是去中心化的,也就是没有master和slave之分,每个master都参与工作,那么它是如何每个Master服务去取任务执行时,每个Master都取到不同的任务,并且不会漏掉,不会重复的呢,下面从源码角度来分析这个问题MasterServer.java/***runmasterserver*/@PostC......
  • .net8集成Apollo配置中心步骤
    部署Apollo我这里按照官方文档使用docker快速部署了一个测试学习用。 创建应用 新增配置创建.net8版本的webapi项目 修改appsettings.json{"Apollo":{"AppId":"ApolloTest001","MetaServer":"http://127.0.0.1:8080"}}AppId是应用的身......
  • SpringBoot 支付宝付款接口类、支付异步回调函数模板
    1.付款接口类1.1.引入Maven依赖<dependency><groupId>com.alipay.sdk</groupId><artifactId>alipay-sdk-java</artifactId><version>4.38.221.ALL</version></dependency>1.2.将下面代码保存为AlipayTemplate.java@Config......
  • SpringBoot-重要注解(1)
    ConfigurationProperties注解https://blog.csdn.net/skh2015java/article/details/120141409@Import、@ImportAutoConfigurationhttps://www.cnblogs.com/imyjy/p/16092825.html当我们需要在一个配置类中导入另一个Configuration配置类时,可以使用@Import、@ImportAuto......
  • SpringBoot3+Consul配置,启动后,居然不读bootstrap.yml的配置文件,直接连本地localhost:8
    问题描述如题。bootstrap.yml的配置文件: consul控制台打印的日志: 解决方案:booststrap.yml的配置文件缩进搞错了,所以压根就没有读到配置。正确的缩进:  ......
  • 若依集成CIM(即时推送系统)实现将服务端修改为SpringBoot+Vue前后端分离版(文末见代码
    ​ 场景若依前后端分离版本地搭建开发环境并运行项目的教程:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/108465662 CIMGitee地址:https://gitee.com/farsunset/cimCIM项目是基于mina或者netty框架下的推送系统,我们平常使用第三方的推送SDK,如极光推送,百度......
  • centos安装单机版nacos
    效果图如下nacos其实有两个作用一个作为注册中心,二是作为配置中心系统更新sudoyumupdate安装jdksudoyuminstalljava-1.8.0-openjdk-devel nacos是在github上的,所以提前下载好了国内下载wgethttps://yourheart.live/upload/2024/02/6eiajdgnf2hk5......
  • SpringBoot使用外部Web容器的解决方案
    SpringBoot默认内嵌了Web容器(如Tomcat、Jetty或Undertow),这使得应用可以作为独立的可执行JAR或WAR文件运行,无需外部Web容器。然而,在某些情况下,你可能想要将SpringBoot应用部署到外部的Web容器中,比如ApacheTomcat或Jetty。嵌入式的Web容器:应用可以打包成可执行的Jar。优点:简单......