Fork me on GitHub

nacos入门教程

一、nacos简介

Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。

Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。 Nacos 是构建以“服务”为中心的现代应用架构 (例如微服务范式、云原生范式) 的服务基础设施。

 

nacos的主要特性:

  1. 服务发现和服务健康监测

  2. 动态配置服务

  3. 动态DNS服务

  4. 服务及其元数据管理

 

二、常见的注册中心及区别:

 三、启动nacos的服务

源码包下载:

windows系统环境下以管理员运行startup.cmd批处理文件。

 

 

 启动成功,浏览器访问地址  http://192.168.171.173:8848/nacos/index.html   首次登陆可能需要登陆    账号密码为nacos

 

 

 

三、nacos注册中心入门

1.创建两个工程:服务提供方:nacos-spring-cloud-provider-example    服务调用方:nacos-spring-cloud-consumer-example

2.导入pom文件:

provider

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 
  3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5   <modelVersion>4.0.0</modelVersion>
  6 
  7   <groupId>org.example</groupId>
  8   <artifactId>nacos-spring-cloud-provider-example</artifactId>
  9   <version>1.0-SNAPSHOT</version>
 10 
 11   <name>nacos-spring-cloud-provider-example</name>
 12   <!-- FIXME change it to the project's website -->
 13   <url>http://www.example.com</url>
 14 
 15   <parent>
 16 
 17     <groupId>org.springframework.boot</groupId>
 18     <artifactId>spring-boot-starter-parent</artifactId>
 19     <version>2.1.13.RELEASE</version>
 20     <relativePath/> <!-- lookup parent from repository -->
 21   </parent>
 22 
 23   <properties>
 24     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 25     <maven.compiler.source>1.7</maven.compiler.source>
 26     <maven.compiler.target>1.7</maven.compiler.target>
 27   </properties>
 28 
 29   <dependencies>
 30 
 31     <dependency>
 32       <groupId>org.springframework.boot</groupId>
 33       <artifactId>spring-boot-starter-web</artifactId>
 34     </dependency>
 35 
 36     <dependency>
 37       <groupId>org.springframework.boot</groupId>
 38       <artifactId>spring-boot-starter-test</artifactId>
 39       <scope>test</scope>
 40     </dependency>
 41 
 42     <dependency>
 43       <groupId>com.alibaba.cloud</groupId>
 44       <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
 45       <version>2.1.2.RELEASE</version>
 46     </dependency>
 47 
 48     <dependency>
 49       <groupId>com.alibaba.cloud</groupId>
 50       <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
 51       <version>2.1.2.RELEASE</version>
 52     </dependency>
 53   </dependencies>
 54 
 55   <!--SpringCloud的依赖-->
 56   <dependencyManagement>
 57     <dependencies>
 58       <dependency>
 59         <groupId>org.springframework.cloud</groupId>
 60         <artifactId>spring-cloud-dependencies</artifactId>
 61         <version>Greenwich.SR5</version>
 62         <type>pom</type>
 63         <scope>import</scope>
 64       </dependency>
 65     </dependencies>
 66   </dependencyManagement>
 67 
 68   <build>
 69     <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
 70       <plugins>
 71         <plugin>
 72           <groupId>org.apache.maven.plugins</groupId>
 73           <artifactId>maven-compiler-plugin</artifactId>
 74           <version>3.6.0</version>
 75           <configuration>
 76             <source>1.8</source>
 77             <target>1.8</target>
 78           </configuration>
 79         </plugin>
 80         <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
 81         <plugin>
 82           <artifactId>maven-clean-plugin</artifactId>
 83           <version>3.1.0</version>
 84         </plugin>
 85         <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
 86         <plugin>
 87           <artifactId>maven-resources-plugin</artifactId>
 88           <version>3.0.2</version>
 89         </plugin>
 90         <plugin>
 91           <artifactId>maven-compiler-plugin</artifactId>
 92           <version>3.8.0</version>
 93         </plugin>
 94         <plugin>
 95           <artifactId>maven-surefire-plugin</artifactId>
 96           <version>2.22.1</version>
 97         </plugin>
 98         <plugin>
 99           <artifactId>maven-jar-plugin</artifactId>
100           <version>3.0.2</version>
101         </plugin>
102         <plugin>
103           <artifactId>maven-install-plugin</artifactId>
104           <version>2.5.2</version>
105         </plugin>
106         <plugin>
107           <artifactId>maven-deploy-plugin</artifactId>
108           <version>2.8.2</version>
109         </plugin>
110         <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
111         <plugin>
112           <artifactId>maven-site-plugin</artifactId>
113           <version>3.7.1</version>
114         </plugin>
115         <plugin>
116           <artifactId>maven-project-info-reports-plugin</artifactId>
117           <version>3.0.0</version>
118         </plugin>
119       </plugins>
120     </pluginManagement>
121   </build>
122 </project>

application.yml

server:
  port: 8070
spring:
  application:
    name: nacos-provider
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848

controller:

1 @RestController
2 public class ProviderController {
3     @GetMapping("invoke")
4     public String invoke() {
5         return LocalTime.now() + "invoke";
6     }
7 }

启动类:

