赞
踩
一、Nacos简介
Nacos核心提供两个功能:服务注册与发现,动态配置管理。
1、服务注册与发现
Nacos提供基于DNS和基于RPC的服务发现,即能被用来支持https/http的服务注册与发现,也支持RPC如dubbo的服务注册与发现。
与Dubbo使用的zookeeper相比而言,两者差异还是比较大的,zookeeper是一种分布式的协调服务,它天生是作为分布式数据一致性场景下的解决方案,所以zookeeper是CP的,它牺牲了可用性来保证一致性,在极端情况下(master选举期间)服务会对外停止,对于服务可用性要求比较高的系统是难以接受的。Nacos是一种去中心化的架构,属于CAP理论里的AP架构,支持最终一致性,在分布式服务发现与注册场景下具有很不错的性能。目前dubbo官方也支持使用Nacos代替zookeeper。
2、动态配置服务
动态修改配置并实时生效对于服务端的同学而已并不陌生,这种服务能够让我们的服务拥有更多的灵活性,不需要重启服务即可做到配置实时生效,非常适合于“配置优先”的服务开发。
二、Nacos安装(windows)
1.下载安装
下载地址:https://github.com/alibaba/nacos/releases/tag/1.1.0
选择:nacos-server-1.1.0.zip或者nacos-server-1.1.0.tar.gz 下载;下载完成解压。解压文件中的bin文件夹中有个startup.cmd文件,双击该文件就可以启动。启动成功后会弹出一个cmd窗口。启动失败则不会弹出。
2.访问
访问地址:http://localhost:8848/nacos/index.html 用户名密码:nacos/nacos
windows7、JDK8、nacos-server-1.1.0、springboot-2.1.9.RELEASE、springcloud-Greenwich.SR3、spring-cloud-alibaba-dependencie-0.9.0.RELEASE
创建一个聚合工程SpringCloudNacos作为父工程,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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <!-- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.2.4.RELEASE</version>
- <relativePath/> <!– lookup parent from repository –>
- </parent>-->
- <groupId>com.learning.nacos</groupId>
- <artifactId>springcloudnacos</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>springcloudnacos</name>
- <description>Demo project for Spring Boot</description>
- <!--子工程 子工程创建好之后取消注释
- <modules>
- <module>nacos-provider</module>
- <module>nacos-consumers</module>
- </modules>-->
- <properties>
- <java.version>1.8</java.version>
- <spring-boot.version>2.1.9.RELEASE</spring-boot.version>
- <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
- <nacos.version>0.9.0.RELEASE</nacos.version>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <version>1.18.2</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
- <dependency>
- <groupId>org.junit.jupiter</groupId>
- <artifactId>junit-jupiter-api</artifactId>
- <version>5.5.2</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
-
- <!-- 依赖版本号管理 重点在这里 -->
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>${spring-cloud.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-dependencies</artifactId>
- <version>${spring-boot.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-alibaba-dependencies</artifactId>
- <version>${nacos.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
-
- </project>
在父工程springcloudnacos下创建子工程nacos-provider(服务提供者)。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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>com.learning.nacos</groupId>
- <artifactId>springcloudnacos</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </parent>
- <groupId>com.sprincloud.nacos</groupId>
- <artifactId>nacos-provider</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>nacos-provider</name>
- <description>Demo project for Spring Boot</description>
-
- <properties>
- <java.version>1.8</java.version>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-discovery -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
- <version>0.9.0.RELEASE</version>
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
-
- </project>
在启动类NacosProviderApplication中提供一个对外接口,并添加@EnableDiscoveryClient注解,开启注册发现功能。
- package com.sprincloud.nacos.nacosprovider;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- @RestController
- @EnableDiscoveryClient
- @SpringBootApplication
- public class NacosProviderApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(NacosProviderApplication.class, args);
- }
-
- @GetMapping("/hello")
- public String helloNacos(){
- return "Hello Nacos !";
- }
- }
最好一步:application.yml配置文件:
- server:
- port: 8001
- spring:
- application:
- name: nacos-provide
- cloud:
- nacos:
- discovery:
- server-addr: 127.0.0.1:8848
在父工程springcloudnacos下创建子工程nacos-consumers(服务消费者)。pom.xml文件和nacos-provider一样。
服务消费者通过TestTemplate+Ribbon进行服务调用;NacosConsumersApplication启动类代码如下:
- package com.springcloud.nacos.nacosconsumers;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
- import org.springframework.cloud.client.loadbalancer.LoadBalanced;
- import org.springframework.context.annotation.Bean;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.client.RestTemplate;
-
- @RestController
- @EnableDiscoveryClient
- @SpringBootApplication
- public class NacosConsumersApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(NacosConsumersApplication.class, args);
- }
- @Autowired
- private RestTemplate template;
- @Bean
- @LoadBalanced
- public RestTemplate getRestTemplate(){
- return new RestTemplate();
- }
- @GetMapping("/consumer")
- public String test1() {
- return template.getForObject("http://nacos-provider/hello",String.class);
- }
- }
application.yml配置如下:
- server:
- port: 8002
- spring:
- application:
- name: nacos-consumers
- cloud:
- nacos:
- discovery:
- server-addr: 127.0.0.1:8848
先启动启动nacos,然后运行刚刚创建好的两个子工程。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。