微服务注册中心nacos学习:先尝试使用它,然后撸它源码搞懂它。

在这里整理一下自己之前集成nacos的内容。

我的github地址:https://github.com/mrxiaobai-wen/springcloud_study.git

前置条件:下载nacos并安装启动。

服务提供者集成

创建一个Spring Cloud项目,即nacos-server-spring-cloud。

引入Nacos的依赖

<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>

配置nacos连接

bootstrap.yml配置内容:

server:
port: 8021 spring:
application:
name: nacos-server-spring-cloud
cloud:
nacos:
discovery:
server-addr: http://localhost:8848
config:
server-addr: http://localhost:8848
file-extension: yaml

在application.yml中添加一个ceshi.version配置,用于后面测试nacos配置中心:

ceshi:
version: dev

创建启动类

@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigurationProperties
public class NacosServerSpringCloudApplication { @Resource
private ConfigBean configBean; public static void main(String[] args) {
SpringApplication.run(NacosServerSpringCloudApplication.class, args);
} @RestController
class EchoController {
@RequestMapping(value = "/server/echo/{string}", method = RequestMethod.GET)
public String echo(@PathVariable String string) {
return "Hello Nacos Discovery " + string + " 当前版本:" + configBean.getVersion();
}
} }
@ConfigurationProperties("ceshi")
@Component
public class ConfigBean { public String version; public String getVersion() {
return version;
} public void setVersion(String version) {
this.version = version;
}
}

就这样,启动上面的NacosServerSpringCloudApplication后,就可以在本地nacos服务列表中查看到当前服务了,然后在nacos配置中心里面新建一个nacos-server-spring-cloud.yml文件,变更发布ceshi.version的值,然后访问localhost:8021/server/echo/{string}就可以看到变更的内容了。

这样就简单的完成了服务注册和配置动态管理。

采坑

我在最开始的时候,将bootstrap.yml的内容放在application.yml中,然后在配置中心一起发布,但是配置变更一直没有生效。然后经过一番摸索后,拆成了bootstrap.yml和application.yml两个配置文件后,配置动态变更生效了。

Spring Cloud获取数据的时候,其dataId的拼接格式为:${prefix} - ${spring.profiles.active} . ${file-extension}。其中prefix默认为spring.application.name的值,如果配置了多环境,spring.profiles.active即为配置的环境的值。

服务消费者集成

创建一个Spring Cloud项目,即nacos-consumer-spring-cloud。

依赖与上面一致。application.yml配置也与上面一致。

创建启动类

@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerSpringCloudApplication { public static void main(String[] args) {
SpringApplication.run(NacosConsumerSpringCloudApplication.class, args);
} @LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
} @RestController
public class TestController { private final RestTemplate restTemplate; @Autowired
public TestController(RestTemplate restTemplate) {this.restTemplate = restTemplate;} @RequestMapping(value = "/consumer/echo/{str}", method = RequestMethod.GET)
public String echo(@PathVariable String str) {
return restTemplate.getForObject("http://nacos-server-spring-cloud/server/echo/" + str, String.class);
}
}
}

然后将上面的nacos-server-spring-cloud和这个消费服务一起启动。然后访问当前的消费服务,访问/consumer/echo/{str}接口,可以看到请求最终转到了上面的那个服务中。

而我们的请求地址是http://nacos-server-spring-cloud/server/echo/,这就是注册中心的作用,我们不用关注server服务的具体地址,只是请求nacos-server-spring-cloud即可。

