springcloud(二) 微服务架构编码构建

时间:2021-04-10 19:29:54

微服务架构编码构建

1 基础知识

1.1 版本

springcloud : CURRENT
springcloud(二)  微服务架构编码构建

springboot : 2.4.3
springcloud(二)  微服务架构编码构建

2 微服务cloud整体聚合父工程Project

2.1 new project

2.2 字符编码设置 utf-8

2.3 pom.xml

 <!-- 统一管理jar包和版本 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>4.12</junit.version>
<mysql.version>8.0.16</mysql.version>
<druid.verison>1.1.16</druid.verison>
<mybatis.spring.boot.verison>2.1.0</mybatis.spring.boot.verison>
<lombok.version>1.18.10</lombok.version>
<junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version>
</properties> <!-- 子模块继承后提供作用,锁定版本+子modlue,不用写groupid和version -->
<dependencyManagement>
<dependencies>
<!-- springboot2.4.3-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.4.3</version>
<type>pom</type>
<scope>import</scope>
</dependency> <!-- springcloud CURRENT版本 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>CURRENT</version>
<type>pom</type>
</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> <!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency> <!-- mysql 8.0.16-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency> <!-- Druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.verison}</version>
</dependency> <!-- mybatis-springboot整合 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.spring.boot.verison}</version>
</dependency> <!-- lombok 1.18.10 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency> <!-- junit 4.12 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency> <!-- log4j 1.2.17 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.verson}</version>
</dependency> </dependencies>
</dependencyManagement>

加一个这个<packaging>pom</packaging>
springcloud(二)  微服务架构编码构建

问题:Maven中的DependencyManagement和Dependencies区别
springcloud(二)  微服务架构编码构建
springcloud(二)  微服务架构编码构建

2.4 父工程创建完成执行mvn:install将父工程发布到仓库方便子工程继承

3 cloud-provider-payment8001微服务提供者支付Module模块

3.1 建cloud-provider-payment8001

3.2 改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>
</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>

3.3 写yml

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/db2021?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
username: root
password: 18170021 mybatis:
mapperLocations: classpath:mapper/*.xml
type-aliases-package: com.wang.entity

3.4 主启动

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

3.5 数据库

springcloud(二)  微服务架构编码构建

3.6 entity

Payment类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable { private Long id;
private String serial;
}

CommonResult类
Json封装体CommonResult

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> { private Integer code;
private String messag;
private T data; public CommonResult(Integer code, String messag){
this.code = code;
this.messag = messag;
} }

3.7 dao

springcloud(二)  微服务架构编码构建

3.8 service

3.9 controller

@RestController
@Slf4j
public class PaymentController { @Autowired
private PaymentService paymentService; @PostMapping(value = "/payment/create")
public CommonResult create(@RequestBody Payment payment) {
int result = paymentService.create(payment);
log.info("*******插入结果" + result); if(result > 0){
return new CommonResult(200, "插入成功", result);
}else {
return new CommonResult(444, "插入失败", null);
}
} @GetMapping(value = "/payment/get/{id}")
public CommonResult getPaymentById(@PathVariable("id") Long id){ Payment payment = paymentService.getPaymentById(id);
if(payment != null){
return new CommonResult(200, "查询成功", payment);
}else{
return new CommonResult(444, "查询失败", null);
}
}
}

4 cloud-consumer-order80 微服务消费者订单Module模块

4.1 建cloud-consumer-order80

4.2 改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.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.3 写YML

server:
port: 8082

4.4 主启动


//因为消费者不需要写dao层数据库操作 所以yml文件中没有数据库配置,不加后面的就报错
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class OrderMain80 { public static void main(String[] args) {
SpringApplication.run(OrderMain80.class, args);
}
}

4.5 业务类

4.5.1 entity (同上)

4.5.2 RestTemplate使用

springcloud(二)  微服务架构编码构建
springcloud(二)  微服务架构编码构建

(1) config配置类注册restTemplate

@Configuration
public class ApplicationContextConfig { //applicationContext.xml <bean id=? ..... >
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
} }

4.5.3 controller

@RestController
@Slf4j
public class OrderController { private static final String PAYMENT_URL = "http://localhost:8001";
@Autowired
private RestTemplate restTemplate; @GetMapping(value = "/consumer/payment/create")
public CommonResult<Payment> create(Payment payment){
return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
} @GetMapping(value = "/consumer/payment/get/{id}")
public CommonResult<Payment> getPayment(@PathVariable("id") Long id){ return restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
}
}

5 工程重构

系统中有entity重复

5.1 新建工程 cloud-api-commons

5.2 pom

 <dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency> <dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.6.3</version>
</dependency>
</dependencies>

5.3 entity

springcloud(二)  微服务架构编码构建

5.4 maven命令clean install

5.5 订单80和支付8001分别改造

5.5.1 删除各自的原先有过的entity文件夹

5.5.2 各自粘贴POM内容

 <!--    引入自己的定义的api通用包,可以使用payment支付的entity-->
<dependency>
<groupId>com.wang</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>