其他分享
首页 > 其他分享> > [SOA] Nacos 服务注册中心探秘(一)

[SOA] Nacos 服务注册中心探秘(一)

作者:互联网

目录

[SOA] Nacos 服务注册中心探秘(一)

手机用户请横屏获取最佳阅读体验,REFERENCES中是本文参考的链接,如需要链接和更多资源,可以关注其他博客发布地址。

平台 地址
CSDN https://blog.csdn.net/sinat_28690417
简书 https://www.jianshu.com/u/3032cc862300
个人博客 http://xiazhaoyang.tech/

介绍

什么是Nacos ?

Nacos 是构建以“服务”为中心的现代应用架构 (例如微服务范式、云原生范式) 的服务基础设施。

Nacos 能做什么?

Nacos的特性一览

Nacos概念

安装

参考官网引导

快速上手 >>> Here !

新版本有登录拦截界面,登录账户和密码为:nacos/nacos

界面访问路径:http://127.0.0.1:8848/nacos

启动

Linux/Unix/Mac

启动命令(standalone代表着单机模式运行,非集群模式):

sh startup.sh -m standalone

在这里插入图片描述

服务注册、发现和配置管理

curl -X PUT 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=nacos.naming.serviceName&ip=20.18.7.10&port=8080'
curl -X GET 'http://127.0.0.1:8848/nacos/v1/ns/instances?serviceName=nacos.naming.serviceName'
curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test&content=HelloWorld"
curl -X GET "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test"

服务注册&服务发现

在这里插入图片描述

在这里插入图片描述

发布配置&获取配置

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

Spring Boot 开发

项目搭建

buildscript {
   repositories {
       maven { url = 'https://maven.aliyun.com/repository/jcenter' }
       maven { url = "https://plugins.gradle.org/m2/" }
       maven { url = "http://maven.aliyun.com/nexus/content/groups/public/" }
       jcenter()
   }
   dependencies {
       classpath libs["spring-boot-gradle-plugin"]
   }
}

apply plugin: "idea"
apply plugin: "java"
apply plugin: "io.spring.dependency-management"
apply plugin: "org.springframework.boot"

group = "com.example"
version = "1.0.0-SNAPSHOT"
sourceCompatibility = 1.8

dependencies {

   annotationProcessor libs['lombok']
   compileOnly libs['lombok']
   testAnnotationProcessor libs['lombok']
   testCompileOnly libs['lombok']

   compile "org.springframework.boot:spring-boot-starter-web"

   compile "org.springframework.boot:spring-boot-starter-actuator"

   compile libs["nacos-config-spring-boot-starter"]

   compile libs["nacos-config-spring-boot-actuator"]

   testCompile "org.springframework.boot:spring-boot-starter-test"
}

libs中版本信息

//...
ver.nacos = [
        nacos_config_spring_boot_starter:"0.2.1"
]
//...
libs{
//Nacos
"nacos-config-spring-boot-starter" : "com.alibaba.boot:nacos-config-spring-boot-starter:$ver.nacos.nacos_config_spring_boot_starter",
"nacos-config-spring-boot-actuator" : "com.alibaba.boot:nacos-config-spring-boot-actuator:$ver.nacos.nacos_config_spring_boot_starter"    
}

CapsuleNacosConfigApplication

package com.example.nacos;

import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Document: https://nacos.io/zh-cn/docs/quick-start-spring-boot.html
 * <p>
 * Nacos 控制台添加配置:
 * <p>
 * Data ID:nacos.cfg.dataId
 * <p>
 * Group:test
 * <p>
 * 配置内容:useLocalCache=true
 */
@SpringBootApplication
//此处需要注意:groupId默认是DEFAULT_GROUP,官网教程用的是默认分组名,如果自定义分组的话此处不配置估计会踩坑哦!
@NacosPropertySource(dataId = "nacos.cfg.dataId",groupId = "test",autoRefreshed = true)
public class CapsuleNacosConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(CapsuleNacosConfigApplication.class, args);
    }

}

NacosConfigController

/*
 * @ProjectName: 编程学习
 * @Copyright:   2019 HangZhou xiazhaoyang Dev, Ltd. All Right Reserved.
 * @address:     http://xiazhaoyang.tech
 * @date:        2019/2/28 22:02
 * @email:       xiazhaoyang@live.com
 * @description: 本内容仅限于编程技术学习使用,转发请注明出处.
 */
package com.example.nacos.controller;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * <p>
 *
 * </p>
 *
 * @author xiazhaoyang
 * @version v1.0.0
 * @date 2019/2/28 22:02
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify By: {修改人} 2019/2/28
 * @modify reason: {方法名}:{原因}
 * ...
 */
@RestController
@RequestMapping("/configService")
public class NacosConfigController {

    @NacosValue(value = "${useLocalCache:false}", autoRefreshed = true)
    private boolean useLocalCache;

    @RequestMapping(value = "/get", method = GET)
    public boolean get() {
        return useLocalCache;
    }
}

yaml

nacos:
  config:
    server-addr: 127.0.0.1:8848

server:
  servlet:
    context-path: /capsule-nacos-config
  port: 8080

除此之外还可以配置:

# 在命名空间详情处可以获取到 endpoint 和 namespace 的值
nacos.config.endpoint=${endpoint}
nacos.config.namespace=${namespace}
# 推荐使用 RAM 账户的 accessKey 和 secretKey
nacos.config.access-key=${accessKey}
nacos.config.secret-key=${secretKey}

配置信息获取测试

curl http://localhost:8080/capsule-nacos-config/configService/get
false
curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test&content=useLocalCache=true"

在这里插入图片描述

在这里插入图片描述

REFRENCES


更多

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

扫码关注或搜索架构探险之道获取最新文章,不积跬步无以至千里,坚持每周一更,坚持技术分享^_^

标签:SOA,服务,config,配置,boot,Nacos,nacos,探秘
来源: https://blog.51cto.com/u_15263565/2896370