• SpringAlibaba的Nacos


    Nacos简介

    为什么叫Nacos

    前四个字母分别为Naming和Configuration的前两个字母,最后的s为Service

    是什么

    1.是一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。

    2.Nacos:Dynamic Naming and Configuration Service

    3.Nacos 就是注册中心 + 配置中心的组合。也就是说Nacos等价于 Eureka + Config + Bus

    能干什么

    1.可以取代Eureka做服务中心。

    2.可以取代Config做配置中心。

    去哪里学习、下载?

    GitHub:https://github.com/alibaba/Nacos

    中文版:https://nacos.io/zh-cn/

    由于github上下载很慢,所以分享个nacos-server-1.2.1链接给大家:

    链接:https://pan.baidu.com/s/112gp5pgDlcSM0Br6ByeymQ
    提取码:b8l1

    启动运行

    解压文件,cmd进入bin目录并运行startup.cmd出现下图,即可

    C:D	ools
    acosin>startup.cmd
    
             ,--.
           ,--.'|
       ,--,:  : |                                           Nacos 1.2.1
    ,`--.'`|  ' :                       ,---.               Running in stand alone mode, All function modules
    |   :  :  | |                      '   ,'   .--.--.    Port: 8848
    :   |    | :  ,--.--.     ,---.  /   /   | /  /    '   Pid: 7520
    |   : '  '; | /          /     .   ; ,. :|  :  /`./   Console: http://192.168.100.184:8848/nacos/index.html
    '   ' ;.    ;.--.  .-. | /    / ''   | |: :|  :  ;_
    |   | |    | \__/: . ..    ' / '   | .; :       `.      https://nacos.io
    '   : |  ; .' ," .--.; |'   ; :__|   :    |  `----.   
    |   | '`--'  /  /  ,.  |'   | '.'|     /  /  /`--'  /
    '   : |     ;  :   .'      :    : `----'  '--'.     /
    ;   |.'     |  ,     .-./     /            `--'---'
    '---'        `--`---'     `----'

    然后访问直接访问http://localhost:8848/nacos 账号密码 nacos,如果能顺利访问,那么证明nacos启动成功。

    创建实例

    父pom

      1   <!--统一管理jar包版本-->
      2   <properties>
      3     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      4     <maven.compiler.source>1.8</maven.compiler.source>
      5     <maven.compiler.target>1.8</maven.compiler.target>
      6     <junit.version>4.12</junit.version>
      7     <log4j.version>1.2.17</log4j.version>
      8     <lombok.version>1.16.18</lombok.version>
      9     <mysql.version>5.1.47</mysql.version>
     10     <druid.version>1.1.16</druid.version>
     11     <mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
     12   </properties>
     13 
     14 
     15   <!--子模块继承之后,提供作用:锁定版本+子modlue不用谢groupId和version-->
     16   <dependencyManagement>
     17     <dependencies>
     18       <!--springboot 2.2.7-->
     19       <dependency>
     20         <groupId>org.springframework.boot</groupId>
     21         <artifactId>spring-boot-dependencies</artifactId>
     22         <version>2.2.7.RELEASE</version>
     23         <type>pom</type>
     24         <scope>import</scope>
     25       </dependency>
     26       <!--spring cloud Hoxton.SR5-->
     27       <dependency>
     28         <groupId>org.springframework.cloud</groupId>
     29         <artifactId>spring-cloud-dependencies</artifactId>
     30         <version>Hoxton.SR5</version>
     31         <type>pom</type>
     32         <scope>import</scope>
     33       </dependency>
     34       <!--spring cloud alibaba 2.1.0.RELEASE-->
     35       <dependency>
     36         <groupId>com.alibaba.cloud</groupId>
     37         <artifactId>spring-cloud-alibaba-dependencies</artifactId>
     38         <version>2.1.0.RELEASE</version>
     39         <type>pom</type>
     40         <scope>import</scope>
     41       </dependency>
     42       <!--mysql-->
     43       <dependency>
     44         <groupId>mysql</groupId>
     45         <artifactId>mysql-connector-java</artifactId>
     46         <version>${mysql.version}</version>
     47       </dependency>
     48       <!--druid-->
     49       <dependency>
     50         <groupId>com.alibaba</groupId>
     51         <artifactId>druid</artifactId>
     52         <version>${druid.version}</version>
     53       </dependency>
     54       <!--mybatis-->
     55       <dependency>
     56         <groupId>org.mybatis.spring.boot</groupId>
     57         <artifactId>mybatis-spring-boot-starter</artifactId>
     58         <version>${mybatis.spring.boot.version}</version>
     59       </dependency>
     60       <dependency>
     61         <groupId>org.projectlombok</groupId>
     62         <artifactId>lombok</artifactId>
     63       </dependency>
     64     </dependencies>
     65   </dependencyManagement>
     66 
     67   <build>
     68     <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
     69       <plugins>
     70         <plugin>
     71           <artifactId>maven-clean-plugin</artifactId>
     72           <version>3.1.0</version>
     73         </plugin>
     74         <plugin>
     75           <artifactId>maven-site-plugin</artifactId>
     76           <version>3.7.1</version>
     77         </plugin>
     78         <plugin>
     79           <artifactId>maven-project-info-reports-plugin</artifactId>
     80           <version>3.0.0</version>
     81         </plugin>
     82       </plugins>
     83     </pluginManagement>
     84     <plugins>
     85       <plugin>
     86         <groupId>org.apache.maven.plugins</groupId>
     87         <artifactId>maven-site-plugin</artifactId>
     88         <configuration>
     89           <locales>en,fr</locales>
     90         </configuration>
     91       </plugin>
     92       <!--热启动插件-->
     93       <plugin>
     94         <groupId>org.springframework.boot</groupId>
     95         <artifactId>spring-boot-maven-plugin</artifactId>
     96         <configuration>
     97           <fork>true</fork>
     98           <addResources>true</addResources>
     99         </configuration>
    100       </plugin>
    101     </plugins>
    102   </build>
    103   <repositories>
    104     <repository>
    105       <id>nexus-aliyun</id>
    106       <name>Nexus aliyun</name>
    107       <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    108       <releases>
    109         <enabled>true</enabled>
    110       </releases>
    111       <snapshots>
    112         <enabled>false</enabled>
    113       </snapshots>
    114     </repository>
    115   </repositories>
    View Code

    新建module模块cloudalibaba-provider-payment9001

    POM

    <dependencies>
            <!--springcloud alibaba nacos-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            </dependency> 
            <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>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.12</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>

    application.yml

    server:
      port: 9001
    spring:
      application:
        name: nacos-provider
      cloud:
        nacos:
          discovery:
            server-addr: 127.0.0.1:8848
    management:
      endpoints:
        web:
          exposure:
            include: '*'

    主启动类 ,重点关注@EnableDiscoveryClient

    package com.seegot.springcloud;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    /**
     * @program: cloud2020
     * @description:
     * @author: PP Zhang
     * @create: 2020-07-01 14:04
     */
    @SpringBootApplication
    @EnableDiscoveryClient
    public class NacosProviderMain9002 {
        public static void main(String[] args) {
            SpringApplication.run(NacosProviderMain9002.class,args);
        }
    }

    业务类

     1 package com.seegot.springcloud.controller;
     2 
     3 import lombok.extern.slf4j.Slf4j;
     4 import org.springframework.beans.factory.annotation.Value;
     5 import org.springframework.web.bind.annotation.GetMapping;
     6 import org.springframework.web.bind.annotation.PathVariable;
     7 import org.springframework.web.bind.annotation.RestController;
     8 
     9 /**
    10  * @program: cloud2020
    11  * @description:
    12  * @author: PP Zhang
    13  * @create: 2020-07-01 14:05
    14  */
    15 @RestController
    16 @Slf4j
    17 public class ProviderController {
    18     @Value("${server.port}")
    19     private  String serverPort;
    20     @GetMapping(value = "/echo/{name}")
    21     public String echo(@PathVariable("name") String name){
    22         return "Hello Nacos discovery,welcome  " + name+",serverPort:"+serverPort;
    23     }
    24 }

    启动测试

    http://localhost:8848/nacos/#/serviceManagement?dataId=&group=&appName=&serverId=&namespace=

    服务注册成功。

  • 相关阅读:
    js中for..of..和迭代器
    Python与其他语言的区别
    数据结构和算法部分总结
    MVC设计模式及SSH框架的介绍
    块级元素和行内元素以及display中block、inline和inline-block的区别
    线程安全和非线程安全
    Spring MVC拦截器入门
    Java反射的理解
    mybatis
    重定向和转发
  • 原文地址:https://www.cnblogs.com/pengpengzhang/p/13219086.html
Copyright © 2020-2023  润新知