目录
目录
2.2加入注解:@EnableDiscoveryClient
4.1 添加依赖包spring-cloud-alibaba-nacos-config
4.2 编写配置文件,需要写一个bootstrap.yml配置文件
序号 | 名称 | 链接地址 |
1 | spring cloud Alibaba系列(一)nacos配置中心使用详解 | https://blog.csdn.net/qq_38130094/article/details/104722653 |
简介
spring cloud Alibaba是spring cloud的一个子项目,听过了微服务开发的一整套企业级解决方案;包含了微服务开发的必要组件是阿里的微服务解决方案
nacos简介:Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。
1. Nacos服务端搭建
nacos下载地址:https://github.com/alibaba/Nacos/releases
1.2 Linux环境下使用nacos服务端
1:进入到安装目录后上传Nacos包,解压:tar -zxvf nacos-server-1.2.0.tar.gz
2: 进入解压缩后的目录:cd nacos
3: 进入到bin目录下单机启动nacos:sh startup.sh -m standalone
4:查看进程或端口状态:ps -ef|grep java netstat -tunlp|grep 8848
5:停止nocas 在nocas/bin目录下 执行 sh shutdown.sh
5:访问nocas的服务端 http://IP:8848/nacos/index.html 默认的用户名密码是nocas/nocas;我的这个现在直接进入没有密码
1.3 windows下搭建nacos服务端
1:解压nacos目录后进入bin目录:
2. nacos client端搭建
2.1 加入依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring‐cloud‐alibaba‐nacos‐discovery</artifactId>
</dependency>
2.2加入注解:@EnableDiscoveryClient
@SpringBootApplication
@EnableDiscoveryClient
@MapperScan("com.ssy")
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
2.3 配置文件: application.yml
server:
port: 8081
spring:
datasource:
druid:
username: root
password: 123456
jdbcUrl: jdbc:mysql://localhost:3306/alibaba?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
driverClassName: com.mysql.jdbc.Driver
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
filters: stat,wall #\u914D\u7F6E\u8FC7\u6EE4\u5668
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
application:
name: product-center
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #nacos配置地址
#metadata:
#version: v1
#namespace:
#group: pay
#cluster-name:
#namespace:
效果:
3. Nacos 领域模型划分以及概念详解
待研究
4. 微服务接入配置中心的步骤
4.1 添加依赖包spring-cloud-alibaba-nacos-config
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring‐cloud‐alibaba‐nacos‐config</artifactId>
</dependency>
4.2 编写配置文件,需要写一个bootstrap.yml配置文件
spring:
cloud:
nacos:
config:
server-addr: localhost:8848
file-extension: yml
shared-dataids: common.yml,common2.yml
refreshable-dataids: common.yml,common2.yml
ext-config:
- data-id: common3.yml
group: DEFAULT_GROUP
refresh: true
- data-id: common4.yml
group: DEFAULT_GROUP
refresh: true
application:
name: product-center
profiles:
active: dev
- server-addr: localhost:8848 表示我微服务怎么去找我的配置中心
- spring.application.name=order-center 表示当前微服务需要向配置中心索要order-center的配置
- spring.profiles.active=prod 表示我需要向配置中心索要order-center的生产环境的配置
- 索要文件的格式为${application.name}- ${spring.profiles.active}.${file-extension}
- 更具上述文件去nacos配置中心上拿到的配置文件就是product-center-prod.yml
5 nacos集群搭建
5.1 下载编译后压缩包方式
wget https://github.com/alibaba/nacos/releases/download/1.3.0/nacos-server-1.3.0.tar.gz
5.2 配置集群配置文件
在nacos的解压目录nacos/的conf目录下,有配置文件cluster.conf,请每行配置成ip:port。(请配置3个或3个以上节点)
# ip:port
172.17.24.185:8848
172.17.24.186:8848
172.17.24.187:8848
5. 3 确定数据源
mysql数据源
尽量使用高可用的mysql数据源
在安装包里的 nacos-mysql.sql 初始化数据库
### 内置数据源
无需进行任何配置**但是启动命令需要修改**
sh startup.sh -p embedded
5. 4 启动nacos
#单机:
sh startup.sh -m standalone
使用内置数据源:
sh startup.sh -p embedded
#使用外置数据源
sh startup.sh
5. 5 访问
url: ip:port/nacos
用户名密码:nacos/nacos