• springcloud入门


    微服务架构介绍

    微服务架构, 简单的说就是将单体应用进一步拆分,拆分成更小的服务,每个服务都是一个可以独
    立运行的项目。

    微服务架构的常见问题

    一旦采用微服务系统架构,就势必会遇到这样几个问题:

    • 这么多小服务,如何管理他们?(服务治理 注册中心[服务注册 发现 剔除])
    • 这么多小服务,他们之间如何通讯?(restful rpc)
    • 这么多小服务,客户端怎么访问他们?(网关)
    • 这么多小服务,一旦出现问题了,应该如何自处理?(容错)
    • 这么多小服务,一旦出现问题了,应该如何排错? (链路追踪)

    对于上面的问题,是任何一个微服务设计者都不能绕过去的,因此大部分的微服务产品都针对每一
    个问题提供了相应的组件来解决它们。

    springcloud:
    Spring Cloud是一系列框架的集合。它利用Spring Boot的开发便利性巧妙地简化了分布式系统
    础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用
    Spring Boot的开发风格做到一键启动和部署。
    Spring Cloud并没有重复制造轮子,它只是将目前各家公司开发的比较成熟、经得起实际考验的服
    务框架组合起来,通过Spring Boot风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留
    出了一套简单易懂、易部署和易维护的分布式系统开发工具包。

    父项pom.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.xujie</groupId>
        <artifactId>springcloud_sp</artifactId>
        <packaging>pom</packaging>
        <version>1.0-SNAPSHOT</version>
        <!--    子模块-->
        <modules>
            <module>shop_common</module>
            <module>shop_user</module>
            <module>shop_product</module>
            <module>shop_order</module>
        </modules>
    
        <!--依赖版本的锁定-->
        <properties>
            <java.version>1.8</java.version>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <spring-boot.version>2.3.2.RELEASE</spring-boot.version>
            <spring-cloud.version>Hoxton.SR9</spring-cloud.version>
            <spring-cloud-alibaba.version>2.2.6.RELEASE</spring-cloud-alibaba.version>
        </properties>
    
        <dependencyManagement>
            <dependencies>
                <!-- SpringBoot 依赖配置 -->
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>${spring-boot.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
    
        </dependencyManagement>
    
    </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

    common公共模块:
    所有的项目需要的依赖:

    <?xml version="1.0" encoding="UTF-8"?>
    <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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <!--    继承父项-->
        <parent>
            <artifactId>springcloud_sp</artifactId>
            <groupId>com.xujie</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>shop_common</artifactId>
        <!--依赖-->
        <dependencies>
            <!--<dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>-->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.56</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.6</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
        <properties>
            <maven.compiler.source>8</maven.compiler.source>
            <maven.compiler.target>8</maven.compiler.target>
        </properties>
    
    </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

    子项目:

    <?xml version="1.0" encoding="UTF-8"?>
    <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.0</modelVersion>
        <groupId>com.xujie</groupId>
        <version>0.0.1-SNAPSHOT</version>
        <parent>
            <artifactId>springcloud_sp</artifactId>
            <groupId>com.xujie</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <artifactId>shop_product</artifactId>
    
        <dependencies>
        <!--        引入公共模块-->
            <dependency>
                <groupId>com.xujie</groupId>
                <artifactId>shop_common</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </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

    调用其他模块:

    package com.xujie.shop_order;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;
    
    @SpringBootApplication
    public class ShopOrderApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ShopOrderApplication.class, args);
        }
    
        @Bean
        public RestTemplate restTemplate(){
            return new RestTemplate();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    controller:

    package com.xujie.shop_order.controller;
    
    import com.xujie.common.model.Order;
    import com.xujie.common.model.Product;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    /**
     * @author 许缘
     * @company xxx公司
     * @create 2022-12-06  17:25
     */
    @RestController
    @RequestMapping("/order")
    public class ordercontroller {
    
        @Autowired
        private RestTemplate restTemplate;
        @RequestMapping("/test/{pid}")
        public Order createOrder(@PathVariable("pid") Integer pid){
            Product product =restTemplate.getForObject(
                    "http://localhost:8080/product/getOne/"+pid,Product.class
            );
            //创建订单
            Order order = new Order();
            order.setOid(System.currentTimeMillis());
            order.setUid(12);
            order.setUsername("张三");
            order.setPid(product.getPid());
            order.setPname(product.getPname());
            order.setPprice(product.getPprice());
            order.setNumber(product.getStock());
            return order;
    
    
        }
    }
    
    
    • 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

    效果图:
    在这里插入图片描述

  • 相关阅读:
    【多线程进阶】CAS实现及应用
    【云原生 • Kubernetes】kubernetes 核心技术 - Label 和 Selector
    Elasticsearch 注册为Windows服务
    input 的 placeholder 样式
    1465. 切割后面积最大的蛋糕 : 为何仅需处理相邻切割位(多语言题解)
    算法--找到和最大的长度为 K 的子序列(Kotlin)
    git 更换远程地址的方法
    Servlet使用
    java 比较运算符
    Java中如何实现定时任务?
  • 原文地址:https://blog.csdn.net/weixin_63719049/article/details/128210388