6.Cloud-Nacos 2.x

本文详细介绍了Nacos的单机部署、作为服务注册中心在支付和订单项目中的使用、配置中心功能以及集群配置,包括Windows服务部署步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.单机部署

  1.1 官网

https://nacos.io/zh-cn/index.html
https://github.com/alibaba/Nacos

  1.2.版本说明

https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E
Spring Cloud Alibaba VersionSpring Cloud VersionSpring Boot Version

2.2.10-RC1*

Spring Cloud Hoxton.SR12

2.3.12.RELEASE

2.2.9.RELEASE

Spring Cloud Hoxton.SR12

2.3.12.RELEASE

2.2.8.RELEASE

Spring Cloud Hoxton.SR12

2.3.12.RELEASE

2.2.7.RELEASE

Spring Cloud Hoxton.SR12

2.3.12.RELEASE

2.2.6.RELEASE

Spring Cloud Hoxton.SR9

2.3.2.RELEASE

Nacos 版本关系, 2.2.6.RELEASE是1.x版本及以下,单机没体现主要是集群需要配置grpc端口号

  1.3.下载地址

https://github.com/alibaba/nacos/releases/tag/2.2.0

  1.4.安装

    解压安装包,直接运行bin目录下,默认账号密码都是nacos,命令运行成功后直接访问http://localhost:8848/nacos,结果页面

startup.cmd -m standalone

  1.5 mysql为存储

        修改配置文件application.properties

spring.datasource.platform=mysql

### Count of DB:
db.num=1

### Connect URL of DB:
db.url.0=jdbc:mysql://192.168.2.18:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user.0=root
db.password.0=root

### Connection pool configuration: hikariCP
db.pool.config.connectionTimeout=30000
db.pool.config.validationTimeout=10000
db.pool.config.maximumPoolSize=20
db.pool.config.minimumIdle=2

    startup.cmd 启动即可

2.做为服务注册中心

    2.1 支付项目

    1) 父POM

    2.2.6.RELEASE是2.x和1.x版本的分界线,2集群时必须配置grpc的端口映射

 <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.10-RC1</version>
                <type>pom</type>
                <scope>import</scope>
</dependency>

    2) POM

 <!--nacos-->
<dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

    3) YML

server:
  port: 9001

spring:
  application:
    name: nacos-payment-provider
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #配置Nacos地址
 

    4) 主程序

@EnableDiscoveryClient
@SpringBootApplication
public class PaymentMain9001 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain9001.class,args);
    }
}

  2.2 订单项目

    1) POM

 <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

    2) YML

spring:
  application:
    name: order-nacos-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

    3) 主程序

@EnableDiscoveryClient
@SpringBootApplication
public class OrderNacosMain {

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

    4) 业务类

        ApplicationContextBean

@Configuration
public class ApplicationContextBean {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate()
    {
        return new RestTemplate();
    }
}

       OrderNacosController

@RestController
public class OrderController {

    @Resource
    private RestTemplate restTemplate;

    private String serverURL = "http://nacos-payment-service";

    @GetMapping(value = "/payment/get/{id}")
    public String paymentInfo(@PathVariable("id") Long id)
    {
        return restTemplate.getForObject(serverURL+"/payment/get/"+id,String.class);
    }

}

3.做为参数配置中心

  3.1 POM

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

  3.2 YOM

     1) bootstrap.yml

        此文件不写,yaml格式无法加载

spring:
  profiles:
    active: dev
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
      config:
        server-addr: localhost:8848
        file-extension: yaml

    2) application.yml

server:
  port: 8001
spring:
  application:
    name: nacos-payment-service

  3.3 业务类


@RestController
@RefreshScope
public class ConfigClientController
{
    @Value("${config.info}")
    private String configInfo;

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

 通过 Spring Cloud 原生注解@RefreshScope  实现配置自动更新

  3.4 Nacos中添加配置信息

    1) 配置规格

${prefix}-${spring.profiles.active}.${file-extension}

    2) 说明

  • prefix 默认为 spring.application.name 的值,也可以通过配置项 spring.cloud.nacos.config.prefix来配置
  • spring.profiles.active 即为当前环境对应的 profile
  • file-exetension 为配置内容的数据格式,可以通过配置项 spring.cloud.nacos.config.file-extension 来配置,官网上说目前只支持 properties 和 yaml 类型,但我的版本只有properties 成功过

    3) 配置新增

4.集群配置

  4.1 单机配置

   修改配置配置文件cluster.conf

192.168.2.93:8840
192.168.2.93:8850
192.168.2.93:8860

   4.2 nginx 配置

tream{
   upstream nacos-cluster-tcp{
	   server 192.168.2.18:9840;
	   server 192.168.2.18:9850;
	   server 192.168.2.18:9860;
  }
  
  server{
	listen 9848;
	proxy_pass nacos-cluster-tcp;
  }
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
	
	upstream nacos-cluster{
	  server 192.168.2.18:8840;
	  server 192.168.2.18:8850;
	  server 192.168.2.18:8860;
	}
 
    server {
        listen       8848;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
		
		location /nacos {	
	        proxy_pass http://nacos-cluster;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 
    }

}

   4.3 程序bootstrap.yml

spring:
  profiles:
    active: dev
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.2.18:8848
      config:
        server-addr: 192.168.2.18:8848
        file-extension: yaml

5.windows服务部署

  5.1 WinSW相关详见,这里只说关键

https://blog.youkuaiyun.com/wang_peng/article/details/136066840

  5.2 修改startup.cmd

set MODE="standalone"

  5.3 nacos-service.xml内容

<service>
  
  <!-- ID of the service. It should be unique across the Windows system-->
  <id>nacos-service</id>
  
  <!-- 服务名 -->
  <name>nacos-service</name>
  
  <!-- 服务说明 -->
  <description>This service is a service nacos-service</description>
  
  <!-- 应启动的可执行文件的路径 -->
  <executable>startup.cmd</executable>
   
  <!-- 服务启动模式 Automatic:自动 -->
  <startmode>Automatic</startmode>
  
  <!-- 日志位置,为当前路径logs文件夹下 -->
  <logpath>logs</logpath>
  
</service>

  5.4 install.bat文件内容

cd %~dp0
nacos-service install
nacos-service start
pause

  5.5 unstall.bat文件内容

cd %~dp0
nacos-service stop
nacos-service uninstall
pause

  5.6 最后目录结构

  5.7 安装执行install.bat,

       我看网上有些帖子说按这思路失败了,那就得分析具体问题,反正我这成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wang_peng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值