个人博客开发之旅后端篇02:框架搭建

发表于 2023-07-06 | 后端

废话开始

idea新建项目到的时候,选spring init,已经什么都给你做了,不需要你在费什么劲。

gradle配置

plugins {
    id 'org.springframework.boot' version '2.5.5'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}
 
group = 'cn.inyaa'
sourceCompatibility = '17'
 
configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}
 
repositories {
    maven { url 'https://maven.aliyun.com/repository/public/' }
}
 
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'org.springframework.boot:spring-boot-starter-data-redis'
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    //consul配置中心
    implementation 'org.springframework.cloud:spring-cloud-starter-consul-config'
    //consul服务发现
    implementation 'org.springframework.cloud:spring-cloud-starter-consul-discovery'
    //服务调用
    implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
 
    implementation 'com.github.xiaoymin:knife4j-spring-boot-starter:2.0.9'
    implementation 'com.alibaba:fastjson:1.2.68'
 
    implementation 'com.querydsl:querydsl-core'
    implementation 'com.querydsl:querydsl-jpa'
    implementation 'org.apache.commons:commons-lang3'
 
    annotationProcessor 'com.querydsl:querydsl-apt:4.4.0:jpa'
    annotationProcessor 'org.springframework.boot:spring-boot-starter-data-jpa'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    runtimeOnly 'mysql:mysql-connector-java'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
 
test {
    useJUnitPlatform()
}
 
dependencyManagement {
    imports {
        mavenBom "org.springframework.boot:spring-boot-dependencies:2.5.5"
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:2020.0.4"
    }
}
 

没啥好说的,从上往下看,webflux、actuator什么的是给配置中心用的。 openfeign是服务发现用的。 querydsl是加强jpa的。

spring 配置

server:
  port: 8080
 
spring:
  config:
    import: optional:consul:${spring.cloud.consul.host}:${spring.cloud.consul.port}
  application:
    name: inyaa-admin # 应用名称
  profiles:
    active: dev # 指定环境,默认加载 default 环境
  cloud:
    consul:
      # Consul 服务器地址
      host: 122.xx.xx.xx
      port: 8500
      # 配置中心相关配置
      config:
        # 是否启用配置中心,默认值 true 开启
        enabled: true
        # 指定配置格式为 yaml
        format: YAML
        watch:
          # 是否开启自动刷新,默认值 true 开启
          enabled: true
          # 刷新频率,单位:毫秒,默认值 1000
          delay: 1000
      # 服务发现相关配置
      discovery:
        register: true                                # 是否需要注册
        instance-id: ${spring.application.name}-01    # 注册实例 id(必须唯一)
        service-name: ${spring.application.name}      # 服务名称
        port: ${server.port}                          # 服务端口
        prefer-ip-address: true                       # 是否使用 ip 地址注册
        ip-address: ${spring.cloud.client.ip-address} # 服务请求 ip
 

配置中心的配置

搜consul