Spring Cloud Alibaba 参考文档
Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。 是Spring Cloud A 中的服务注册发现组件,类似于Consul、Eureka,同时它又提供了分布式配置中心的功能,这点和Consul的config类似,支持热加载。
1.1、Nacos 的关键特性
- 服务发现和服务健康监测
- 动态配置服务,带管理界面,支持丰富的配置维度。
- 动态 DNS 服务
- 服务及其元数据管理
1.2、Nacos下载和安装
下载地址:https://github.com/alibaba/nacos/releases
下载完成后,解压,在解压后的文件的/bin目录下,windows系统点击startup.cmd就可以启动nacos。linux或mac执行以下命令启动nacos:
sh startup.sh -m standalone
启动成功,在浏览器上访问:http://localhost:8848/nacos,会跳转到登陆界面,默认的登陆用户名为nacos,密码也为nacos。
登陆成功后,展示的界面如下:
2、使用Nacos服务注册和发现
2.1、 Idea-New Project-Maven
一直Next-Finish,然后删除不必要的src,且添加:
<packaging>pom</packaging>
2.2、选中项目New——Model-Spring Initializr
然后删除不必要的src。
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.steven</groupId><artifactId>springcloudalibaba</artifactId><version>1.0-SNAPSHOT</version></parent><groupId>com.steven</groupId><artifactId>nacos-server</artifactId><version>0.0.1-SNAPSHOT</version><name>nacos-server</name><description>spring nacos server</description><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><exclusions><exclusion><artifactId>guava</artifactId><groupId>com.google.guava</groupId></exclusion></exclusions></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
再在外层的父pom.xml添加:
<modules><module>nacos-server</module></modules>
2.3、 父pom.xml添加相关配置
父pom.xml:
<?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.steven</groupId><artifactId>springcloudalibaba</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><modules><module>nacos-server</module></modules><properties><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>8</java.version><spring-boot-dependencies.version>2.1.12.RELEASE</spring-boot-dependencies.version><spring-cloud-dependencies.version>Greenwich.SR5</spring-cloud-dependencies.version><spring-cloud-alibaba-dependencies.version>2.1.1.RELEASE</spring-cloud-alibaba-dependencies.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot-dependencies.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud-dependencies.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${spring-cloud-alibaba-dependencies.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><pluginManagement><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>${spring-boot-dependencies.version}</version></plugin></plugins></pluginManagement></build>
</project>
2.4、在工程的配置文件application.yml做相关的配置,配置如下
server:port: 8090
spring:application:name: nacos-providercloud:nacos:discovery:server-addr: 127.0.0.1:8848
2.5、然后在Spring Boot的启动文件NacosProviderApplication加上@EnableDiscoveryClient注解:
@EnableDiscoveryClient
@SpringBootApplication
public class NacosProviderApplication {public static void main(String[] args) {SpringApplication.run(NacosProviderApplication.class, args);}}