简单搭建一个微服务框架,进而了解微服务
1. 父工程构建
- 注解生效激活
- setting -> Build, Execution, Deployment -> Compiler -> Annotation Processors
- 勾选
Enable annotation processing
- File Type过滤(过滤多余文件)
- setting -> Editor -> File Types
- 在
Ignore Files and Folders
中添加 *.idea;*.iml;
2. 修改pom.xml文件
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
| <groupId>com.letere.springcloud</groupId> <artifactId>cloud2020</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging>
<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>8.0.18</mysql.version> <druid.verison>1.1.16</druid.verison> <mybatis.spring.boot.verison>1.3.0</mybatis.spring.boot.verison> </properties>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.2.2.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR1</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.2.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-spring-boot-starter</artifactId> <version>${druid.verison}</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.spring.boot.verison}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.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> </dependencies>
|
3. 知识补充
dependenciesManagement
:用来锁定子模块依赖的版本,子模块写依赖时可以省略版本号,而且此并不会引入依赖
- Maven跳过单元测试:IDEA -> 右侧栏
maven
-> 圆形闪电
4. 支付模块构建
目的:用户端口为80,去引用8001端口的支付功能
4.1 微服务模块
- (1)建module
- (2)改POM
- (3)写YML
- (4)主启动
- (5)业务类
4.2 新建module
4.3 改POM
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
| <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> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </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>
|
4.4 写YML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| server: port: 8001
spring: application: name: cloud-payment-service datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8 username: root password: 123
mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.letere.springcloud.entities
|
4.5 创建主启动类
1 2 3 4 5 6
| @SpringBootApplication public class PaymentMain8001 { public static void main(String[] args) { SpringApplication.run(PaymentMain8001.class, args); } }
|
4.6 业务类
1 2 3 4 5
| CREATE TABLE `payment` ( `id` bigint NOT NULL AUTO_INCREMENT, `serial` varchar(200) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
1 2 3 4 5 6 7
| @Data @NoArgsConstructor @AllArgsConstructor public class Payment implements Serializable { private Long id; private String serial; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| @Data @NoArgsConstructor @AllArgsConstructor public class CommonResult<T> { private Integer code; private String message; private T data;
public CommonResult(Integer code, String message) { this(code, message, null); } }
|
1 2 3 4 5 6
| @Mapper public interface PaymentDao { public int create(Payment payment);
public Payment getPaymentById(Long id); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.letere.springcloud.dao.PaymentDao">
<resultMap id="PaymentMap" type="com.letere.springcloud.entities.Payment"> <id column="id" property="id" jdbcType="BIGINT"/> <id column="serial" property="serial" jdbcType="VARCHAR"/> </resultMap>
<select id="getPaymentById" parameterType="Payment" resultMap="PaymentMap"> select * from `payment` where id = #{id} </select>
<insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id"> insert into `payment`(serial) values(#{servial}) </insert> </mapper>
|
1 2 3 4 5
| public interface PaymentService { public int create(Payment payment);
public Payment getPaymentById(Long id); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| @Service public class PaymentServiceImp implements PaymentService{ @Resource private PaymentDao paymentDao;
@Override public int create(Payment payment) { return paymentDao.create(payment); }
@Override public Payment getPaymentById(Long id) { return paymentDao.getPaymentById(id); } }
|
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
| @RestController @Slf4j public class PaymentController { @Resource private PaymentService paymentService;
@PostMapping("/payment/create") public CommonResult create(@RequestBody Payment payment) { int result = paymentService.create(payment); log.info("****插入结果:" + result); if(result > 0) { return new CommonResult(200, "插入成功", null); } return new CommonResult(400, "插入失败", null);
}
@GetMapping("/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, "查询成功", payment); } return new CommonResult(400, "查询失败", null); } }
|
5. 消费者模块
消费者模块 与 支付模块差不多,所以重复的地方不说了
5.1 依赖引入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <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.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>
|
5.2 写YML
5.3 实体类
5.4 注入RestTemplate
- RestTemplate是Spring用来模拟客户端来发送请求
1 2 3 4 5 6 7
| @Configuration public class ApplicationContextConfig { @Bean public RestTemplate getTemplate() { return new RestTemplate(); } }
|
5.5 Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| @RestController @Slf4j public class OrderController {
private static final String PAYMENT_URL = "http://localhost:8001";
@Autowired private RestTemplate restTemplate;
@GetMapping("/consumer/payment/create") public CommonResult<Payment> create(Payment payment) { return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class); }
@GetMapping("/consumer/payment/get/{id}") public CommonResult getPayment(@PathVariable("id") Long id) { return restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, CommonResult.class); } }
|
6. 工程重构
6.1 新建共用工程
- 创建Maven项目:
cloud-api-commons
6.2 修改POM
- 引入依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <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> <scope>provided</scope> <optional>true</optional> </dependency>
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.3.7</version> </dependency>
|
6.3 install项目
6.4 引入依赖
1 2 3 4 5
| <dependency> <groupId>com.letere.springcloud</groupId> <artifactId>cloud-api-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
|