1 @EnableDiscoveryClient
2 @SpringBootApplication
3 public class ProviderApp
4 {
5     public static void main( String[] args )
6     {
7         SpringApplication.run(ProviderApp.class,args);
8     }
9 }

 


 

consumer

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 
  3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5   <modelVersion>4.0.0</modelVersion>
  6 
  7   <groupId>org.example</groupId>
  8   <artifactId>nacos-spring-cloud-consumer-example</artifactId>
  9   <version>1.0-SNAPSHOT</version>
 10 
 11   <name>nacos-spring-cloud-consumer-example</name>
 12   <!-- FIXME change it to the project's website -->
 13   <url>http://www.example.com</url>
 14 
 15   <parent>
 16     <groupId>org.springframework.boot</groupId>
 17     <artifactId>spring-boot-starter-parent</artifactId>
 18     <version>2.1.3.RELEASE</version>
 19     <relativePath/> <!-- lookup parent from repository -->
 20   </parent>
 21 
 22   <properties>
 23     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 24     <maven.compiler.source>1.7</maven.compiler.source>
 25     <maven.compiler.target>1.7</maven.compiler.target>
 26   </properties>
 27 
 28   <dependencies>
 29     <dependency>
 30       <groupId>org.springframework.boot</groupId>
 31       <artifactId>spring-boot-starter-web</artifactId>
 32     </dependency>
 33 
 34     <dependency>
 35       <groupId>org.springframework.boot</groupId>
 36       <artifactId>spring-boot-starter-test</artifactId>
 37       <scope>test</scope>
 38     </dependency>
 39 
 40     <dependency>
 41       <groupId>org.springframework.cloud</groupId>
 42       <artifactId>spring-cloud-starter-openfeign</artifactId>
 43     </dependency>
 44 
 45     <dependency>
 46       <groupId>com.alibaba.cloud</groupId>
 47       <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
 48       <version>2.1.2.RELEASE</version>
 49     </dependency>
 50   </dependencies>
 51 
 52   <!--SpringCloud的依赖-->
 53 
 54   <dependencyManagement>
 55     <dependencies>
 56       <dependency>
 57         <groupId>org.springframework.cloud</groupId>
 58         <artifactId>spring-cloud-dependencies</artifactId>
 59         <version>Greenwich.SR5</version>
 60         <type>pom</type>
 61         <scope>import</scope>
 62       </dependency>
 63     </dependencies>
 64   </dependencyManagement>
 65 
 66   <build>
 67     <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
 68       <plugins>
 69         <plugin>
 70           <groupId>org.apache.maven.plugins</groupId>
 71           <artifactId>maven-compiler-plugin</artifactId>
 72           <version>3.6.0</version>
 73           <configuration>
 74             <source>1.8</source>
 75             <target>1.8</target>
 76           </configuration>
 77         </plugin>
 78         <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
 79         <plugin>
 80           <artifactId>maven-clean-plugin</artifactId>
 81           <version>3.1.0</version>
 82         </plugin>
 83         <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
 84         <plugin>
 85           <artifactId>maven-resources-plugin</artifactId>
 86           <version>3.0.2</version>
 87         </plugin>
 88         <plugin>
 89           <artifactId>maven-compiler-plugin</artifactId>
 90           <version>3.8.0</version>
 91         </plugin>
 92         <plugin>
 93           <artifactId>maven-surefire-plugin</artifactId>
 94           <version>2.22.1</version>
 95         </plugin>
 96         <plugin>
 97           <artifactId>maven-jar-plugin</artifactId>
 98           <version>3.0.2</version>
 99         </plugin>
100         <plugin>
101           <artifactId>maven-install-plugin</artifactId>
102           <version>2.5.2</version>
103         </plugin>
104         <plugin>
105           <artifactId>maven-deploy-plugin</artifactId>
106           <version>2.8.2</version>
107         </plugin>
108         <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
109         <plugin>
110           <artifactId>maven-site-plugin</artifactId>
111           <version>3.7.1</version>
112         </plugin>
113         <plugin>
114           <artifactId>maven-project-info-reports-plugin</artifactId>
115           <version>3.0.0</version>
116         </plugin>
117       </plugins>
118     </pluginManagement>
119   </build>
120 </project>

application.yml:

1 server:
2   port: 8071
3 spring:
4   application:
5     name: nacos-consumer
6   cloud:
7     nacos:
8       config:
9         server-addr: 127.0.0.1:8848

fegin:

1 @FeignClient("nacos-provider")
2 public interface ProviderFeign {
3     @GetMapping("invoke")
4     String invoke();
5 }

controller:

 1 @RestController
 2 public class ConsummerController {
 3     @Autowired
 4     private ProviderFeign providerFeign;
 5 
 6     @GetMapping("/test")
 7     public String test() {
 8         return providerFeign.invoke();
 9     }
10 }

启动类:

 1 @EnableFeignClients
 2 @EnableDiscoveryClient
 3 @SpringBootApplication
 4 public class ConsumerApp
 5 {
 6     public static void main( String[] args )
 7     {
 8         SpringApplication.run(ConsumerApp.class,args);
 9     }
10 }

 

查看nacos服务列表是否注册:

 

 

启动服务,访问接口地址:localhost:8071/test

 

 

 

  

posted @ 2021-07-06 14:32  L波涛  阅读(1200)  评论(0编辑  收藏  举报