文章目录

  • 1、Spring Cloud 版本选择
  • 2、Spring Cloud 停更组件说明
  • 3、Spring Cloud 升级
  • 4、Spring Cloud 父工程构建
  • 4.1、父工程构建
  • 4.2、父工程 pom
  • 4.3、Maven 工程落地细节复习
  • 5、Rest 微服务工程构建
  • 5.1、构建步骤
  • 5.2、新建 cloud-api-commons 公共模块
  • 5.2.1、pom
  • 5.2.2、entity
  • 5.2.3、result 返回统一数据格式
  • 5.2.4、maven 命令打包发布
  • 5.2.5、创建完成后,回到父工程查看 pom 文件变化
  • 5.3、建立 cloud-provider-payment8001 模块
  • 5.3.1、创建完成后,回到父工程查看 pom 文件变化
  • 5.3.2、更改 pom 文件
  • 5.3.3、写 YML
  • 5.3.4、主启动类(新建)
  • 5.3.5、业务类
  • 5.3.6、测试
  • 5.4、建立 cloud-consumer-order80 模块
  • 5.4.1、创建完成后,回到父工程查看 pom 文件变化
  • 5.4.2、更改 pom 文件
  • 5.4.3、写 YML
  • 5.4.4、主启动类(新建)
  • 5.4.5、业务类(RestTemplate)
  • 5.4.6、测试


1、Spring Cloud 版本选择

官网https://spring.io/projects/spring-cloud

选择版本https://start.spring.io/actuator/info 查看 Json 串返回结果。

springcloud openfeign 中文乱码 springcloud中文社区_maven

  • cloud
  • Hoxton.SR9
  • boot
  • 2.3.5.RELEASE
  • cloud alibaba
  • 2.2.1.RELEASE
  • jdk
  • 1.8
  • MySQL
  • 8.0.x
  • Maven
  • 3.6.x

2、Spring Cloud 停更组件说明

springcloud openfeign 中文乱码 springcloud中文社区_maven_02

3、Spring Cloud 升级

springcloud openfeign 中文乱码 springcloud中文社区_maven_03

4、Spring Cloud 父工程构建

4.1、父工程构建

  1. New Project
  2. springcloud openfeign 中文乱码 springcloud中文社区_maven_04

  3. 聚合总父工程名字
  4. springcloud openfeign 中文乱码 springcloud中文社区_ci_05

  5. Maven 版本
  6. springcloud openfeign 中文乱码 springcloud中文社区_maven_06

  7. 工程测试
  8. springcloud openfeign 中文乱码 springcloud中文社区_spring_07

  9. 字符编码
  10. springcloud openfeign 中文乱码 springcloud中文社区_maven_08

  11. 注解生效激活
  12. springcloud openfeign 中文乱码 springcloud中文社区_ci_09

  13. Java 编译版本选择 1.8
  14. springcloud openfeign 中文乱码 springcloud中文社区_maven_10

  15. File Type 过滤
  16. springcloud openfeign 中文乱码 springcloud中文社区_spring_11

