简介 事实上Nacos就是一个springboot的web项目,它提供了注册和配置的相关核心功能,然后对外暴露了各种接口;我相信做过javaweb开发的同学都知道,怎么对外提供接口呢?无非就是通过controller层暴露一些RequestMapping嘛,这点,咱们可以通过源码去验证。 nacos具体暴露哪些接口调用可以参考:Open API 指南 。
到这一步,我们大概明白了nacos的架构了,但是有一个问题,虽然nacos提供了各种接口,但是我们要使用它的话,不能我们自己去封装各种http调用工具去调用它的接口吧?如果是要我们自己通过业务代码去封装工具的话,那nacos的价值也不会很高,所以,为了让我们使用起来更加方便,nacos的设计者肯定是要帮我们做一些事情的,这个时候nacos就分为了两个模块:nacos-server(主要提供注册和配置的核心功能)和nacos-client端(这里面实际上就是封装了http调用的一些工具,让使用者不需要自己去封装,只要调用方法就好了)。
nacos-client封装了哪些方法可以参考:Java SDK 。
到这一步,事实上还是不够,我们一起来想想,虽然nacos-client端给我们封装了工具,但是我业务代码还是得去手动调用这些方法,还是对我的业务代码有侵入性,这样的话还是不够简洁。这个时候怎么办呢?我们会想到springboot的自动装配,通过springboot去帮我们完成对象的实例化,然后在适当的时机自动帮我们调用核心api。
这个时候好事之人帮我们对nacos-client包又进行了封装,有注册的jar包:nacos-discovery-spring-boot-starter和配置的jar包:nacos-config-spring-boot-starter,但是事实上这两个jar包功能还不够强大,因为无法做到自动注册,而且动态刷新配置的时候需要加一些注解,那好事之人又出现了,对nacos-client做了更进一步封装:spring-cloud-starter-alibaba-nacos-discovery和spring-cloud-starter-alibaba-nacos-config
封装示例 spring-cloud-starter-alibaba-nacos-config
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 <?xml version="1.0" encoding="UTF-8"?> <project xsi:schemaLocation ="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns ="http://maven.apache.org/POM/4.0.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" > <modelVersion > 4.0.0</modelVersion > <parent > <groupId > com.alibaba.cloud</groupId > <artifactId > spring-cloud-alibaba-starters</artifactId > <version > 2.2.7.RELEASE</version > </parent > <groupId > com.alibaba.cloud</groupId > <artifactId > spring-cloud-starter-alibaba-nacos-config</artifactId > <version > 2.2.7.RELEASE</version > <name > Spring Cloud Starter Alibaba Nacos Config</name > <description > Spring Cloud Alibaba Starters</description > <url > https://github.com/alibaba/spring-cloud-alibaba/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config</url > <organization > <name > Pivotal Software, Inc.</name > <url > https://www.spring.io</url > </organization > <licenses > <license > <name > Apache License, Version 2.0</name > <url > https://www.apache.org/licenses/LICENSE-2.0.txt</url > <distribution > repo</distribution > </license > </licenses > <developers > <developer > <name > xiaojing</name > <email > flystar32@163.com</email > </developer > <developer > <name > Jim Fang</name > <email > fangjian0423@gmail.com</email > <url > https://github.com/fangjian0423</url > <organization > Alibaba</organization > </developer > <developer > <name > xiaolongzuo</name > <email > 150349407@qq.com</email > </developer > <developer > <name > hengyunabc</name > <email > hengyunabc@gmail.com</email > </developer > <developer > <id > mercyblitz</id > <name > Mercy Ma</name > <email > mercyblitz@gmail.com</email > <url > https://github.com/mercyblitz</url > <organization > Alibaba</organization > </developer > <developer > <name > yunzheng</name > <email > yunzheng1228@gmail.com</email > </developer > <developer > <id > theonefx</id > <name > theonefx</name > <email > chenxilzx1@gmail.com</email > <url > https://github.com/theonefx</url > <organization > Alibaba</organization > </developer > </developers > <scm > <connection > scm:git:git://github.com/alibaba/spring-cloud-alibaba.git/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config</connection > <developerConnection > scm:git:ssh://git@github.com/alibaba/spring-cloud-alibaba.git/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config</developerConnection > <url > https://github.com/alibaba/spring-cloud-alibaba/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config</url > </scm > <dependencies > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-actuator</artifactId > <version > 2.3.12.RELEASE</version > <scope > compile</scope > <optional > true</optional > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-actuator-autoconfigure</artifactId > <version > 2.3.12.RELEASE</version > <scope > compile</scope > <optional > true</optional > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-configuration-processor</artifactId > <version > 2.3.12.RELEASE</version > <scope > compile</scope > <optional > true</optional > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot</artifactId > <version > 2.3.12.RELEASE</version > <scope > compile</scope > <optional > true</optional > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-autoconfigure</artifactId > <version > 2.3.12.RELEASE</version > <scope > compile</scope > <optional > true</optional > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter</artifactId > <version > 2.3.12.RELEASE</version > <scope > compile</scope > <optional > true</optional > </dependency > <dependency > <groupId > com.alibaba.spring</groupId > <artifactId > spring-context-support</artifactId > <version > 1.0.10</version > <scope > compile</scope > </dependency > <dependency > <groupId > com.alibaba.nacos</groupId > <artifactId > nacos-client</artifactId > <version > 2.0.3</version > <scope > compile</scope > </dependency > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-commons</artifactId > <version > 2.2.9.RELEASE</version > <scope > compile</scope > </dependency > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-context</artifactId > <version > 2.2.9.RELEASE</version > <scope > compile</scope > </dependency > </dependencies > </project >
spring-cloud-starter-alibaba-nacos-discovery
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 <?xml version="1.0" encoding="UTF-8"?> <project xsi:schemaLocation ="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns ="http://maven.apache.org/POM/4.0.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" > <modelVersion > 4.0.0</modelVersion > <parent > <groupId > com.alibaba.cloud</groupId > <artifactId > spring-cloud-alibaba-starters</artifactId > <version > 2.2.7.RELEASE</version > </parent > <groupId > com.alibaba.cloud</groupId > <artifactId > spring-cloud-starter-alibaba-nacos-discovery</artifactId > <version > 2.2.7.RELEASE</version > <name > Spring Cloud Starter Alibaba Nacos Discovery</name > <description > Spring Cloud Alibaba Starters</description > <url > https://github.com/alibaba/spring-cloud-alibaba/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery</url > <organization > <name > Pivotal Software, Inc.</name > <url > https://www.spring.io</url > </organization > <licenses > <license > <name > Apache License, Version 2.0</name > <url > https://www.apache.org/licenses/LICENSE-2.0.txt</url > <distribution > repo</distribution > </license > </licenses > <developers > <developer > <name > xiaojing</name > <email > flystar32@163.com</email > </developer > <developer > <name > Jim Fang</name > <email > fangjian0423@gmail.com</email > <url > https://github.com/fangjian0423</url > <organization > Alibaba</organization > </developer > <developer > <name > xiaolongzuo</name > <email > 150349407@qq.com</email > </developer > <developer > <name > hengyunabc</name > <email > hengyunabc@gmail.com</email > </developer > <developer > <id > mercyblitz</id > <name > Mercy Ma</name > <email > mercyblitz@gmail.com</email > <url > https://github.com/mercyblitz</url > <organization > Alibaba</organization > </developer > <developer > <name > yunzheng</name > <email > yunzheng1228@gmail.com</email > </developer > <developer > <id > theonefx</id > <name > theonefx</name > <email > chenxilzx1@gmail.com</email > <url > https://github.com/theonefx</url > <organization > Alibaba</organization > </developer > </developers > <scm > <connection > scm:git:git://github.com/alibaba/spring-cloud-alibaba.git/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery</connection > <developerConnection > scm:git:ssh://git@github.com/alibaba/spring-cloud-alibaba.git/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery</developerConnection > <url > https://github.com/alibaba/spring-cloud-alibaba/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-discovery</url > </scm > <dependencies > <dependency > <groupId > com.alibaba.cloud</groupId > <artifactId > spring-cloud-alibaba-commons</artifactId > <version > 2.2.7.RELEASE</version > <scope > compile</scope > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-actuator</artifactId > <version > 2.3.12.RELEASE</version > <scope > compile</scope > <optional > true</optional > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-actuator-autoconfigure</artifactId > <version > 2.3.12.RELEASE</version > <scope > compile</scope > <optional > true</optional > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-configuration-processor</artifactId > <version > 2.3.12.RELEASE</version > <scope > compile</scope > <optional > true</optional > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot</artifactId > <version > 2.3.12.RELEASE</version > <scope > compile</scope > <optional > true</optional > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-autoconfigure</artifactId > <version > 2.3.12.RELEASE</version > <scope > compile</scope > <optional > true</optional > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter</artifactId > <version > 2.3.12.RELEASE</version > <scope > compile</scope > <optional > true</optional > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-webflux</artifactId > <version > 2.3.12.RELEASE</version > <scope > compile</scope > <optional > true</optional > </dependency > <dependency > <groupId > com.alibaba.nacos</groupId > <artifactId > nacos-client</artifactId > <version > 2.0.3</version > <scope > compile</scope > </dependency > <dependency > <groupId > com.alibaba.spring</groupId > <artifactId > spring-context-support</artifactId > <version > 1.0.10</version > <scope > compile</scope > </dependency > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-commons</artifactId > <version > 2.2.9.RELEASE</version > <scope > compile</scope > </dependency > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-context</artifactId > <version > 2.2.9.RELEASE</version > <scope > compile</scope > </dependency > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-netflix-ribbon</artifactId > <version > 2.2.9.RELEASE</version > <scope > compile</scope > </dependency > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-config-client</artifactId > <version > 2.2.8.RELEASE</version > <scope > compile</scope > <optional > true</optional > </dependency > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-config-server</artifactId > <version > 2.2.8.RELEASE</version > <scope > compile</scope > <optional > true</optional > </dependency > </dependencies > </project >