SpringCloud使用nacos作为配置中心和注册中心

1简介

Spring Cloud Alibaba 致力于提供微服务开发的一站式解决方案,Nacos 作为其核心组件之一,可以作为注册中心和配置中心使用,本文将对其用法进行详细介绍。

Nacos简介
Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。

Nacos 具有如下特性:

服务发现和服务健康监测:支持基于DNS和基于RPC的服务发现,支持对服务的实时的健康检查,阻止向不健康的主机或服务实例发送请求;
动态配置服务:动态配置服务可以让您以中心化、外部化和动态化的方式管理所有环境的应用配置和服务配置;
动态 DNS 服务:动态 DNS 服务支持权重路由,让您更容易地实现中间层负载均衡、更灵活的路由策略、流量控制以及数据中心内网的简单DNS解析服务;
服务及其元数据管理:支持从微服务平台建设的视角管理数据中心的所有服务及元数据。

我们先从官网下载Nacos,这里下载的是nacos-server-1.1.4.zip文件,下载地址:
https://github.com/alibaba/nacos/releases

配置JAVA_HOME环境变量,不配置会导致无法运行Nacos;

JAVA_HOME=C:\develop\Java\jdk1.8.0_191

解压安装包,直接运行bin目录下的startup.cmd;

2注册中心

将原先的user-service 和 order-service按下方的配置

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.8.RELEASE</version>
		<relativePath />
	</parent>
	<properties>
		<spring.cloud-version>Hoxton.SR8</spring.cloud-version>
	</properties>
	<dependencies>
		<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>
	</dependencies>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>com.alibaba.cloud</groupId>
				<artifactId>spring-cloud-alibaba-dependencies</artifactId>
				<version>2.1.1.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
server:
  port: 8302

spring:
  application:
    name: nacos-user-service
  cloud:
    nacos:
      discovery:
        server-addr: http://localhost:8848
management:
  endpoints:
    web:
      exposure:
		include: '*'

分别启动user和order.发现http://localhost:8848/nacos 里面已经有了这两项服务。
在这里插入图片描述

负载均衡

启动两个user和一个order,order调用user使用ribbon时候进行了负载均衡。

3配置中心

新建客户端nacos-config

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.8.RELEASE</version>
		<relativePath />
	</parent>
	<properties>
		<spring.cloud-version>Hoxton.SR8</spring.cloud-version>
	</properties>
	<dependencies>
		<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>
		<dependency>
			<groupId>com.alibaba.cloud</groupId>
			<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
		</dependency>

	</dependencies>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>com.alibaba.cloud</groupId>
				<artifactId>spring-cloud-alibaba-dependencies</artifactId>
				<version>2.1.1.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

要有两个配置文件
application.yml

spring:
  profiles:
    active: dev

bootstrap.yml

server:
  port: 9101

spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:       
        server-addr: http://localhost:8848
      config:
        server-addr: http://localhost:8848
        file-extension: yaml
@RefreshScope
@RestController
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public Object getConfigInfo() {
        return configInfo;
    }


}

在Nacos中添加配置
我们先来讲下Nacos中的dataid的组成格式及与SpringBoot配置文件中的属性对应关系:

${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}

比如说我们现在要获取应用名称为nacos-config-client的应用在dev环境下的yaml配置,dataid如下:

nacos-config-client-dev.yaml

按照以上dataid添加如下配置:

config:
  info: "config info for dev"

在这里插入图片描述

启动nacos-config-client,调用接口查看配置信息:http://localhost:9101/configInfo

config info for dev

Nacos的动态刷新配置
我们只要修改下Nacos中的配置信息,再次调用查看配置的接口,就会发现配置已经刷新,Nacos和Consul一样都支持动态刷新配置。当我们在Nacos页面上修改配置并发布后,应用会刷新配置并打印如下信息

2019-12-30 17:11:39.736  INFO 17640 --- [-localhost_8848] o.s.c.e.event.RefreshEventListener       : Refres
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值