4.2、父工程 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.springcloud-demo</groupId>
    <artifactId>springcloud-parent</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!-- 多模块项目中, 父模块打包类型必须是 pom  -->
    <packaging>pom</packaging>

    <!-- 模块说明:这里声明多个子模块 -->
    <modules>
        <!-- <module>...</module> -->
    </modules>

    <!-- 版本号 -->
    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- 其他模块 -->
        <lombok.version>1.18.16</lombok.version>
        <junit.version>4.13.1</junit.version>
        <mysql.version>8.0.22</mysql.version>
        <druid.version>1.2.2</druid.version>
        <log4j.version>1.2.17</log4j.version>
        <hutool.version>5.3.10</hutool.version>
        <swagger.version>3.0.0</swagger.version>
        <mybatis-plus.version>3.4.1</mybatis-plus.version>
    </properties>

    <!-- 版本说明: 这里统一管理依赖的版本号 -->
    <dependencyManagement>
        <dependencies>
            <!-- spring-cloud-dependencies -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR9</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- spring-boot-dependencies -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.3.5.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- spring-cloud-alibaba-dependencies -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- 其他模块 -->
            <!-- Lombok -->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </dependency>
            <!-- junit -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
            </dependency>
            <!-- MySQL 数据库驱动 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
            <!-- druid 数据源 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${druid.version}</version>
            </dependency>
            <!-- log4j: druid 要用这个 -->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <!-- Hutool 工具包 -->
            <dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-all</artifactId>
                <version>${hutool.version}</version>
            </dependency>
            <!-- Swagger -->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-boot-starter</artifactId>
                <version>${swagger.version}</version>
            </dependency>
            <!-- MyBatis-Plus -->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>${mybatis-plus.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <finalName>cloud-demo</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.4.RELEASE</version>
                <configuration>
                    <fork>true</fork>
                    <addResources>true</addResources>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

4.3、Maven 工程落地细节复习

  1. Maven 中的 dependencyManagement 和 dependencies
  2. Maven 中跳过单元测试
  3. springcloud openfeign 中文乱码 springcloud中文社区_spring_12

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

5、Rest 微服务工程构建

5.1、构建步骤

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

5.2、新建 cloud-api-commons 公共模块

5.2.1、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">
    <parent>
        <artifactId>springcloud-parent</artifactId>
        <groupId>com.springcloud-demo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-api-commons</artifactId>

    <dependencies>
        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- Hutool 工具包 -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
        </dependency>
        <!-- 热部署 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
    </dependencies>
</project>
5.2.2、entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {

    private Long id;
    private String serial;
}
5.2.3、result 返回统一数据格式


5.2.4、maven 命令打包发布
- clean
- install
5.2.5、创建完成后,回到父工程查看 pom 文件变化
<!-- 模块说明:这里声明多个子模块 -->
<modules>
	<!-- 新加入的模块 -->
    <module>cloud-api-commons</module>
</modules>

5.3、建立 cloud-provider-payment8001 模块

5.3.1、创建完成后,回到父工程查看 pom 文件变化
<!-- 模块说明:这里声明多个子模块 -->
<modules>
	<!-- 新加入的模块 -->
    <module>cloud-provider-payment8001</module>
    <module>cloud-api-commons</module>
</modules>
5.3.2、更改 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">
    <parent>
        <artifactId>springcloud-parent</artifactId>
        <groupId>com.springcloud-demo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-provider-payment8001</artifactId>

    <dependencies>
        <!-- 引入自己定义的 api 通用包 -->
        <dependency>
            <groupId>com.springcloud-demo</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>

        <!-- web -->
        <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-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- MyBatis-Plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
        </dependency>
        <!-- MySQL 数据库驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- druid 数据源 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
        <!-- log4j: druid 要用这个 -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </dependency>
        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- 热部署 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

    </dependencies>

</project>
5.3.3、写 YML
  1. application-database.yml
spring:
  datasource: # 数据源
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC # &serverTimezone=UTC 解决时区报错
    type: com.alibaba.druid.pool.DruidDataSource # 自定义数据源
    # Spring Boot 默认是不注入这些属性值的,需要自己绑定
    # druid 数据源专有配置
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL # 验证连接
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    # 配置监控统计拦截的filters
    #   stat:监控统计、
    #   log4j:日志记录、
    #   wall:防御 sql 注入
    # 如果允许时报错 java.lang.ClassNotFoundException:org.apache.log4j.Priority
    # 则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
  1. application-mybatis-plus.yml
mybatis-plus:
  # 指定 MyBatis 的核心配置文件与 Mapper 映射文件
  mapper-locations: classpath:mappers/*.xml
  # 对应实体类的路径
  type-aliases-package: com.demo.entity
  # MyBatis 驼峰命名规则自动转换
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true
  1. application.yml
server:
  port: 8001

spring:
  application:
    name: cloud-payment-service
  profiles:
    include: database, mybatis-plus
5.3.4、主启动类(新建)
@SpringBootApplication
public class PaymentMain8001 {

    public static void main(String[] args) {
        SpringApplication.run(PaymentMain8001.class);
    }
}
5.3.5、业务类
  1. 建表
  2. springcloud openfeign 中文乱码 springcloud中文社区_spring_13

  3. mapper
@Mapper
public interface PaymentMapper extends BaseMapper<Payment> {

}

springcloud openfeign 中文乱码 springcloud中文社区_spring_14

<?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.demo.mapper.PaymentMapper">

</mapper>
  1. service
public interface PaymentService extends IService<Payment> {

}
@Service
public class PaymentServiceImpl extends ServiceImpl<PaymentMapper, Payment> implements PaymentService {

}
  1. controller
@Slf4j
@RestController
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @GetMapping("/listPayment")
    public ResultBody listPayment() {
        return ResultTool.success(paymentService.list());
    }

}
5.3.6、测试

postman 模拟 get

springcloud openfeign 中文乱码 springcloud中文社区_maven_15

5.4、建立 cloud-consumer-order80 模块

5.4.1、创建完成后,回到父工程查看 pom 文件变化
<!-- 模块说明:这里声明多个子模块 -->
 <modules>
     <module>cloud-provider-payment8001</module>
     <!-- 新加入的模块 -->
     <module>cloud-consumer-order80</module>
     <module>cloud-api-commons</module>
 </modules>
5.4.2、更改 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">
    <parent>
        <artifactId>springcloud-parent</artifactId>
        <groupId>com.springcloud-demo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-consumer-order80</artifactId>

    <dependencies>
        <!-- 引入自己定义的 api 通用包 -->
        <dependency>
            <groupId>com.springcloud-demo</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>

        <!-- web -->
        <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-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- 热部署 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
    </dependencies>

</project>
5.4.3、写 YML
server:
  port: 80
spring:
  application:
    name: cloud-payment-order
5.4.4、主启动类(新建)
@SpringBootApplication
public class OrderMain {

    public static void main(String[] args) {
        SpringApplication.run(OrderMain.class);
    }
}
5.4.5、业务类(RestTemplate)

RestTemplate 是什么?

RestTemplate 提供了多种便捷访问远程 Http 服务的方法,是一种简单便捷的访问 restful 的服务模板类,是 Spring 提供的用户访问 Rest 服务的客户端模板工具集。

config 配置类:ApplicationContextConfig

@Configuration
public class ApplicationContextConfig {

    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}

controller:注意加上 http://

@Slf4j
@RestController
public class OrderController {

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/consumer/listPayment")
    public ResultBody listPayment() {
        return restTemplate.getForObject("http://localhost:8001" + "/listPayment", ResultBody.class);
    }
}
5.4.6、测试

postman 模拟 get

springcloud openfeign 中文乱码 springcloud中文社区_spring_16