目录
一、简介
Nacos是阿里开源的项目,用于实现动态服务发现、服务配置管理(官网:https://nacos.io/zh-cn/docs/what-is-nacos.html)。说到这里,做过微服务的小伙伴应该会有熟悉感(内心OS:这个不就是跟Zookeeper的功能一样吗!!!)。而在SpringCloud生态中,我们也能找到对应的产品,Eureka注册中心用于动态服务发现,SpringCloudConfig配置中心用于服务配置管理。
根据分布式架构的CAP理论来说,Nacos是可以设置成CP或者AP 模式,Zookeeper是CP,Eureka是AP;同时Nacos作为同时集成了Eureka和SpringCloudConfig两大产品的功能,运维只需要维护一套Nacos集群,而且具备着好看易用的管理页面......balabala(自己脑补画面:哎呀哎呀,不用说了,我用还不行吗!)
二、Nacos服务搭建
这里只搭建单机版本,集群版本的搭建大家可以参考https://nacos.io/zh-cn/docs/cluster-mode-quick-start.html
准备环境:Linux 64 bit系统 和 64 bit JDK 1.8+;
1、下载安装包
从官网(https://github.com/alibaba/nacos/releases)中下载版本nacos-server-2.0.1.tar.gz,上传到linux服务器中;
2、解压
tar -xvf nacos-server-2.0.1.tar.gz
3、启动
cd nacos/bin
sh startup.sh -m standalone
4、访问管理页面
浏览器上输入,http://ip:8848/nacos 默认账号是nacos,密码是nacos
三、配置中心实战
项目结构预览:
引用的版本为:
Spring Cloud Version | Spring Cloud Alibaba Version | Spring Boot Version |
Spring Cloud Hoxton.SR8 | 2.2.3.RELEASE | 2.3.2.RELEASE |
1、创建parent的pom.xml文件如下:
<?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>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.sam.cloud</groupId>
<artifactId>magic-cloud</artifactId>
<version>1.0-SNAPSHOT</version>
<name>magic-cloud</name>
<modules>
<module>magic-provider</module>
<module>magic-consumer</module>
</modules>
<properties>
<project.version>0.0.1-SNAPSHOT</project.version>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR8</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
2、子项目magic-provider的pom.xml文件
<?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>magic-cloud</artifactId>
<groupId>com.sam.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>magic-provider</artifactId>
<name>magic-provider</name>
<version>${project.version}</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<finalName>magic-provider</finalName>
</build>
</project>
这里关键是要引入maven包:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
3、application.properties文件
# 应用启动端口
server.port=8082
# 访问路径
server.servlet.context-path=/magic/provider
# 应用名称,用于nacos的注册
spring.application.name=magic-provider
# nacos配置中心地址
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.server-addr=127.0.0.1:8848设置nacos的ip和端口号,启动后会自动从nacos上获取配置。
4、创建启动类MagicProviderApplication.java
package com.sam.cloud.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MagicProviderApplication {
public static void main(String[] args) {
SpringApplication.run(MagicProviderApplication.class, args);
}
}
5、创建配置测试Controller:ConfigController.java
package com.sam.cloud.provider.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController {
@Value("${my.name:haha}")
private String myName;
@RequestMapping("/get")
public String get(String msg) {
return "hi, " + msg + ", my name is " + myName;
}
}
这里通过 Spring Cloud 原生注解 @RefreshScope
实现nacos的magic-provider配置自动更新my.name的值;
6、Nacos管理页面上添加配置
这里的Data ID的值要跟项目magic-provider的配置文件application.properties里面的spring.application.name值一致;
7、验证
启动应用magic-provider,在浏览器中输入访问地址:http://localhost:8082/magic/provider/config/get?msg=Tom
然后在nacos管理页面修改配置值my.name=Jack,刷新请求,发现已经动态更新
四、动态服务发现实战
A、magic-provider项目
在上面第三步创建的项目基础上进行配置:
1、添加maven包
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
2、application中添加内容
# nacos注册发现地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
3、启动类MagicProviderApplication.java中加入注解@EnableDiscoveryClient开启服务注册发现功能
package com.sam.cloud.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class MagicProviderApplication {
public static void main(String[] args) {
SpringApplication.run(MagicProviderApplication.class, args);
}
}
B、magic-consumer项目
项目结构预览:
1、子项目pom.xml文件如下:
<?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>magic-cloud</artifactId>
<groupId>com.sam.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>magic-consumer</artifactId>
<name>magic-consumer</name>
<version>${project.version}</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<finalName>magic-consumer</finalName>
</build>
</project>
2、application.properties文件
# 应用启动配置
server.port=8083
server.servlet.context-path=/magic/consumer
spring.application.name=magic-consumer
# nacos注册发现地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
3、创建配置类ConsumerConfiguration.java
package com.sam.cloud.consumer.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ConsumerConfiguration {
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
这里采用给 RestTemplate 实例添加 @LoadBalanced 注解,开启 @LoadBalanced 与 Ribbon 的集成;
4、创建测试类ConsumerController.java
package com.sam.cloud.consumer.controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
@RequestMapping("/test")
public class ConsumerController {
@Resource
private RestTemplate restTemplate;
@RequestMapping(value = "/hello/{name}", method = RequestMethod.GET)
public String testHello(@PathVariable String name) {
String message = restTemplate.getForObject("http://magic-provider/magic/provider/config/get?msg=" + name, String.class);
return "consumer : " + message;
}
}
这里使用已经初始化的RestTemplate请求服务提供方magic-provider;
4、创建启动类MagicProviderApplication.java
package com.sam.cloud.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class MagicConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(MagicConsumerApplication.class, args);
}
}
同样的,注解@EnableDiscoveryClient开启服务注册发现功能
C、验证
1、启动magic-provider项目
2、启动magic-consumer项目
3、查看nacos管理页面中的服务列表
4、浏览器中通过访问magic-consumer的接口,获取magic-provider中的接口数据
http://localhost:8083/magic/consumer/test/hello/Marry