• spring cloud alibaba nacos搭建最小可运行微服务


    一、环境

    开发工具:IntelliJ Idea
    JDK 1.8
    Spring boot 2.3.12.RELEASE
    spring cloud Alibaba 2.2.7.RELEASE
    openfeign 2.2.9.RELEASE

    二、程序目录

    可以通过开发工具中的maven、spring initializr等进行项目创建。内容包括:父工程、两个子工程。结构如下图:
    在这里插入图片描述

    ①父工程,该工程仅是pom工程,向子工程提供pom的继承。
    ②子工程,用于两个服务之间的调用

    • 工程说明:
    • order服务通过restTemplate和openfeign两种方式分别调用stock服务中的reduct接口

    三、配置文件

    • 3.1 父POM文件


      4.0.0

      com.jwssw
      microservice
      0.0.1-SNAPSHOT
      microservice
      microservice
      pom
      
      
          order
          stock
      
      
      
          org.springframework.boot
          spring-boot-starter-parent
          2.3.12.RELEASE
           
      
      
      
          8
          Hoxton.SR12
          2.2.7.RELEASE
      
      
      
          
              
              
                  org.springframework.cloud
                  spring-cloud-dependencies
                  ${spring.cloud.version}
                  pom
                  import
              
              
              
                  com.alibaba.cloud
                  spring-cloud-alibaba-dependencies
                  ${alibaba.cloud.version}
                  pom
                  import
              
          
      
      
      
      
          
              org.springframework.boot
              spring-boot-starter
          
          
          
              org.springframework.boot
              spring-boot-starter-web
          
          
          
              com.alibaba.cloud
              spring-cloud-starter-alibaba-nacos-discovery
          
          
              com.alibaba.cloud
              spring-cloud-starter-alibaba-nacos-config
          
          
          
              org.springframework.cloud
              spring-cloud-starter-openfeign
          
          
          
              org.springframework.boot
              spring-boot-starter-test
              test
          
      
      
      
      
          
              
                  org.springframework.boot
                  spring-boot-maven-plugin
                  
                      
                          
                              org.projectlombok
                              lombok
                          
                      
                  
              
          
      
      
      • 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
      • 81
      • 82
      • 83
      • 84
      • 85
      • 86
      • 87
      • 88
      • 89
      • 90
      • 91
      • 92
      • 93
      • 94
      • 95
      • 96
    • 3.2 子工程POM,除artifactId外两个子工程配置一致




      microservice
      com.jwssw
      0.0.1-SNAPSHOT

      4.0.0

      order
      
      
      
          8
          8
      
      
      
      
      
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
    • 3.3 application.yml配置文件,除application.name外两个子工程配置一致

      server:
      port: 8011

      spring 相关配置

      spring:
      application:
      name: order-server # 服务名称
      cloud:
      # nacos客户端配置
      nacos:
      discovery:
      server-addr: 192.168.0.182:8848 # nacos服务地址

      feign配置

      feign:

      是否开启服务降级,默认不开启,true表示开启

      hystrix:
      enabled: true

    四、程序代码

    4.1 Order子工程的相关代码
    • 4.1.1 启动类OrderApplication

      package com.jwssw.order;

      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.cloud.openfeign.EnableFeignClients;
      import org.springframework.context.annotation.Bean;
      import org.springframework.web.client.RestTemplate;

      /**

      • 类描述:启动类

      • @author 鲁浩鹏 Lu Haopeng

      • @version 1.0

      • @email Lu Haopeng

      • @date 2022/3/19 10:42

      • @since JDK 8
        /
        @SpringBootApplication
        @EnableFeignClients
        public class OrderApplication {
        /
        *

        • 方法描述: 启动类入口
        • @param args 参数
        • @author 鲁浩鹏 Lu Haopeng
        • @date 2022/3/25 13:50
          */
          public static void main(String[] args) {
          SpringApplication.run(OrderApplication.class, args);
          }

        /**

        • 方法描述: 装载restTemplate bean对象
        • @param builder 配置类
        • @return {@link RestTemplate}
        • @author 鲁浩鹏 Lu Haopeng
        • @date 2022/3/25 13:51
          */
          @LoadBalanced
          @Bean
          public RestTemplate restTemplate(RestTemplateBuilder builder) {
          return builder.build();
          }
          }
    • 4.1.2 控制类OrderController

      package com.jwssw.order;

      import com.jwssw.order.service.OrderService;
      import org.springframework.beans.factory.annotation.Qualifier;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RestController;
      import org.springframework.web.client.RestTemplate;

      /**

      • 类描述:对外提供服务类

      • @author 鲁浩鹏 Lu Haopeng

      • @version 1.0

      • @email Lu Haopeng

      • @date 2022/3/19 10:43

      • @since JDK 8
        /
        @RestController
        @RequestMapping(“/order”)
        public class OrderController {
        /
        * restTemplate远程调用对象 /
        private final RestTemplate restTemplate;
        /
        * openfeign远程调用对象 */
        private final OrderService orderService;

        /**

        • 构造方法注入
          */
          public OrderController(RestTemplate restTemplate,
          @Qualifier(“com.jwssw.order.service.OrderService”) OrderService orderService) {
          this.restTemplate = restTemplate;
          this.orderService = orderService;
          }

        /**

        • 方法描述: 采用restTemplate方式远程调用
        • @author 鲁浩鹏 Lu Haopeng
        • @date 2022/3/25 14:11
          */
          @RequestMapping(“/add”)
          public String add() {
          // 远程调用
          String str = restTemplate.getForObject(“http://stock-server/stock/reduct”, String.class);
          // 返回结果
          return "Hello World " + str;
          }

        /**

        • 方法描述: 采用openfeign方式远程调用
        • @return {@link String}
        • @author 鲁浩鹏 Lu Haopeng
        • @date 2022/3/25 14:12
          */
          @RequestMapping(“/feignAdd”)
          public String feignAdd() {
          // openfeign调用远程接口
          String str = orderService.reduct();
          // 返回结果
          return “feign调用:” + str;
          }
          }
    • 4.1.3 openfeign接口类OrderService

      package com.jwssw.order.service;

      import com.jwssw.order.service.impl.OrderServiceImpl;
      import org.springframework.cloud.openfeign.FeignClient;
      import org.springframework.web.bind.annotation.GetMapping;

      /**

      • 类描述:openfeign远程调用接口类
      • @author 鲁浩鹏 Lu Haopeng
      • @version 1.0
      • @email Lu Haopeng
      • @date 2022/3/24 09:53
      • @since JDK 8
        /
        @FeignClient(name = “stock-server”, fallback = OrderServiceImpl.class)
        public interface OrderService {
        /
        *
        • 方法描述: 调用stock服务的reduct接口的方法
        • @return {@link String}
        • @author 鲁浩鹏 Lu Haopeng
        • @date 2022/3/25 14:23
          */
          @GetMapping(“/stock/reduct”)
          String reduct();
          }
    • 4.1.4 回调类OrderServiceImpl

      package com.jwssw.order.service.impl;

      import com.jwssw.order.service.OrderService;
      import org.springframework.stereotype.Component;

      /**

      • 类描述:openfeign远程调用失败的回调类
      • @author 鲁浩鹏 Lu Haopeng
      • @version 1.0
      • @email Lu Haopeng
      • @date 2022/3/24 09:54
      • @since JDK 8
        /
        @Component
        public class OrderServiceImpl implements OrderService {
        /
        *
        • 方法描述: 调用失败后的回调方法
        • @return {@link String}
        • @author 鲁浩鹏 Lu Haopeng
        • @date 2022/3/25 14:23
          */
          @Override
          public String reduct() {
          return “库存服务不可达”;
          }
          }
    4.2 stock子工程的相关代码
    • 4.2.1 启动类StockApplication

      package com.jwssw.stock;

      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;

      /**

      • 类描述:stock项目启动类
      • @author 鲁浩鹏 Lu Haopeng
      • @version 1.0
      • @email Lu Haopeng
      • @date 2022/3/19 10:52
      • @since JDK 8
        /
        @SpringBootApplication
        public class StockApplication {
        /
        *
        • 方法描述: stock服务启动入口
        • @param args 参数
        • @author 鲁浩鹏 Lu Haopeng
        • @date 2022/3/25 14:27
          */
          public static void main(String[] args) {
          SpringApplication.run(StockApplication.class, args);
          }
          }
    • 4.2.2 对外提供服务类StockController

      package com.jwssw.stock;

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

      /**

      • 类描述:对外提供服务类

      • @author 鲁浩鹏 Lu Haopeng

      • @version 1.0

      • @email Lu Haopeng

      • @date 2022/3/19 10:46

      • @since JDK 8
        */
        @RestController
        @RequestMapping(“/stock”)
        public class StockController {

        /** 获取配置文件中的端口号 */
        @Value(“${server.port}”)
        private String port;

        /**

        • 方法描述: 对外提供的方法
        • @return {@link String}
        • @author 鲁浩鹏 Lu Haopeng
        • @date 2022/3/25 14:29
          */
          @RequestMapping(“/reduct”)
          public String reduct() {
          System.out.println(“扣减库存成功”);
          return “扣减库存” + port;
          }
          }

    五、总结

    以上内容主要给予spring cloud alibaba的最小可运行的微服务Demo工程,如果你在实际项目中尚未微服务,不妨可以采用本文上述方式在实际项目中推行一下。

  • 相关阅读:
    容易被忽视的CNN模型的感受野及其计算
    数据结构与算法课程设计:基于交通路线的规划系统
    Vue3.0 如何写自定义指令
    P1-P5_动手学深度学习-pytorch(李沐版,粗浅的笔记)
    组件库都在使用CSS变量了
    Django ModelForm中使用钩子函数校验数据
    专访通过 OBCP V3 首位考生:V3 让知识更加结构化、体系化
    基于Java+SpringBoot+Vue前后端分离库存管理系统设计和实现
    概率图模型在自然语言处理中的应用
    社群运营怎么做,有哪些互动玩法?
  • 原文地址:https://blog.csdn.net/m0_54864585/article/details/126496768