1-3 Building microservices

Microservice construction steps

  • Build object
用户微服务shop-user
商品微服务shop-product
订单微服务shop-order
  • Brief description of steps
1 构建工程
2 修改依赖
3 创建主类
4 创建配置文件
    优先级:bootstrap.properites > bootstrap.yml > application.properties > application.yml
5 创建接口和实现类(cotroller、service、dao)

1 Create a new module on the parent project, the type is maven

2 Modify pom.xml

  • Add web dependency and common entity dependency
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>cn.hzp</groupId>
            <artifactId>shop-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

3 Add the main class

  • Take the user microservice shop-user as an example, create cn.hzp.UserApplication under /src/main/java
@SpringBootApplication
public class UserApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class);
    }
}

4 Create the configuration file application.yml and create it in the src/main/resources directory

  • Add service port, service name, data source, JPA
server:
  port: 8071
spring:
  application:
    name: service-user
  # 数据源配置
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/shop?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=true
    username: root
    password: root
  # 持久层配置
  jpa:
    properties:
      hibernate:
        hbm2ddl:
          auto: update
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect

5 Create controller, service, dao under src/main/java/cn/hzp

Guess you like

Origin blog.csdn.net/weixin_45544465/article/details/105936490
1-3