首页 > 技术文章 > SpringCloud搭建微服务模块

pengsay 2021-08-15 20:04 原文

套路五步走:

  1. 建Module
  2. 改POM
  3. 写YML
  4. 主启动
  5. 业务类

创建cloud-provider-payment8001微服务提供者支付Module模块:

第一步——建名为cloud-provider-payment8001的Maven工程

第二步——修改cloud-provider-payment8001子工程的POM文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>cloud2021</artifactId>
 7         <groupId>org.lzp.springcloud</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11     
12     <artifactId>cloud-provider-payment8001</artifactId>
13     
14     <dependencies>
15         <!--包含了sleuth+zipkin-->
16         <dependency>
17             <groupId>org.springframework.cloud</groupId>
18             <artifactId>spring-cloud-starter-zipkin</artifactId>
19         </dependency>
20         <!--eureka-client-->
21         <dependency>
22             <groupId>org.springframework.cloud</groupId>
23             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
24         </dependency>
25         <!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
26         <!--
27         <dependency>
28             <groupId>com.atguigu.springcloud</groupId>
29             <artifactId>cloud-api-commons</artifactId>
30             <version>${project.version}</version>
31         </dependency>
32         -->
33         <dependency>
34             <groupId>org.springframework.boot</groupId>
35             <artifactId>spring-boot-starter-web</artifactId>
36         </dependency>
37         <dependency>
38             <groupId>org.springframework.boot</groupId>
39             <artifactId>spring-boot-starter-actuator</artifactId>
40         </dependency>
41         <dependency>
42             <groupId>org.mybatis.spring.boot</groupId>
43             <artifactId>mybatis-spring-boot-starter</artifactId>
44         </dependency>
45         <dependency>
46             <groupId>com.alibaba</groupId>
47             <artifactId>druid-spring-boot-starter</artifactId>
48             <version>1.1.10</version>
49         </dependency>
50         <!--mysql-connector-java-->
51         <dependency>
52             <groupId>mysql</groupId>
53             <artifactId>mysql-connector-java</artifactId>
54         </dependency>
55         <!--jdbc-->
56         <dependency>
57             <groupId>org.springframework.boot</groupId>
58             <artifactId>spring-boot-starter-jdbc</artifactId>
59         </dependency>
60         <dependency>
61             <groupId>org.springframework.boot</groupId>
62             <artifactId>spring-boot-devtools</artifactId>
63             <scope>runtime</scope>
64             <optional>true</optional>
65         </dependency>
66         <dependency>
67             <groupId>org.projectlombok</groupId>
68             <artifactId>lombok</artifactId>
69             <optional>true</optional>
70         </dependency>
71         <dependency>
72             <groupId>org.springframework.boot</groupId>
73             <artifactId>spring-boot-starter-test</artifactId>
74             <scope>test</scope>
75         </dependency>
76     </dependencies>
77 
78 </project>

第三步——在项目src/main的resources根目录下编写YML配置文件

 1 server:
 2   port: 8001
 3 
 4 spring:
 5   application:
 6     name: cloud-payment-service
 7   datasource:
 8     type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
 9     driver-class-name: org.gjt.mm.mysql.Driver              # mysql驱动包
10     url: jdbc:mysql://localhost:3306/my?useUnicode=true&characterEncoding=utf-8&useSSL=false
11     username: root
12     password: 1234
13 
14 mybatis:
15   mapperLocations: classpath:mapper/*.xml
16   type-aliases-package: com.lzp.springcloud.entities    # 所有Entity别名类所在包

第四步——创建主启动类

 1 package com.lzp.springcloud;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 
 6 /**
 7  * @Author LZP
 8  * @Date 2021/8/15 19:45
 9  * @Version 1.0
10  */
11 @SpringBootApplication
12 public class PaymentMain8001 {
13     public static void main(String[] args) {
14         SpringApplication.run(PaymentMain8001.class);
15     }
16 }

第五步——业务类

略....这里主要讲步骤,具体业务不做实现

推荐阅读