如何制作企业网站的版式,京东网站的公司全名,中信建设有限责任公司云南分公司,程序员做笔记的网站一、概述 首先#xff0c;nacos是一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。而springboot能够创建一个独立运行spring应用框架#xff0c;因此#xff0c;springboot整合nacos#xff0c;可以更方便实现服务的发现、配置管理等相关功能。
二、spr…一、概述 首先nacos是一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。而springboot能够创建一个独立运行spring应用框架因此springboot整合nacos可以更方便实现服务的发现、配置管理等相关功能。
二、springboot是如何整合nacos? 1.在pom文件中添加nacos依赖。
dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactIdversion2021.0.5.0/version/dependencydependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-config/artifactIdversion2021.0.5.0/version/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-bootstrap/artifactIdversion3.1.3/version/dependency注这里用的spring版本是2.6.0。
2.配置nacos。 在bootstrap.properties或bootstrap.yml文件中配置nacos的相关信息。这里以bootstrap.yml为例。
spring:application:name: my-blog#为服务名cloud:nacos:username: nacospassword: nacosdiscovery:namespace: e50bbc31-d4bd-4f7f-852a-c5cacf3a2a24server-addr: localhost:8848register-enabled: truewatch:enabled: trueconfig:extension-configs:- data-id: common.propertiesrefresh: truefile-extension: properties #后缀名只支持 properties 和 yaml 类型prefix: my-blog #文件名如果没有配置则默认为 ${spring.appliction.name}namespace: e50bbc31-d4bd-4f7f-852a-c5cacf3a2a24auto-refresh: trueenable-remote-sync-config: true #启用远程同步配置group: DEFAULT_GROUP #配置组refresh-enabled: trueserver-addr: localhost:88483.使用nacos配置。 我们可以在SpringBoot应用中通过Value注解或在application.yml中你可以通过占位符${…}来引用nacos中的配置。
Component
ConfigurationProperties(prefix blog)
public class MyConfigProperties { private String property;
}
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;Component
Data
RefreshScope
public class MyConfig {/*** url地址*/Value(${blog.url})private String url;/*** 路径*/Value(${blog.path})private String path;
}其中 RefreshScope注解实现配置动态刷新。
4.启动nacos服务发现。
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;SpringBootApplication
EnableDiscoveryClient
public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); }
}
接着启动应用后就可以在nacos的服务列表中看到我们已经注册到nacos的服务。