nacos

nacos

为什么使用服务注册中心配置中心

注册中心解决了服务发现的问题 (而且可以是一个服务多个实例)

配置中心就是一种统一管理各种应用配置的基础服务

naocos是什么?

官网描述:一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。

简洁:服务注册配置中心

前四个字母分别为Naming和Configuration的前两个字母,最后的s为Service

在这里插入图片描述

可以调整切换为CP,我们一般用默认AP即可 (CAP理论)

怎么用?—服务注册中心

先下载nacos 然后启动nacos 默认端口8848

  1. 加依赖
  2. 改配置
  3. 写业务代码

1加依赖

<!--nacos-discovery-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>   服务注册中心
        </dependency>

<!--loadbalancer-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>  负载均衡
        </dependency>

2改配置

加载顺序

  • bootstrap.yml -->去注册到nacos
  • 加载application.yml -->然后根据application.yml的active加载远程配置文件,或者匹配的本地文件(local,dev,等等)
  • 加载application-local.yml(如果active: local)

这里注意优先级,优先级是跟加载顺序相反,即application.name+active+file-extension的优先级最高,后面的配置会覆盖前面的

bootstrap.yml

spring:
  cloud:
    nacos:
      discovery:
        server-addr: nacos.ilaw.com.cn:8848
      config:
        server-addr: nacos.ilaw.com.cn:8848
        file-extension: yml
         #消费者将要去访问的微服务名称(nacos微服务提供者叫什么你写什么)
service-url:
  nacos-user-service: http://nacos-payment-provider   
        

application.yml

spring:
  profiles:
    active: local
  application:
    name: demo

application-local.yml

env: local
actuator:
  access:
    ip: 127.0.0.1
spring:
  application:
    name: kinglex-service

3业务代码

消费者:

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfig
{
    @Bean
    @LoadBalanced //赋予RestTemplate负载均衡的能力
    public RestTemplate restTemplate()
    {
        return new RestTemplate();
    }
}
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class OrderNacosController
{
    @Resource
    private RestTemplate restTemplate;
 
    @Value("${service-url.nacos-user-service}")  //这个其实不从配置文件读取也行  http://abc..com.cn/XXX  也可以
    private String serverURL;
 
    @GetMapping("/consumer/pay/nacos/{id}")
    public String paymentInfo(@PathVariable("id") Integer id)
    {
        String result = restTemplate.getForObject(serverURL + "/pay/nacos/" + id, String.class);
        return result+"\t"+"    我是OrderNacosController83调用者。。。。。。";
    }
}

生产者:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class PayAlibabaController
{
    @Value("${server.port}")
    private String serverPort;

    @GetMapping(value = "/pay/nacos/{id}")
    public String getPayInfo(@PathVariable("id") Integer id)
    {
        return "nacos registry, serverPort: "+ serverPort+"\t id"+id;
    }
}

怎么用?—配置中心

  1. 加依赖
  2. 改配置
  3. 写业务代码

1加依赖

        <!--bootstrap-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId> 对于bootstrap.properties/bootstrap.yaml配置文件的支持
        </dependency>
        <!--nacos-config-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> 配置中心
        </dependency>

扩展SpringCloud-Config-Client配置文件为什么一定要是bootstrap.yml或者bootstrap.properties

2改配置

bootstrap.yml

# nacos配置
spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #Nacos服务注册中心地址
      config:
        server-addr: localhost:8848 #Nacos作为配置中心地址
        file-extension: yaml #指定yaml格式的配置

# nacos端配置文件DataId的命名规则是:
# ${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}
# 本案例的DataID是:nacos-config-client-dev.yaml

application.yml

server:
  port: 3377
spring:
  profiles:
    active: dev # 表示开发环境
       #active: prod # 表示生产环境
       #active: test # 表示测试环境

nacos-config-client-dev.yaml

config:
 	info:XXX

在这里插入图片描述

3业务代码

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @auther zzyy
 * @create 2023-11-27 15:51
 */
@RestController
@RefreshScope //在控制器类加入@RefreshScope注解使当前类下的配置支持Nacos的动态刷新功能。
public class NacosConfigClientController
{
    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/config/info")
    public String getConfigInfo() {
        return configInfo;
    }
}

4配置的多环境多项目管理

  • Namespace
  • Group
  • Service / Datald

个人理解:3个为一组确定一个配置文件 方便管理 (或者2个或者1个 因为Group Namespace 有默认值)

# nacos配置 第3种:新建空间+新建分组+新建DataID
spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 
      config:
        server-addr: localhost:8848
        file-extension: yaml 
        group: PROD_GROUP                    #指定分组
               namespace: Prod_Namespace        #指定空间
  • 17
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值