SpringCloud全家桶:SpringCloud01 – 初识
SpringCloud全家桶:SpringCloud02 – 服务注册 Eureka Zookeeper Consul Nacos
SpringCloud全家桶:SpringCloud03 – 服务调用 Ribbon OpenFeign
SpringCloud全家桶:SpringCloud04 – 服务降级熔断 Hystrix Sentinel
SpringCloud全家桶:SpringCloud05 – 服务网关 Gateway
SpringCloud全家桶:SpringCloud06 – 服务配置 Config Nacos
SpringCloud全家桶:SpringCloud07 – 消息总线 Bus
SpringCloud全家桶:SpringCloud08 – 消息驱动 Stream
SpringCloud全家桶:SpringCloud09 – 分布式请求链路追踪 Sleuth
SpringCloud全家桶:SpringCloud10 – Alibaba Nacos
SpringCloud全家桶:SpringCloud11 – Alibaba Sentinel
SpringCloud全家桶:SpringCloud12 – Alibaba 分布式事务 Seata
Nacos
简介
一个更易于构建云原生应用的动态服务发现,配置管理和服务管理中心
Nacos就是注册中心+配置中心
Nacos = Eureka + Config + Bus
为啥那么叫Nacos?
Nacos: Dynamic Naming and Configuration Service
`Na`对应`Naming`,`co`对应`Configuration`的前两个字母,`s`对应着`Service`
能做什么呢?
- 代替Eureka做服务注册中心
- 代替Config做服务配置中心
注册中心的比较
安装Nacos
Windows
- 本地Java8+Maven系统
- 下载nacos
- 解压安装包 直接运行bin目录下的startup.cmd
- 直接访问localhost:8848/nacos 账号密码都是nacos
Linux
服务注册
新建项目cloud-alibaba-provider-payment9001
pom
<dependencies>
<!-- SpringCloud ailibaba 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>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
yml
server:
port: 9001
spring:
application:
name: nacos-payment-provider
cloud:
nacos:
discovery:
server-addr: localhost:8848 # 配置Nacos地址
management:
endpoints:
web:
exposure:
include: "*"
启动类
@EnableDiscoveryClient
@SpringBootApplication
public class NacosPayment9001 {
public static void main(String[] args) {
SpringApplication.run(NacosPayment9001.class);
}
}
controller
@RestController
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@RequestMapping(value = "/payment/nacos/{id}", method = RequestMethod.GET)
public String getPayment(@PathVariable("id") Integer id) {
return "nacos register, serverport: " + serverPort+"\t id: "+id;
}
}
参考9001 建立cloud-alibaba-provider-payment9002
建立消费端 cloud-alibaba-nacos-consumer-order83
pom
<dependencies>
<!-- SpringCloud ailibaba nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency><!-- 引用自己定义的api通用包,可以使用Payment支付Entity -->
<groupId>com.jsu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</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>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
yml
server:
port: 83
spring:
application:
name: nacos-order-consumer
cloud:
nacos:
discovery:
server-addr: localhost:8848
# server-addr: 47.93.139.167:1111
# 消费者将要去访问的微服务名称(注册成功进入nacos的微服务提供者)
service-url:
nacos-user-service: http://nacos-payment-provider
启动类
server:
port: 83
spring:
application:
name: nacos-order-consumer
cloud:
nacos:
discovery:
server-addr: localhost:8848
# server-addr: 47.93.139.167:1111
# 消费者将要去访问的微服务名称(注册成功进入nacos的微服务提供者)
service-url:
nacos-user-service: http://nacos-payment-provider
逻辑代码
// 实现负载均衡
@Configuration
public class ApplicationContext {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
@RestController
@Slf4j
public class OrderController {
// 使用了配置文件的方式 避免了写死
@Value("${service-url.nacos-user-service}")
private String serverURL;
@Autowired
private RestTemplate restTemplate;
@RequestMapping(value = "/consumer/payment/nacos/{id}")
public String paymentInfo(@PathVariable("id") Integer id) {
return restTemplate.getForObject(serverURL + "/payment/nacos/" + id, String.class);
}
}
测试
同时实现负载均衡 使用83 访问9001/9002 轮询负载成功
服务注册的对比
配置中心
新建项目cloud-alibaba-nacos-config-client3377
pom
<dependencies>
<!-- nacos config-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- SpringCloud ailibaba nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency><!-- 引用自己定义的api通用包,可以使用Payment支付Entity -->
<groupId>com.jsu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</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>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
yml
bootstrap.l.yml
server:
port: 3377
spring:
application:
name: nacos-config-client
cloud:
nacos:
discovery:
server-addr: localhost:8848 # nacos服务注册中心
config:
server-addr: localhost:8848 # nacos服务配置中心
file-extension: yaml # 去读取8848上的yaml格式的配置
application.yml
spring:
profiles:
# 配什么加载什么
active: dev # 开发环境
启动类
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConfig3377 {
public static void main(String[] args) {
SpringApplication.run(NacosConfig3377.class);
}
}
controller
@RestController
@RefreshScope // 自动刷新
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@RequestMapping(value = "/config/info",method = RequestMethod.GET)
public String getConfigInfo(){
return configInfo;
}
}
nacos新建配置
测试成功
测试自动刷新
修改环境
注意 nacos 新建nacos-config-client-test.yaml 以及yml内容
都要新建配置
GROUP
NASPACE
持久化
Nacos默认自带是嵌入式数据库Derby
将内置数据库 设置为mysql
-
nacos/conf 找到nacos-mysql.sql 运行
-
nacos/conf 找到application.properties配置
spring.datasource.platform=mysql db.num=1 db.url.0=jdbc:mysql://xx.xx.xx.xx:3306/nacos-config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true db.user=root db.password=root
启动nacos 发现全新的 新建一个以后 发现数据库中多了一条数据