本篇记录了关于Nacos-server2.0.3的搭建过程(VMware和CentOS7),以及SpringBoot整合Nacos,已搭建的可以跳过第一节看第三节的整合过程。
记录和分享也是一种学习的方式,希望大家多多探讨相关技术问题。
一、搭建nacos-server2.0.3本文首发地址:https://xu.vercel.app
禁止转载!
下载编译好的包官方搭建文档
cd /opt wget https://github.com/alibaba/nacos/releases/download/2.0.3/nacos-server-2.0.3.tar.gz tar -xvf nacos-server-2.0.3.tar.gz cd nacos/bin启动命令
# Linux/Unix/Mac # 启动命令(standalone代表着单机模式运行,非集群模式): sh startup.sh -m standalone # 如果您使用的是ubuntu系统,或者运行脚本报错提示[[符号找不到,可尝试如下运行: bash startup.sh -m standalone # Windows # 启动命令(standalone代表着单机模式运行,非集群模式): startup.cmd -m standaloneCentos7下启动提示
[root@localhost bin]# sh startup.sh -m standalone /usr/java/jdk1.8/bin/java -Djava.ext.dirs=/usr/java/jdk1.8/jre/lib/ext:/usr/java/jdk1.8/lib/ext -Xms512m -Xmx512m -Xmn256m -Dnacos.standalone=true -Dnacos.member.list= -Xloggc:/opt/nacos2.0.3/nacos/logs/nacos_gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M -Dloader.path=/opt/nacos2.0.3/nacos/plugins/health,/opt/nacos2.0.3/nacos/plugins/cmdb -Dnacos.home=/opt/nacos2.0.3/nacos -jar /opt/nacos2.0.3/nacos/target/nacos-server.jar --spring.config.additional-location=file:/opt/nacos2.0.3/nacos/conf/ --logging.config=/opt/nacos2.0.3/nacos/conf/nacos-logback.xml --server.max-http-header-size=524288 nacos is starting with standalone nacos is starting,you can check the /opt/nacos2.0.3/nacos/logs/start.out关闭服务器
# Linux/Unix/Mac sh shutdown.sh # Windows shutdown.cmd # 或者双击shutdown.cmd运行文件 # 或者kill -15 进程id请求测试
[root@localhost bin]# curl -v http://127.0.0.1:8848/nacos
* about to connect() to 127.0.0.1 port 8848 (#0)
* Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 8848 (#0)
> GET /nacos HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 127.0.0.1:8848
> Accept: *
@GetMapping("/info/{id}")
public Object getStudentInfo(@PathVariable("id")Integer id){
Map info = new HashMap<>();
info.put("id",id);
info.put("info",queryStudentInfo(id));
return info;
}
private JSONObject queryStudentInfo(Integer id) {
try {
if (namingService != null) {
// 选择user_service服务的一个健康的实例(可配置负载均衡策略)
Instance instance = namingService.selectOneHealthyInstance("nacos-producer-service");
// 拼接请求接口url并请求选取的实例
String url = "http://" + instance.getIp() + ":" + instance.getPort() + "/demo/student/detail/"+id;
ResponseEntity entity = restTemplate.getForEntity(url, JSONObject.class);
return entity.getBody();
}
} catch (Exception e) {
log.error("查询学生数据失败", e);
}
return null;
}
}
在nacos-config-discovery-producer中创建一个Controller,作为服务生产者
@Slf4j
@RestController
@RequestMapping("/student")
public class StudentController {
@GetMapping("/detail/{id}")
public Object getStudentInfo(@PathVariable("id")Integer id){
Map info = new HashMap<>();
info.put("id",id);
info.put("userName","test");
info.put("nickName","小明");
return info;
}
}
调用接口
[root@localhost bin]# curl -X GET http://192.168.1.212:8082/demo/student/info/1
{"id":1,"info":{"nickName":"小明","id":1,"userName":"test"}}