Nacos集成学习入门的更多相关文章

  1. 【笔记】集成学习入门之soft voting classifier和hard voting classifier

    集成学习入门之soft voting classifier和hard voting classifier 集成学习 通过构建并结合多个学习器来完成学习任务,一般是先产生一组"个体学习器&qu ...

  2. 使用sklearn进行集成学习——实践

    系列 <使用sklearn进行集成学习——理论> <使用sklearn进行集成学习——实践> 目录 1 Random Forest和Gradient Tree Boosting ...

  3. 使用sklearn进行集成学习——理论

    系列 <使用sklearn进行集成学习——理论> <使用sklearn进行集成学习——实践> 目录 1 前言2 集成学习是什么?3 偏差和方差 3.1 模型的偏差和方差是什么? ...

  4. [转]使用sklearn进行集成学习——理论

    转:http://www.cnblogs.com/jasonfreak/p/5657196.html 目录 1 前言2 集成学习是什么?3 偏差和方差 3.1 模型的偏差和方差是什么? 3.2 bag ...

  5. [转]使用sklearn进行集成学习——实践

    转:http://www.cnblogs.com/jasonfreak/p/5720137.html 目录 1 Random Forest和Gradient Tree Boosting参数详解2 如何 ...

  6. 【Unity游戏开发】SDK接入与集成——小白入门篇

    一.简介 通常一款游戏开发到后期,一般都会涉及到第三方SDK的接入与集成,对于不熟悉SDK接入的同学来说,接SDK每次都是云里雾里,而熟悉SDK接入的同学又觉得不断地重复做接入SDK工作这样没有成就感 ...

  7. Android studio 安装与配置【Android学习入门】

    终于下定决心认真学习Android开发了. 之前在很多平台看到很多大牛们学习Android的经验和心得,纸上得来终觉浅. 这里推荐stormzhang老师总结的Android学习之路. 为了防止电脑卡 ...

  8. 01-Spring Security框架学习--入门(二)

    一.入门案例 Spring Security 自定义登录界面 通过之前的一节 01-Spring Security框架学习--入门(一)的简单演示,Spring security 使用框架自带的登录界 ...

  9. Spring Cloud - Nacos注册中心入门单机模式及集群模式

    近几年微服务很火,Spring Cloud提供了为服务领域的一整套解决方案.其中Spring Cloud Alibaba是我们SpringCloud的一个子项目,是提供微服务开发的一站式解决方案. 包 ...

随机推荐

  1. [从源码学设计]蚂蚁金服SOFARegistry之时间轮的使用

    [从源码学设计]蚂蚁金服SOFARegistry之时间轮的使用 目录 [从源码学设计]蚂蚁金服SOFARegistry之时间轮的使用 0x00 摘要 0x01 业务领域 1.1 应用场景 0x02 定 ...

  2. [水题日常]Luogu1113 杂务

    这几天又在颓. 我的blog难度目前还比较低,主要面向像我这样子的新手(当然内容也会尽量讲得具体清楚一些)x 如果有错误还请指出~ 写完随笔之后才去翻了一下这题题解之后才注意到这题是有序的 QAQ完全 ...

  3. Java篇:Docker的介绍安装 和常用命令

    文章目录 为什么 出现docker Docker的简介 容器(Container) 镜像(Image) 仓库(Repository) Docker的安装 查看容器 删除镜像 删除容器 部署应用 以my ...

  4. 用Wireshark对Android应用的网络流量进行抓包

    通过Wireshark.Charles.Burpsuite等工具分析网络流量的过程,又叫做抓包. 为何需要抓包 测试手机应用(如搜狗号码通.搜狗手机浏览器)的功能时,经常遇到与网络交互的场景,这时候我 ...

  5. 4.自定义view-进度条控件

    1.效果 2.实现原理 画圆,画圆弧,画文字 外部控制进度,通过invalidate()方法更新 核心代码: @Override protected void onDraw(Canvas canvas ...

  6. 跳表(SkipList)设计与实现(Java)

    微信搜一搜「bigsai」关注这个有趣的程序员 文章已收录在 我的Github bigsai-algorithm 欢迎star 前言 跳表是面试常问的一种数据结构,它在很多中间件和语言中得到应用,我们 ...

  7. MALL的学习笔记启动计划

    基本网络文档:http://www.macrozheng.com/#/ 电子书: Spring: <Spring实战(第4版)> Springboot: <Spring Boot实战 ...

  8. bladex从blade-dev.yaml 读取配置信息

    blade-dev.yaml配置======nacos文件配置 #sap配置 sap: api: read: url: http://read.xxxxxxxx.com.cn port: 80 use ...

  9. Spring 之AOP AspectJ切入点语法详解

    记录一下,以后学习 https://blog.csdn.net/zhengchao1991/article/details/53391244

  10. Redis 设计与实现 6:五大数据类型之列表

    列表对象有 3 种编码:ziplist.linkedlist.quicklist. ziplist 和 linkedlist 是 3.2 版本之前的编码. quicklist 是 3.2 版本新增的编 ...