文件名称 | 版本号 | 作者 | 版本 | |
---|---|---|---|---|
Nacos学习 | v1.0.0 | 学生宫布 | 8416837 | SpringCloud H Nacos 1.1.4 mysql 8 |
查看Nacos文档
点击下载
https://github.com/alibaba/nacos/releases/download/1.1.4/nacos-server-1.1.4.tar.gz
tar xvzf 文件 -C 目录 # 解压
cd 根目录
sh bin/startup.sh -m standalone # 单机启动
tail -f logs/start.out -n 500 # 查看启动日志
http://192.168.4.152:8848/nacos/index.html 访问
初始用户名密码:nacos=nacos
docker search nacos
docker pull nacos/nacos-server
docker run -d \
--name nacos-server \
--network common-network \
-e PREFER_HOST_MODE=hostname \
-e MODE=standalone \
-p 8848:8848 \
nacos/nacos-server
执行:
【普版】因为没有自定义网络,去掉网络参数,并且删除启动失败的容器,再次执行:8852
是对外端口
docker run -d \
--name nacos-server \
-e PREFER_HOST_MODE=hostname \
-e MODE=standalone \
-p 8852:8848 \
--restart=always \
nacos/nacos-server
删除原来的:
检查启动情况:
先开放端口或关闭防火墙,然后在内网访问nacos,账号密码都是:nacos:
启动成功。
docker run -d \
--name nacos-server \
--network common-network \
-e PREFER_HOST_MODE=hostname \
-e MODE=standalone \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_MASTER_SERVICE_HOST=mysql-master \
-e MYSQL_MASTER_SERVICE_PORT=3306 \
-e MYSQL_MASTER_SERVICE_USER=root \
-e MYSQL_MASTER_SERVICE_PASSWORD=Passw0rd \
-e MYSQL_MASTER_SERVICE_DB_NAME=nacos \
-e MYSQL_SLAVE_SERVICE_HOST=mysql-slave \
-e MYSQL_SLAVE_SERVICE_PORT=3306 \
-p 8848:8848 \
nacos/nacos-server
根据源码创建配置数据库:ry-config
创建好表之后,再进行下一步:
配置文件(节选):
改成你自己的内网ip:(注意:使用db.url.0)
# spring
server.servlet.contextPath=${SERVER_SERVLET_CONTEXTPATH:/nacos}
server.contextPath=/nacos
server.port=${NACOS_SERVER_PORT:8848}
spring.datasource.platform=${SPRING_DATASOURCE_PLATFORM:mysql}
nacos.cmdb.dumpTaskInterval=3600
nacos.cmdb.eventTaskInterval=10
nacos.cmdb.labelTaskInterval=300
nacos.cmdb.loadDataAtStart=false
db.num=${MYSQL_DATABASE_NUM:1}
db.url.0=jdbc:mysql://192.168.59.1:${MYSQL_SERVICE_PORT:3307}/ry-config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&serverTimezone=GMT%2B8
db.user=${MYSQL_SERVICE_USER:root}
db.password=${MYSQL_SERVICE_PASSWORD:root}
### The auth system to use, currently only 'nacos' is supported:
nacos.core.auth.system.type=${NACOS_AUTH_SYSTEM_TYPE:nacos}
如果是mysql8 server
,请增加插件:
cd nacos根目录
mkdir -p plugins/mysql
# 将文件mysql-connector-java-8.x.jar放进mysql目录
【下载mysql-connector-java-8.x.jar】
重启,观察Nacos日志:
mysql8连接成功,特别提示:
sql节选:
上文有讲,将sql(sql里包含了配置信息)写入数据库,启动完毕,即可在Nacos配置中心看到配置列表:
然后将配置文件改为实际配置,点击发布按钮即可。
启动成功标志:2020-07-01 11:32:08,775 INFO Nacos started successfully in stand alone mode. use external storage,
curl -X POST "http://192.168.4.152:8848/nacos/v1/cs/configs?dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=user.id=9527%0Auser.name=John%0Auser.age=136"
Java类:
package test.rest;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 功能:
*
* @author: cc
* @qq: 8416837
* @date: 2020/4/4 20:41
*/
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
Environment environment;
@Value("${app.user.name}")
String name;
@GetMapping("/resource")
public ResponseEntity getResource() {
JSONObject data = new JSONObject();
data.put("name", name);
data.put("serverPort", environment.getProperty("server.port"));
return new ResponseEntity(data, HttpStatus.OK);
}
}
访问这个接口
新建Nacos配置
然后发布,配置并没起效。配置Data ID改为:blade-dev.yaml
就起效了。
日志:
2020-04-04 23:53:05.942 INFO 23628 --- [.168.1.107_8848] c.a.c.n.c.NacosPropertySourceBuilder : Loading nacos data, dataId: 'blade-dev.yaml', group: 'DEFAULT_GROUP', data: app:
user:
name: CC
2020-04-04 23:53:05.943 INFO 23628 --- [.168.1.107_8848] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-blade-dev.yaml'}, BootstrapPropertySource {name='bootstrapProperties-blade.yaml'}, BootstrapPropertySource {name='bootstrapProperties-blade'}]
2020-04-04 23:53:05.956 INFO 23628 --- [.168.1.107_8848] o.s.boot.SpringApplication : The following profiles are active: dev
2020-04-04 23:53:05.981 INFO 23628 --- [.168.1.107_8848] o.s.boot.SpringApplication : Started application in 1.789 seconds (JVM running for 355.194)
2020-04-04 23:53:06.157 INFO 23628 --- [.168.1.107_8848] o.s.c.e.event.RefreshEventListener : Refresh keys changed: [app.user.name]
至于Data ID为什么要配置成blade-dev.yaml?因为Data ID必须是 s p r i n g . c l o u d . n a c o s . c o n f i g . p r e f i x − {spring.cloud.nacos.config.prefix}- spring.cloud.nacos.config.prefix−{spring.profiles.active}.yaml.如:
props.setProperty("spring.cloud.nacos.config.prefix", "blade");
props.setProperty("spring.cloud.nacos.config.file-extension", "yaml");
因为prefix被预置成了blade。
小贴士
如果指定了yaml配置,就算再Nacos控制台勾选非yaml,也是可以的:
但是配置ID写成yml就不起效,很严格,如:
不完全匹配就不会起效。
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
<version>2.1.0.RELEASEversion>
dependency>
nacos registry, DEFAULT_GROUP jxbd-gateway-authentication-chengchong 192.168.59.1:10010 register finished