返回
顶部

修改密码

首页 > 文章 > 国内 > 正文
Nacos服务整合SpringBoot框架技术

+1

-1

收藏

+1

-1

点赞0

评论0

标题:Nacos服务整合SpringBoot框架技术
详情介绍-作者:xiaoyi-来源: 极全网 -如有问题点击:在线客服帮助

本文主要面向 Spring Boot 的使用者,通过两个示例来介绍如何使用 Nacos 来实现分布式环境下的配置管理和服务发现。


通过Nacos Server 和 nacos-config-spring-boot-starter 实现配置的动态变更;


通过Nacos Server和nacos-discovery-spring-boot-starter实现服务注册与发现。


前提条件


    您需要先下载 Nacos 并启动 Nacos server。操作步骤参见 [Nacos 快速入门](https://nacos.io/zh-cn/docs/quick-start.html)。


启动配置管理


   启动了 Nacos server 后,您就可以参考以下示例代码,为您的 Spring Boot 应用启动 Nacos 配置管理服务了。完整示例代码请参考:[nacos-spring-boot-config-example](https://github.com/nacos-group/nacos-examples/tree/master/nacos-spring-boot-example/nacos-spring-boot-config-example)


添加依赖。
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>${latest.version}</version>
</dependency>
**       注意**:版本 0.2.x.RELEASE 对应的是 Spring Boot 2.x 版本,版
本 0.1.x.RELEASE 对应的是 Spring Boot 1.x 版本。
   在 application.properties 中配置 Nacos server 的地址:
nacos.config.server-addr=127.0.0.1:8848
使用@NacosPropertySource加载 dataId 为 example 的配置源,并开启自动更新:
@SpringBootApplication
@NacosPropertySource(dataId = "example", autoRefreshed = true)
public class NacosConfigApplication {
public static void main(String[] args) {
    SpringApplication.run(NacosConfigApplication.class, args);
}
}
通过 Nacos 的 @NacosValue 注解设置属性值。
@Controller
@RequestMapping("config")
public class ConfigController {
@NacosValue(value = "${useLocalCache:false}", autoRefreshed = true)
private boolean useLocalCache;
@RequestMapping(value = "/get", method = GET)
@ResponseBody
public boolean get() {
    return useLocalCache;
}
}
    启动 NacosConfigApplication,调用 curl http://localhost:8080/config/get,返回 
内容是 false。
    通过调用 [Nacos Open API](https://nacos.io/zh-cn/docs/open-API.html) 向 Nacos server 发布配置:dataId 为example,内容
为useLocalCache=true
curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configsdataId=example&group=DEFAULT_GROUP&content=useLocalCache=true" 
再次访问 http://localhost:8080/config/get,此时返回内容为true,说明程序中的useLocalCache值已经被动态更新了。 
启动服务发现
    本节演示如何在您的 Spring Boot 项目中启动 Nacos 的服务发现功能。完整示例代码请参考:[nacos-spring-boot-discovery-example](https://github.com/nacos-group/nacos-examples/tree/master/nacos-spring-boot-example/nacos-spring-boot-discovery-example) 
添加依赖。
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-discovery-spring-boot-starter</artifactId>
<version>${latest.version}</version>
</dependency>
注意:版本 0.2.x.RELEASE 对应的是 Spring Boot 2.x 版本,版本 0.1.x.RELEASE 对应的是 Spring Boot 1.x 版本。
在 application.properties 中配置 Nacos server 的地址:
nacos.discovery.server-addr=127.0.0.1:8848
使用 @NacosInjected 注入 Nacos 的 NamingService 实例:
@Controller
@RequestMapping("discovery")
public class DiscoveryController {
@NacosInjected
private NamingService namingService;
@RequestMapping(value = "/get", method = GET)
@ResponseBody
public List<Instance> get(@RequestParam String serviceName) throws NacosException {
    return namingService.getAllInstances(serviceName);
}
}
@SpringBootApplication
public class NacosDiscoveryApplication {
public static void main(String[] args) {
    SpringApplication.run(NacosDiscoveryApplication.class, args);
}
}



   启动 NacosDiscoveryApplication,调用 curl http://localhost:8080/discovery/getserviceName=example,此时返回为空 JSON 数组[]。


  通过调用 [Nacos Open API](https://nacos.io/zh-cn/docs/open-API.html) 向 Nacos server 注册一个名称为 example 服务


curl -X PUT 'http://127.0.0.1:8848/nacos/v1/ns/instanceserviceName=example&ip=127.0.0.1&port=8080' 
  再次访问 curl http://localhost:8080/discovery/getserviceName=example,此时返回内容为: 
[
{
"instanceId": "127.0.0.1-8080-DEFAULT-example",
"ip": "127.0.0.1",
"port": 8080,
"weight": 1.0,
"healthy": true,
"cluster": {
  "serviceName": null,
  "name": "",
  "healthChecker": {
    "type": "TCP"
  },
  "defaultPort": 80,
  "defaultCheckPort": 80,
  "useIPPort4Check": true,
  "metadata": {}
},
"service": null,
"metadata": {}
}
]


版权声明:本文内容由极全网实名注册用户自发贡献,版权归原作者所有,极全网-官网不拥有其著作权,亦不承担相应法律责任。具体规则请查看《极全网用户服务协议》和《极全网知识产权保护指引》。如果您发现极全网中有涉嫌抄袭的内容,点击进入填写侵权投诉表单进行举报,一经查实,极全网将立刻删除涉嫌侵权内容。

扫一扫在手机打开

评论
已有0条评论
0/150
提交
热门评论
相关推荐
换一批
热点排行