|
1 | 1 | # Dubbo使用Nacos作为注册中心 |
| 2 | + |
| 3 | +## Nacos是什么 |
| 4 | + |
| 5 | +官方定义是:Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。 |
| 6 | +Nacos是阿里搞出来的又一个开源项目,可以用于替代其他的注册中心,众所周知Eureka已经不再更新,目前Nacos已经支持和SpringBoot,Spring Cloud,Dubbo等集成,理论上可以实现不同微服务框架之间相互调用。 |
| 7 | + |
| 8 | +## 准备工作 |
| 9 | + |
| 10 | +开始之前需要提前安装Nacos,可以通过构建源码运行或者直接下载打包好的zip文件解压后执行,在开始下面的步骤之前,确保Nacos已经启动,如果没有安装Nacos,请参考[Nacos快速入门](https://nacos.io/en-us/docs/quick-start.html)。 |
| 11 | + |
| 12 | +## 快速开始 |
| 13 | + |
| 14 | +> 首先创建一个父子结构的maven工程,大致如下: |
| 15 | +
|
| 16 | +``` |
| 17 | +<?xml version="1.0" encoding="UTF-8"?> |
| 18 | +<project xmlns="http://maven.apache.org/POM/4.0.0" |
| 19 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 20 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| 21 | + <parent> |
| 22 | + <groupId>org.springframework.boot</groupId> |
| 23 | + <artifactId>spring-boot-starter-parent</artifactId> |
| 24 | + <version>2.1.9.RELEASE</version> |
| 25 | + <relativePath/> <!-- lookup parent from repository --> |
| 26 | + </parent> |
| 27 | +
|
| 28 | + <modelVersion>4.0.0</modelVersion> |
| 29 | +
|
| 30 | + <artifactId>dubbo-spring-boot-registry-nacos</artifactId> |
| 31 | + <groupId>org.boot.dubbo</groupId> |
| 32 | + <name>Apache Dubbo Spring Boot :: Samples : Registry Nacos</name> |
| 33 | + <description>Apache Dubbo Spring Boot Registry Nacos Samples</description> |
| 34 | + <version>${revision}</version> |
| 35 | + <packaging>pom</packaging> |
| 36 | +
|
| 37 | + <modules> |
| 38 | + <module>provider-sample</module> |
| 39 | + <module>consumer-sample</module> |
| 40 | + <module>sample-api</module> |
| 41 | + </modules> |
| 42 | +
|
| 43 | + ... |
| 44 | +</project> |
| 45 | +``` |
| 46 | + |
| 47 | +### 创建提供者 |
| 48 | +> 在子项目中引入必要的依赖 |
| 49 | +
|
| 50 | +``` |
| 51 | +<?xml version="1.0" encoding="UTF-8"?> |
| 52 | +<project xmlns="http://maven.apache.org/POM/4.0.0" |
| 53 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 54 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| 55 | + <parent> |
| 56 | + <groupId>org.boot.dubbo</groupId> |
| 57 | + <artifactId>dubbo-spring-boot-registry-nacos</artifactId> |
| 58 | + <version>${revision}</version> |
| 59 | + <relativePath>../pom.xml</relativePath> |
| 60 | + </parent> |
| 61 | + <modelVersion>4.0.0</modelVersion> |
| 62 | +
|
| 63 | + <artifactId>dubbo-spring-boot-registry-nacos-provider-sample</artifactId> |
| 64 | + <name>Apache Dubbo Spring Boot :: Samples : Registry Nacos :: Provider Sample</name> |
| 65 | +
|
| 66 | + <properties> |
| 67 | + <nacos.version>1.1.1</nacos.version> |
| 68 | + </properties> |
| 69 | +
|
| 70 | + <dependencies> |
| 71 | + <!-- Spring Boot dependencies --> |
| 72 | + <dependency> |
| 73 | + <groupId>org.springframework.boot</groupId> |
| 74 | + <artifactId>spring-boot-starter</artifactId> |
| 75 | + </dependency> |
| 76 | +
|
| 77 | + <dependency> |
| 78 | + <groupId>org.apache.dubbo</groupId> |
| 79 | + <artifactId>dubbo-spring-boot-starter</artifactId> |
| 80 | + <version>${revision}</version> |
| 81 | + </dependency> |
| 82 | +
|
| 83 | + <!-- Dubbo Registry Nacos --> |
| 84 | + <dependency> |
| 85 | + <groupId>org.apache.dubbo</groupId> |
| 86 | + <artifactId>dubbo-registry-nacos</artifactId> |
| 87 | + <version>${revision}</version> |
| 88 | + </dependency> |
| 89 | +
|
| 90 | + <dependency> |
| 91 | + <groupId>com.alibaba.nacos</groupId> |
| 92 | + <artifactId>nacos-client</artifactId> |
| 93 | + <version>${nacos.version}</version> |
| 94 | + </dependency> |
| 95 | +
|
| 96 | + <dependency> |
| 97 | + <groupId>org.boot.dubbo</groupId> |
| 98 | + <artifactId>dubbo-spring-boot-sample-api</artifactId> |
| 99 | + <version>${revision}</version> |
| 100 | + </dependency> |
| 101 | + </dependencies> |
| 102 | +
|
| 103 | + ... |
| 104 | +</project> |
| 105 | +``` |
| 106 | + |
| 107 | +> 添加Dubbo外部化配置,SpringBoot会进行自动配置 |
| 108 | +
|
| 109 | +这是Dubbo推荐的一种方式,也可以通过xml的形式进行配置 |
| 110 | + |
| 111 | +``` |
| 112 | +# Spring boot application |
| 113 | +spring.application.name=dubbo-registry-nacos-provider-sample |
| 114 | +# Base packages to scan Dubbo Component: @org.apache.dubbo.config.annotation.Service |
| 115 | +dubbo.scan.base-packages=org.boot.dubbo.nacos.demo.provider.service |
| 116 | +
|
| 117 | +# Dubbo Application |
| 118 | +## The default value of dubbo.application.name is ${spring.application.name} |
| 119 | +## dubbo.application.name=${spring.application.name} |
| 120 | +nacos.server-address = 127.0.0.1 |
| 121 | +nacos.port = 8848 |
| 122 | +
|
| 123 | +# Dubbo Protocol |
| 124 | +dubbo.protocol.name=dubbo |
| 125 | +## Random port |
| 126 | +dubbo.protocol.port=-1 |
| 127 | +
|
| 128 | +## Dubbo Registry |
| 129 | +dubbo.registry.address=nacos://${nacos.server-address}:${nacos.port} |
| 130 | +
|
| 131 | +## DemoService version |
| 132 | +demo.service.version=1.0.0 |
| 133 | +
|
| 134 | +``` |
| 135 | + |
| 136 | +> 编写示例接口和实现 |
| 137 | +
|
| 138 | +```java |
| 139 | +public interface DemoService { |
| 140 | + |
| 141 | + String sayHello(String name); |
| 142 | + |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | + |
| 147 | +```java |
| 148 | + |
| 149 | +@Service(version = "${demo.service.version}") |
| 150 | +public class DefaultDemoService implements DemoService { |
| 151 | + /** |
| 152 | + * The default value of ${dubbo.application.name} is ${spring.application.name} |
| 153 | + */ |
| 154 | + @Value("${dubbo.application.name}") |
| 155 | + private String serviceName; |
| 156 | + |
| 157 | + @Override |
| 158 | + public String sayHello(String name) { |
| 159 | + return String.format("[%s] : Hello, %s", serviceName, name); |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +``` |
| 164 | +这里通过Dubbo提供的`@Service`注解暴露服务,注意和Spring提供的`@Service`注解区分开。 |
| 165 | + |
| 166 | +```java |
| 167 | +@EnableAutoConfiguration |
| 168 | +public class DubboRegistryNacosProviderBootstrap { |
| 169 | + |
| 170 | + public static void main(String[] args) { |
| 171 | + new SpringApplicationBuilder(DubboRegistryNacosProviderBootstrap.class) |
| 172 | + .run(args); |
| 173 | + } |
| 174 | +} |
| 175 | +``` |
| 176 | + |
| 177 | +> 这时候可以启动提供者,可以看到我们编写的服务以及注册在Nacos中 |
| 178 | +
|
| 179 | + |
| 180 | + |
| 181 | +### 创建服务消费者 |
| 182 | + |
| 183 | +> 创建一个web项目用于消费我们刚才提供的服务 |
| 184 | +
|
| 185 | +``` |
| 186 | +<?xml version="1.0" encoding="UTF-8"?> |
| 187 | +<project xmlns="http://maven.apache.org/POM/4.0.0" |
| 188 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 189 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| 190 | + <parent> |
| 191 | + <groupId>org.boot.dubbo</groupId> |
| 192 | + <artifactId>dubbo-spring-boot-registry-nacos</artifactId> |
| 193 | + <version>${revision}</version> |
| 194 | + <relativePath>../pom.xml</relativePath> |
| 195 | + </parent> |
| 196 | + <modelVersion>4.0.0</modelVersion> |
| 197 | +
|
| 198 | + <artifactId>dubbo-spring-boot-registry-nacos-consumer-sample</artifactId> |
| 199 | + <name>Apache Dubbo Spring Boot :: Samples : Registry Nacos :: Consumer Sample</name> |
| 200 | +
|
| 201 | + <properties> |
| 202 | + <nacos.version>1.1.1</nacos.version> |
| 203 | + </properties> |
| 204 | +
|
| 205 | + <dependencies> |
| 206 | + <!-- Spring Boot dependencies --> |
| 207 | + <dependency> |
| 208 | + <groupId>org.springframework.boot</groupId> |
| 209 | + <artifactId>spring-boot-starter</artifactId> |
| 210 | + </dependency> |
| 211 | +
|
| 212 | + <dependency> |
| 213 | + <groupId>org.apache.dubbo</groupId> |
| 214 | + <artifactId>dubbo-spring-boot-starter</artifactId> |
| 215 | + <version>${revision}</version> |
| 216 | + </dependency> |
| 217 | +
|
| 218 | + <!-- Dubbo Registry Nacos --> |
| 219 | + <dependency> |
| 220 | + <groupId>org.apache.dubbo</groupId> |
| 221 | + <artifactId>dubbo-registry-nacos</artifactId> |
| 222 | + <version>${revision}</version> |
| 223 | + </dependency> |
| 224 | +
|
| 225 | + <dependency> |
| 226 | + <groupId>com.alibaba.nacos</groupId> |
| 227 | + <artifactId>nacos-client</artifactId> |
| 228 | + <version>${nacos.version}</version> |
| 229 | + </dependency> |
| 230 | +
|
| 231 | + <dependency> |
| 232 | + <groupId>org.boot.dubbo</groupId> |
| 233 | + <artifactId>dubbo-spring-boot-sample-api</artifactId> |
| 234 | + <version>${revision}</version> |
| 235 | + </dependency> |
| 236 | +
|
| 237 | + <dependency> |
| 238 | + <groupId>org.springframework.boot</groupId> |
| 239 | + <artifactId>spring-boot-starter-web</artifactId> |
| 240 | + <version>${spring-boot.version}</version> |
| 241 | + </dependency> |
| 242 | + </dependencies> |
| 243 | +
|
| 244 | + ... |
| 245 | +</project> |
| 246 | +
|
| 247 | +``` |
| 248 | + |
| 249 | +> 配置服务注册中心 |
| 250 | +
|
| 251 | +``` |
| 252 | +spring: |
| 253 | + application: |
| 254 | + name: dubbo-registry-nacos-consumer-sample |
| 255 | +
|
| 256 | +demo: |
| 257 | + service: |
| 258 | + version: 1.0.0 |
| 259 | +
|
| 260 | +nacos: |
| 261 | + host: localhost |
| 262 | + port: 8848 |
| 263 | +
|
| 264 | +dubbo: |
| 265 | + registry: |
| 266 | + address: nacos://${nacos.host}:${nacos.port} |
| 267 | +
|
| 268 | +server: |
| 269 | + port: 8081 |
| 270 | +``` |
| 271 | + |
| 272 | +> 编写Controller用于消费服务 |
| 273 | +
|
| 274 | +```java |
| 275 | +@RestController |
| 276 | +@RequestMapping("/demo") |
| 277 | +public class DemoController { |
| 278 | + |
| 279 | + @Reference(version = "${demo.service.version}") |
| 280 | + private DemoService demoService; |
| 281 | + |
| 282 | + @GetMapping("/{name}") |
| 283 | + public String sayHello(@PathVariable("name") String name) { |
| 284 | + return demoService.sayHello(name); |
| 285 | + } |
| 286 | + |
| 287 | +} |
| 288 | +``` |
| 289 | + |
| 290 | +这里直接通过`@Reference`注解进行服务调用,比xml方式更加优雅方便。 |
| 291 | + |
| 292 | +### 验证 |
| 293 | + |
| 294 | +直接通过浏览器访问/demo/{name}这个url进行访问即可,网页上显示`hello,xxx`表示服务消费成功。 |
| 295 | + |
| 296 | + |
| 297 | +## 总结 |
| 298 | +可以看到Dubbo重新维护之后,非常重视生态的建设,并且也在积极探索,Dubbo现在也是Apache基金会的孵化项目,未来可期,是除了Spring Cloud 之后又一个不错的选择。不同于其他注册中心的是,Nacos在阿里的支持下,生态发展得相当不错,并且已经可以用于生产环境。 |
| 299 | + |
| 300 | +#### 源码 |
| 301 | +[github](https://github.com/LuoLiangDSGA/spring-learning/tree/master/boot-dubbo-registry-nacos) |
0 commit comments