• 四、微服务架构编码构建


    1、父工程 cloud2020的搭建

    ①、pom文件

    <?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.atguigu.springcloud</groupId>
        <artifactId>cloud2020</artifactId>
        <version>1.0-SNAPSHOT</version>
        <modules>
            <module>cloud-provider-payment8001</module>
            <module>cloud-provider-payment8002</module>
            <module>cloud-consume-rorder80</module>
            <module>cloud-api-commons</module>
            <module>cloud-eureka-server7001</module>
            <module>cloud-eureka-server7002</module>
            <module>cloud-provider-payment8004</module>
            <module>cloud-consumerzk-order80</module>
            <module>cloud-providerconsul-payment8006</module>
            <module>cloud-consumerconsul-order80</module>
            <module>cloud-consumer-feign-order80</module>
            <module>cloud-provider-hystrix-payment8001</module>
            <module>cloud-consumer-feign-hystrix-order80</module>
            <module>cloud-gateway-gateway9527</module>
            <module>cloud-config-center-3344</module>
            <module>cloud-config-client-3355</module>
            <module>cloud-config-client-3366</module>
            <module>cloud-stream-rabbitmq-provider8801</module>
            <module>cloud-stream-rabbitmq-consumer8802</module>
            <module>cloud-stream-rabbitmq-consumer8803</module>
        </modules>
        <packaging>pom</packaging>
    
        <!-- 统一管理jar包版本 -->
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
            <junit.version>4.12</junit.version>
            <log4j.version>1.2.17</log4j.version>
            <lombok.version>1.16.18</lombok.version>
            <mysql.version>5.1.47</mysql.version>
            <druid.version>1.1.16</druid.version>
            <mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
        </properties>
    
        <!-- 子模块继承之后,提供作用:锁定版本+子modlue不用写groupId和version  -->
        <dependencyManagement>
            <dependencies>
                <!--spring boot 2.2.2-->
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>2.2.2.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <!--spring cloud Hoxton.SR1-->
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Hoxton.SR1</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <!--spring cloud alibaba 2.1.0.RELEASE-->
                <dependency>
                    <groupId>com.alibaba.cloud</groupId>
                    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                    <version>2.1.0.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>${mysql.version}</version>
                </dependency>
                <dependency>
                    <groupId>com.alibaba</groupId>
                    <artifactId>druid</artifactId>
                    <version>${druid.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.mybatis.spring.boot</groupId>
                    <artifactId>mybatis-spring-boot-starter</artifactId>
                    <version>${mybatis.spring.boot.version}</version>
                </dependency>
                <dependency>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                    <version>${junit.version}</version>
                </dependency>
                <dependency>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                    <version>${log4j.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.projectlombok</groupId>
                    <artifactId>lombok</artifactId>
                    <version>${lombok.version}</version>
                    <optional>true</optional>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <fork>true</fork>
                        <addResources>true</addResources>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>


    【注意】

    maven中的 dependencyManagement 和 dependencies 的区别?

    一般出现在最顶层父工程的pom文件中,我们会看到dependencyManagement元素。通过它来管理jar包的版本,让子项目中引用一个依赖而不用显示的列出版本号。Maven会沿着父子层次向上走,直到找到一个拥有dependencyManagement元素的项目,然后它就会使用在这个dependencyManagement元素中指定的版本号。

    【注意】dependencyManagement 声明的依赖并没有被导入项目

    这样做的好处:

    1、统一管理项目的版本号,确保应用的各个项目的依赖和版本一致

    2、想升级或者切换到另一个版本时,只需要在父类容器里更新,不需要任何一个子项目的修改;

    2、Dependencies

           管理项目中所有需要导入的依赖

    3、区别

      dependencyManagement里只是声明依赖,并不实现引入,因此子项目需要显示的声明需要用的依赖。如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom;另外如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。


    2、Rest微服务工程构建

    1)微服务支付模块 cloud-provider-payment8001的搭建

    ① 建moudle

    clipboard


    ②、pom文件

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <!--mysql-connector-java-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--jdbc-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>


    ③、yaml文件

    server:
      port: 8001
    
    spring:
      application:
        name: cloud-payment-service           
      datasource:
        type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
        driver-class-name: org.gjt.mm.mysql.Driver              # mysql驱动包
        url: jdbc:mysql://localhost:3306/springcloud2?useUnicode=true&characterEncoding=utf-8&useSSL=false
        username: root
        password: houchen
    
    mybatis:
      mapperLocations: classpath:mapper/*.xml        #指定sql映射文件的位置
      type-aliases-package: com.atguigu.springcloud.entity    # 所有Entity别名类所在包


    ④、主启动类

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


    ⑤、controller 等业务逻辑的编写

    这里我这写了controller,具体的代码,请见gitee https://gitee.com/houchen1996

    @RestController
    @Slf4j
    public class PaymentController {
    
        @Resource
        private PaymentService paymentService;
    
        @Value("${server.port}")
        private String serverPort;
    
        @PostMapping(value = "/payment/create")
        public CommonResult create(@RequestBody Payment payment){
            int result = paymentService.create(payment);
            if(result>0){
                return new CommonResult(200,"插入数据成功 port:"+serverPort);
            }else{
                return new CommonResult(444,"插入数据失败 port:"+serverPort,null);
            }
        }
    
    
        // {id}代表者路径占位符
        @GetMapping(value = "/payment/get/{id}")
        public CommonResult getPaymentById(@PathVariable("id") Long id){
            Payment payment = paymentService.getPaymentById(id);
            log.info("查询数据===热部署后"+payment);
            if(payment!=null){
                return new CommonResult(200,"数据查询成功 port:"+serverPort,payment);
            }else{
                return new CommonResult(444,"数据查询失败 port:"+serverPort,null);
            }
        }
    
    }


    测试工程

    clipboard


    2)消费者订单模块构建

    ①、在parent下建立子模块 cloud-consumer-order80


    ②、写pom文件

    <dependencies>
        <!--引入自己定义的api通用包,可以使用Payment支付Entity-->
        <dependency>
            <groupId>com.atguigu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>


    ③、yaml文件

    server:
      port: 80
    
    spring:
      application:
        name: cloud-consumer-order


    ④、主启动类

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


    ⑤、业务类:远程调用上述的支付 payment服务

    public class OrderController {
    
      private static final String PAYMANT_URL ="http://localhost:8001";
    
        @Autowired
        private RestTemplate restTemplate;
    
        @GetMapping("/consumer/payment/create")
        public CommonResult create(Payment payment){
            return restTemplate.postForObject(PAYMANT_URL+"/payment/create",payment,CommonResult.class);
        }
    
        @GetMapping("/consumer/payment/get/{id}")
        public CommonResult getPaymentById(@PathVariable("id") Long id){
            return restTemplate.getForObject(PAYMANT_URL+"/payment/get/"+id,CommonResult.class);
        }
    
    }
    
    restTemplate配置类:注入restTemplate对象
    @Configuration
    public class ApplicationContextConfig {
    
        @Bean
        public RestTemplate getRestTemplate(){
            return new RestTemplate();
        }
    }


    ⑤、查看调用情况

    80端口调用8001端口的服务:

    clipboard


    3、工程重构

    1)观察问题

    上面的两个工程中有重复的部分 - 实体类,可以进行工程重构,将实体类抽取到一个单独的工程中

    clipboard

    pom

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.0.6</version>
        </dependency>
    </dependencies>


    2) 将cloud-api-commons引用到需要使用实体类的工程的pom中

    <dependency>
        <groupId>com.atguigu.springcloud</groupId>
        <artifactId>cloud-api-commons</artifactId>
        <version>${project.version}</version>
    </dependency>


    以上是还没有引入微服务的基础服务,接下来的博客会逐渐引入 eureka,hystrix 等组件

  • 相关阅读:
    《UNIX环境高级编程》笔记--UNIX标准化及实现
    SPOJ1811最长公共子串问题(后缀自动机)
    一个leetcode解题报告类目,代码很简洁
    字符压缩题目
    求最佳会议地点
    实现树的横向指针
    lower_bound与upper_bound
    求到所有房子距离和最小的新房子
    增加限制条件的矩阵求和
    切分数组来得到一定的和
  • 原文地址:https://www.cnblogs.com/houchen/p/14774701.html
Copyright © 2020-2023  润新知