• 01_SpringCloud Alibaba环境搭建


    SpringCloud Alibaba环境搭建

    搭建分布式架构

    1.新建springboot项目

    在这里插入图片描述

    父项目pom依赖加上

    <packaging>pompackaging>
    <properties>
        <java.version>1.8java.version>
    properties>
    
    • 1
    • 2
    • 3
    • 4

    创建两个子项目

    1.order

    添加pom依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
    dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    application.yml

    server:
      port: 8080
    
    • 1
    • 2

    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 OrderController {
        @Autowired
        RestTemplate restTemplate;
    
        @RequestMapping("/add")
        public String add(){
            System.out.println("下单成功!");
            String msg = restTemplate.getForObject("http://localhost:8081/stock/reduct", String.class);
            return "hello world"+msg;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    启动类

    @SpringBootApplication
    public class OrderApplication {
        public static void main(String[] args) {
            SpringApplication.run(OrderApplication.class,args);
        }
    
    
        @Bean
        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

    2.stock

    添加pom依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
    dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    application.yml

    server:
      port: 8081
    
    • 1
    • 2

    controller层

    @RestController
    @RequestMapping("/stock")
    public class StockController {
        @RequestMapping("/reduct")
        public String reduct(){
            System.out.println("扣减库存");
            return "扣减库存";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    启动类

    @SpringBootApplication
    public class StockApplication {
        public static void main(String[] args) {
            SpringApplication.run(StockApplication.class,args);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    访问 http://localhost:8080/order/add

    出现 hello world扣减库存

    思考 String msg = restTemplate.getForObject(“http://localhost:8081/stock/reduct”, String.class);

    需要维护ip地址 麻烦

    SpringCloud Alibaba 环境搭建

    版本对应很重要

    Spring Cloud Alibaba:2.2.5.RELEASES

    pring Boot :2.3.2.RELEASE

    Spring Cloud:Hoxton.SR8

    修改父工程的pom.xml

    • 添加统一版本
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
        <modules>
            <module>stockmodule>
            <module>ordermodule>
        modules>
    
        <groupId>com.tian.springcloudgroupId>
        <artifactId>springcloudalibabaartifactId>
        <version>0.0.1-SNAPSHOTversion>
        <name>springcloudalibabaname>
        <description>SpringCloud Alibabadescription>
        <packaging>pompackaging>
        <properties>
            <java.version>1.8java.version>
            <spring.cloud.alibaba.version>2.2.5.RELEASEspring.cloud.alibaba.version>
            <spring.boot.version>2.3.7.RELEASEspring.boot.version>
            <spring.cloud.version>Hoxton.SR8spring.cloud.version>
        properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starterartifactId>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
            dependency>
        dependencies>
    
        <dependencyManagement>
    
            <dependencies>
                <dependency>
                    <groupId>com.alibaba.cloudgroupId>
                    <artifactId>spring-cloud-alibaba-dependenciesartifactId>
                    <version>${spring.cloud.alibaba.version}version>
                    <type>pomtype>
                    <scope>importscope>
                dependency>
    
                <dependency>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-parentartifactId>
                    <version>${spring.boot.version}version>
                    <type>pomtype>
                    <scope>importscope>
                dependency>
    
                <dependency>
                    <groupId>org.springframework.cloudgroupId>
                    <artifactId>spring-cloud-dependenciesartifactId>
                    <version>${spring.cloud.version}version>
                    <type>pomtype>
                    <scope>importscope>
                dependency>
            dependencies>
        dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                plugin>
            plugins>
        build>
    
    project>
    
    
    • 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

  • 相关阅读:
    力扣名企直通车-学习计划-网易-刷题记录
    通过WSL2 Ubuntu18.04搭建CANN算子开发环境
    计算机视觉的发展历程
    Web前端vueDemo—实现图片切换功能(二)
    chapter4——时钟分频器
    设计模式学习(二十):备忘录模式
    Ceph(L版本)部署及相关概念
    《Jetpack Compose从入门到实战》第八章 Compose页面 导航
    vue脚手架(vue-cli)详细安装过程
    ubuntu 18 断网/网络不通,利用 ifconfig -a查看只有lo
  • 原文地址:https://blog.csdn.net/xixihaha_coder/article/details/126582202