1、注册中心对比:
2、下载与安装:
下载地址:Releases · alibaba/nacos (github.com)
下载后解压缩,进入config目录,打开application.properties文件,修改:
### If use MySQL as datasource: spring.datasource.platform=mysql ### Count of DB: db.num=1 ### Connect URL of DB: db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai db.user.0=root db.password.0=123456 ### Connection pool configuration: hikariCP db.pool.config.connectionTimeout=30000 db.pool.config.validationTimeout=10000 db.pool.config.maximumPoolSize=20 db.pool.config.minimumIdle=2
打开本地mysql数据库的管理软件,新建nacos数据库。并执行config目录下的nacos-mysql.sql文件。创建nacos相关的表。
进入bin目录,使用命令如下命令启动:
ubuntu系统:
cd bin
./startup.sh -m standalone
windows系统:
startup.cmd -m standalone
打开浏览器访问 http://localhost:8848/nacos
用户名:nacos
密码:nacos
即可打开nacos管理后台。
3、确定环境版本:
版本说明:版本说明 · alibaba/spring-cloud-alibaba Wiki (github.com)
本文使用的版本为:
SpringCloud-alibaba版本:2.2.5.RELEASE
SpringCloud版本:Hoxton.SR8
SpringBoot版本:2.3.2RELEASE
4、代码示例:
4.1 父项目
使用IDEA建立一个空的Maven项目,pom文件内容如下:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.yas</groupId> <artifactId>cloud-demo01</artifactId> <version>1.0-SNAPSHOT</version> <modules> <module>cloud-goods</module> <module>cloud-entity</module> <module>cloud-order</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.2.RELEASE</version> <relativePath></relativePath> </parent> <packaging>pom</packaging> <dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.2.5.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR8</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> </project>
4.2 实体模块,cloud-entity:
pom文件引入lombok
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>cloud-demo01</artifactId> <groupId>org.yas</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-entity</artifactId> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> </project>
实体类:
1 package com.yas; 2 3 import lombok.AllArgsConstructor; 4 import lombok.Data; 5 import lombok.NoArgsConstructor; 6 7 @Data 8 @NoArgsConstructor 9 @AllArgsConstructor 10 public class Goods { 11 private String goodName; 12 private double price; 13 }
4.3 服务提供者模块,cloud-goods:
pom文件内容:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>cloud-demo01</artifactId> <groupId>org.yas</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-goods</artifactId> <dependencies> <!-- web场景依赖 --> <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> <!-- nacos场景依赖 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.yas</groupId> <artifactId>cloud-entity</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> </project>
配置文件,application.yml
spring: application: name: cloud-goods #服务名称,必须唯一 cloud: nacos: discovery: server-addr: localhost:8848 #指定nacos服务地址 username: nacos password: nacos server: port: 9901
启动类:
1 package com.yas; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 7 @SpringBootApplication 8 @EnableDiscoveryClient//开启服务注册与发现功能 9 public class GoodApp { 10 public static void main(String[] args) { 11 SpringApplication.run(GoodApp.class); 12 } 13 }
测试Controller:
1 package com.yas.controller; 2 3 import com.yas.Goods; 4 import org.springframework.web.bind.annotation.PathVariable; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RestController; 7 8 @RestController 9 @RequestMapping("/goods") 10 public class GoodsController { 11 @RequestMapping("/findById/{id}") 12 public Goods findById(@PathVariable String id) { 13 System.out.println("id=" + id); 14 return new Goods("小米", 99); 15 } 16 }
4.4 服务消费者模块,cloud-order:
pom文件:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>cloud-demo01</artifactId> <groupId>org.yas</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-order</artifactId> <dependencies> <!-- web场景依赖 --> <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> <!-- nacos场景依赖 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.yas</groupId> <artifactId>cloud-entity</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> </project>
配置文件,application.yml:
spring: application: name: cloud-order #服务名称,必须唯一 cloud: nacos: discovery: server-addr: localhost:8848 #指定nacos服务地址 username: nacos password: nacos server: port: 9900
启动类:
1 package com.yas; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 import org.springframework.cloud.client.loadbalancer.LoadBalanced; 7 import org.springframework.context.annotation.Bean; 8 import org.springframework.web.client.RestTemplate; 9 10 @SpringBootApplication 11 @EnableDiscoveryClient 12 public class OrderApp { 13 public static void main(String[] args) { 14 SpringApplication.run(OrderApp.class); 15 } 16 17 @Bean 18 @LoadBalanced//使用Ribon调用服务,将域名请求变为服务地址请求 19 public RestTemplate initRestTemplate() { 20 return new RestTemplate(); 21 } 22 }
测试Controller:
1 package com.yas.controller; 2 3 import com.yas.Goods; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RestController; 7 import org.springframework.web.client.RestTemplate; 8 9 import java.util.HashMap; 10 import java.util.Map; 11 12 @RestController 13 @RequestMapping("/order") 14 public class OrderController { 15 16 @Autowired 17 RestTemplate restTemplate; 18 19 @RequestMapping("/save") 20 public Map save() { 21 //远程调用cloud-goods服务,获取goods信息,发送http请求(httpclient) 22 //硬编码的方式 23 // String uri1 = "http://localhost:9901/goods/findById/1"; 24 // Goods goods = restTemplate.getForObject(uri1, Goods.class); 25 26 String serviceName = "cloud-goods"; 27 String uri2 = "http://" + serviceName + "/goods/findById/1"; 28 Goods goods = restTemplate.getForObject(uri2, Goods.class); 29 System.out.println(goods); 30 31 //模拟保存订单 32 System.out.println("save order success"); 33 Map<String, String> map = new HashMap<>(); 34 map.put("code", "200"); 35 map.put("msg", "success"); 36 return map; 37 } 38 }
5、测试:
启动服务提供者和服务消费者,在8848端口的管理平台查询到两个服务:
使用postman请求地址:localhost:9900/order/save
得到如下结果:
6、Nacos领域模型:
nacos的服务由三元组唯一确定:namespace、group、servicename
nacos的配置由三元组唯一确定:namespace、group、dataId
不同的namespace是相互隔离的,相同namespace但是不同的group也是相互隔离的。
默认的namespace是public,不能删除。
默认的group是DEFAULT_GROUP。