• nacos安装配置和单机部署教程


    首先说明:次教程是针对的nacos版本:1.3.2,不同的版本,可能略有不同。

    Springcloudalibaba的版本与Springboot的版本与nacos版本需要对应

    Nacos支持三种部署模式

    • 单机模式 - 用于测试和单机试用。
    • 集群模式 - 用于生产环境,确保高可用。
    • 多集群模式 - 用于多数据中心场景。

    单机部署:

    1、首先去官网下载nacos

    Releases · alibaba/nacos · GitHub

    默认是集群启动的,可以修改为单机启动模式:

    Windows启动脚本命令:

    startup.cmd -sstandalone

    nacos初体验

    父maven的版本依赖

    
    
        4.0.0
        
            order-nacos
            stock-nacos
        
        
            org.springframework.boot
            spring-boot-starter-parent
            2.3.2.RELEASE
             
        
        com.example.springcloudalibaba
        demo
        0.0.1-SNAPSHOT
        springcloudalibabaDemo
        Demo project for Spring springcloudalibaba
    
        
            1.8
            2.2.5.RELEASE
            2.3.2.RELEASE
            Hoxton.SR8
        
    
        
            
                org.springframework.boot
                spring-boot-starter
            
    
            
                org.springframework.boot
                spring-boot-starter-test
                test
            
        
    
        
            
            
                
                    com.alibaba.cloud
                    spring-cloud-alibaba-dependencies
                    ${spring.cloud.alibaba.version}
                    pom
                    import
                
                
                
                    org.springframework.boot
                    spring-boot-starter-parent
                    ${sping.boot.version}
                    pom
                    import
                
                
                
                    org.springframework.cloud
                    spring-cloud-dependencies
                    ${spring.cloud.version}
                    pom
                    import
                
            
    
        
        
        
            
                
                    org.springframework.boot
                    spring-boot-maven-plugin
                
            
        
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80

    在每个子模块中引入nacos依赖

    
            
                org.springframework.boot
                spring-boot-starter-web
            
            
            
                com.alibaba.cloud
                spring-cloud-starter-alibaba-nacos-discovery
            
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    每个子模块中配置nacos,端口不同

    server:
      port: 8021
    #nacos配置
    # 应用名称,nacos会将该名称当做服务名称
    spring:
      application:
        name: stock-service
      cloud:
        nacos:
          server-addr: 127.0.0.1:8848
          discovery:
            username: nacos
            password: nacos #登录nacos可视化界面的用户名和密码
            namespace: public #命名空间
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    配置好nacos后会将服务自动注册

    nacos的服务调用依赖负载均衡器,默认是ribbon。在消费者端配置restTemplate用于调用提供者端的服务接口

    package per;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.client.RestTemplateBuilder;
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;
    
    @SpringBootApplication
    public class OrderApplication {
        public static void main(String[] args) {
            SpringApplication.run(OrderApplication.class,args);
        }
    
        @Bean
        @LoadBalanced // 负载均衡器注解,nacos的服务调用依赖于负载均衡(nacos无法将服务名称转化为服务地址,需要使用负载均衡器,默认使用轮询的方式)
        public RestTemplate restTemplate(RestTemplateBuilder builder){
            RestTemplate RestTemplate = builder.build();
            return RestTemplate;
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    编写服务提供者

    package per.pz.controller;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("stock")
    public class stockController {
        //读取配置文件中的端口
        @Value("${server.port}")
        String port;
        @RequestMapping("/reduct")
        public String reduct(){
            System.out.println("扣减库存");
            return "扣减库存111"+port;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    编写服务消费者

    package per.pz.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    @RestController
    @RequestMapping("order")
    public class oderController {
        @Autowired
        RestTemplate RestTemplate;
    
        @RequestMapping("/add")
        public String add(){
            System.out.println("下单成功");
    //       服务间的通讯,没有使用微服务的方式
    //        方法的缺点是在代码中写死服务地址,所以使用注册中心解决
    //        String msg = RestTemplate.getForObject("http://localhost:8021/stock/reduct",String.class);
    //        nacos服务调用,无需使用地址,使用服务名称即可
            String msg = RestTemplate.getForObject("http://stock-service/stock/reduct",String.class);
            return "下单成功111"+msg;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦

  • 相关阅读:
    【漏洞复现】EnjoySCM存在文件上传漏洞
    自动化测试系列 —— UI自动化测试
    1.4_12 Axure RP 9 for mac 高保真原型图 - 案例11 【动态面板-滚动条1】
    ResNet网络架构
    Python快速刷题网站——牛客网 数据分析篇(八)
    一文带你弄懂 JVM 三色标记算法!
    Flutter 应用加速之本地缓存管理
    包管理工具--》其他包管理器之cnpm、pnpm、nvm
    0003Java安卓程序设计-springboot基于Android的学习生活交流APP
    【文末附gpt升级方案】数据虚拟化技术的优势
  • 原文地址:https://blog.csdn.net/m0_67402564/article/details/126113674