Spring Cloud微服务项目搭建(二)

141 阅读3分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第12天,点击查看活动详情

前言

上一篇介绍了如何搭建微服务开发框架,本篇介绍一下如何在微服务开发框架中配置数据库,如果添加其他的也来也是相同的步骤。

数据库配置

父工程添加依赖

上面是搭建微服务的整个过程,下面通过连接数据库来演示如何添加依赖。配置vip-calculate-biz连接数据库库。

vip-cloud的pom.xml中添加依赖,这个不能通过复制然后覆盖原来的pom.xml。只是在原来的基础上进行增加。

<properties>
  <mysql-connector-java.version>8.0.15</mysql-connector-java.version>
  <mybatis-spring-boot-starter.version>2.0.0</mybatis-spring-boot-starter.version>
  <druid-spring-boot-starter.version>1.1.9</druid-spring-boot-starter.version>
  <pagehelper.version>1.3.0</pagehelper.version>
</properties>

<dependencyManagement>
  <dependencies>
    <!-- mysql -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>${mysql-connector-java.version}</version>
    </dependency>

    <!--springboot集成mybatis-->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>${mybatis-spring-boot-starter.version}</version>
      <exclusions>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

    <!--druid连接池-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid-spring-boot-starter</artifactId>
      <version>${druid-spring-boot-starter.version}</version>
    </dependency>

    <!-- pagehelper 分页插件 -->
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper-spring-boot-starter</artifactId>
      <version>${pagehelper.version}</version>
    </dependency>
  </dependencies>
</dependencyManagement>

公共服务添加依赖

vip-calculate-common的pom.xml中添加依赖,这个不能通过复制然后覆盖原来的pom.xml。只是在原来的基础上进行增加。

<dependencies>
  <!-- mysql -->
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
  </dependency>

  <!--springboot集成mybatis-->
  <dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <exclusions>
      <exclusion>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-logging</artifactId>
      </exclusion>
    </exclusions>
  </dependency>

  <!--druid连接池-->
  <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
  </dependency>

  <!-- pagehelper 分页插件 -->
  <dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
  </dependency>
</dependencies>

修改业务模块配置文件

vip-calculate-biz添加配置application.yml

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/vip?useUnicode=true&characterEncoding=utf8&useSSL=false&autoReconnect=true&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=123456

添加启动类

新建packagecom.vip.calculate.biz,然后新建一个启动类CalculateBizApplication

package com.vip.calculate.biz;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

启动成功

输出如下信息,表示启动成功。

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.5.RELEASE)

2022-06-05 18:00:13.683  INFO 87776 --- [           main] c.v.c.biz.CalculateBizApplication        : Starting CalculateBizApplication on jielingdeMacBook-Pro.local with PID 87776 (/Users/jielingyang/IdeaProjects/i-workspace/vip-cloud/vip-calculate/vip-calculate-biz/target/classes started by jielingyang in /Users/jielingyang/IdeaProjects/i-workspace/vip-cloud)
2022-06-05 18:00:13.688  INFO 87776 --- [           main] c.v.c.biz.CalculateBizApplication        : No active profile set, falling back to default profiles: default
2022-06-05 18:00:14.559  WARN 87776 --- [           main] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[com.vip.calculate.biz]' package. Please check your configuration.
2022-06-05 18:00:15.266  INFO 87776 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8081 (http)
2022-06-05 18:00:15.272  INFO 87776 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-06-05 18:00:15.273  INFO 87776 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.31]
2022-06-05 18:00:15.330  INFO 87776 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-06-05 18:00:15.330  INFO 87776 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1591 ms
2022-06-05 18:00:15.462  INFO 87776 --- [           main] c.a.d.s.b.a.DruidDataSourceAutoConfigure : Init DruidDataSource
2022-06-05 18:00:15.598  INFO 87776 --- [           main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited
2022-06-05 18:00:15.796  INFO 87776 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2022-06-05 18:00:16.265  INFO 87776 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
2022-06-05 18:00:16.331  INFO 87776 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8081 (http) with context path ''
2022-06-05 18:00:16.333  INFO 87776 --- [           main] c.v.c.biz.CalculateBizApplication        : Started CalculateBizApplication in 3.06 seconds (JVM running for 3.65)
2022-06-05 18:00:16.633  INFO 87776 --- [on(2)-127.0.0.1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-06-05 18:00:16.633  INFO 87776 --- [on(2)-127.0.0.1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2022-06-05 18:00:16.638  INFO 87776 --- [on(2)-127.0.0.1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 5 ms

以上是通过对项目中添加数据库依赖,简单的说明了如何在微服务框架中添加第三方依赖,此添加依赖的方式,只适合于根据第一篇文章搭建的微服务结构。需要注意的地方如下:

  • 在父工程中添加依赖及相关版本号
  • 在公共模块中添加相关依赖,无需关系版本号
  • 在各个业务模块的父工程中添加公共模块

需要注意的是,如果是通过父工程继承的依赖,一定要注意依赖的作用范围scope。比如lombok依赖,如果<scope>provided</scope>,那么在子工程中不会有该依赖。将该scope删除即可。