日常通用开发架构。Spring Cloud技术栈

作为分布式项目的底层,会对项目模块进行规划和划分,因此,我需要对项目重构的功能模块进行整理。

1.为什么要进行重构?

在项目1.0版本时,在qa环境中并不能跟踪到服务的异常,经常出现服务不可用时,测试人员以为代码问题就提出bug。但当你去排查时,很难或者无法重现之前“bug”时的现场,因此,要对项目的框架进行一次升级。

2.新框架的好处?

  • 监控服务的状态
  • 升级注册中心,因eureka的停更,将切换成nacos作为注册发现
  • 加入校验注解,使数据校验可以简单配置
  • 加入docker 一键部署
  • 使用mybatis-plus代码生成

3.新框架的应用场景?

  • 想快速搭建一套分布式框架
  • 项目中没有监控服务的项目,可以用来升级

4.开撸!!!

  • 对项目进行分析后,架构进行梳理画出大致草图

 日常通用开发架构。Spring Cloud技术栈_第1张图片

  • 项目业务划分
    • 计划将Parent项目作为所有项目的总依赖。其子项目common作为所有扩展依赖包。
    • 为便于开发,将写一个规范项目,让所有项目在开发前,都使用样例项目进行开发

因此对项目结构进行如下规范

├─sc2-parent
│  └─sc2-common
├─sc2-web-deploy
│  ├─deploy-api
│  ├─deploy-core
 

 

  • parent项目主要规范项目的版本,以及私服地址等
    • 项目微服务版本选型为 cloud-Hoxton.SR3,boot-2.2.5.RELEASE
    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.5.RELEASE
    
    
    
            
                org.springframework.cloud
                spring-cloud-dependencies
                Hoxton.SR3
                pom
                import
            

    
  • 加入google 代码检查
                
                    org.apache.maven.plugins
                    maven-checkstyle-plugin
                    3.1.1
                    
                        config/checkStyle/google_checks.xml
                        config/checkStyle/suppressions.xml
                        UTF-8
                        true
                        false
                        true
                        false
                    
                    
                        
                            validate
                            validate
                            
                                true
                                true
                            
                        
                    
                
  • google_checks.xml 配置检查规则





    

    

    
    
    
	
		
	

    
        
        
            
            
            
        
        
            
            
            
        

        
        
        
        
            
            
        
        
        
        
            
            
        
        
            
            
            
        
        
            
            
            
            
            
             
        
        
        
        
        
        
        
        
        
            
        
        
            
            
            
        
        
            
            
            
        
        
            
            
            
            
        
        
            
            
            
            
        
        
            
            
            
        
        
            
            
        
        
            
        
        
            
            
        
        
            
            
        
        
            
            
        
        
            
            
            
        
        
            
            
        
        
            
            
        
        
            
            
        
        
        
            
             
             
             
        
        
            
            
            
            
            
            
        
        
            
            
        
        
        
        
            
            
            
        
        
        
          
            
        
        
        
            
            
        
        
            
            
        
        
            
            
            
        
        
        
        
            
        
        
        
            
            
        
        
            
            
            
            
            
            
            
        
        
            
            
        
        
            
        
        
            
        
        
    

  • 并制定docker的插件
    
                    io.fabric8
                    docker-maven-plugin
                    ${docker.plugin.version}
                    
                        ${docker.host}
                        ${docker.registry}
                        
                            
                                ${docker.username}
                                ${docker.password}
                            
                        
                        
                            
                                ${docker.registry}/${docker.namespace}/${project.name}:${project.version}
                                
                                    ${project.basedir}/Dockerfile
                                
                            
                        
                    
                
  • 为了能够在 application.yaml 文件中通过 @xxx@ 引到 pom.xml的属性,因此需要加上这个
        
            
                src/main/resources
                true
            
        
  • 加入hibernate 验证框架
      
        
            org.hibernate.validator
            hibernate-validator
        
        
            javax.validation
            validation-api
        
  • 加入Turbine - 监控集群下hystrix的metrics情况框架
 
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
        
            org.springframework.cloud
            spring-cloud-starter-hystrix
            ${spring-cloud-hystrix-version}
        
        

        
        
            org.springframework.cloud
            spring-cloud-starter-hystrix-dashboard
            ${spring-cloud-hystrix-version}
        
        
        
            org.springframework.cloud
            spring-cloud-starter-turbine
            ${spring-cloud-hystrix-version}
            
                
                    com.netflix.eureka
                    eureka-client
                
            
        
  • 加入 actuator
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        

 

  • 完整的parent-pom.xml

    4.0.0
    com.bobycloud
    sc2-parent
    0.0.1-SNAPSHOT
    pom
    ${project.artifactId}
    
        UTF-8
        2.1.1
        2.2.5.RELEASE
        Hoxton.SR3
        1.4.7.RELEASE
        1.0.2
        0.9.0.RELEASE
        1.2.67
        1.2.14
        2.2.3
        28.2-jre
        1.8
        1.8
        3.1
        2.7.0
        1.5.22
        1.9.4
        1.3.3
        1.21
        3.0.3
        3.7
        1.13
        9.0.10

        
        172.16.30.184
        http://172.16.30.184:2375
        library
        
        
        0.32.0

    

    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.5.RELEASE
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Hoxton.SR3
                pom
                import
            

            
                com.google.guava
                guava
                ${guava-version}
            
        
    
    
        ${project.name}
        
            
                src/main/resources
                true
            
        
        
            
                
                    org.springframework.boot
                    spring-boot-maven-plugin
                    ${spring-boot-version}
                
                
                    org.apache.maven.plugins
                    maven-compiler-plugin
                    ${maven-compiler-plugin-version}
                    
                        ${maven.compiler.target}
                        ${maven.compiler.source}
                    
                
                
                    org.apache.maven.plugins
                    maven-checkstyle-plugin
                    3.1.1
                    
                        config/checkStyle/google_checks.xml
                        config/checkStyle/suppressions.xml
                        UTF-8
                        true
                        false
                        true
                        false
                    
                    
                        
                            validate
                            validate
                            
                                true
                                true
                            
                        
                    
                
                
                    io.fabric8
                    docker-maven-plugin
                    ${docker.plugin.version}
                    
                        ${docker.host}
                        ${docker.registry}
                        
                            
                                ${docker.username}
                                ${docker.password}
                            
                        
                        
                            
                                ${docker.registry}/${docker.namespace}/${project.name}:${project.version}
                                
                                    ${project.basedir}/Dockerfile
                                
                            
                        
                    
                
            
        
    

    
        
            
                org.apache.maven.plugins
                maven-checkstyle-plugin
            
        
    
    
        
            nexus-releases
            Nexus Release Repository
            
            http://10.10.11.95/repository/maven-releases/
        
        
            nexus-snapshots
            Nexus Snapshot Repository
            
            http://10.10.11.95/repository/maven-snapshots/
        
    
    
        
            dev
            
                
                dev
            
            
                true
            
        
        
            test
            
                test
            
        
        
            prod
            
                prod
            
        
    
    
        sc2-common
    

  • 完整的common的pom.xml

    4.0.0
    sc2-common
    0.0.1-SNAPSHOT

    
        
        
            javax.servlet
            javax.servlet-api
        
        
            com.google.guava
            guava
        

        
        
            org.hibernate.validator
            hibernate-validator
        
        
            javax.validation
            validation-api
        

        
        
            org.springframework
            spring-context
        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-devtools
            true
        
        
            org.springframework
            spring-web
        
        
            io.github.openfeign
            feign-core
        
        
            org.springframework
            spring-webmvc
        
        
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
        
            org.springframework.cloud
            spring-cloud-starter-hystrix
            ${spring-cloud-hystrix-version}
        
        

        
        
            org.springframework.cloud
            spring-cloud-starter-hystrix-dashboard
            ${spring-cloud-hystrix-version}
        
        
        
            org.springframework.cloud
            spring-cloud-starter-turbine
            ${spring-cloud-hystrix-version}
            
                
                    com.netflix.eureka
                    eureka-client
                
            
        
        
        
            org.springframework.cloud
            spring-cloud-starter-ribbon
            ${spring-cloud-hystrix-version}
        

        
        
            org.springframework.cloud
            spring-cloud-starter-zipkin
        

        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
        
            org.springframework.cloud
            spring-cloud-context
        
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
        
            com.alibaba.spring
            spring-context-support
            ${spring-context-support-version}
        
        
            org.springframework.cloud
            spring-cloud-starter-alibaba-nacos-discovery
            ${nacos-version}
        

        
        
            org.postgresql
            postgresql
        
        
            org.apache.tomcat
            tomcat-jdbc
            ${tomcat-jdbc-version}
        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            ${mybatis-spring-boot-starter-version}
        

        
        
            com.fasterxml.jackson.core
            jackson-databind
        
        
        
            com.alibaba
            fastjson
            ${fastjson-version}
        
        
        
            log4j
            log4j
            ${log4j-version}
        

        
        
            io.springfox
            springfox-swagger-ui
            ${swagger2.version}
        
        
            io.springfox
            springfox-swagger2
            ${swagger2.version}
        
        
            io.swagger
            swagger-annotations
            ${swagger2-annotations.version}
        

        
        
            commons-beanutils
            commons-beanutils
            ${commons-beanutils-version}
            compile
        
        
            commons-fileupload
            commons-fileupload
            ${commons-fileupload-version}
        
        
            org.apache.commons
            commons-lang3
            ${commons-lang3-version}
        
        
            commons-codec
            commons-codec
            ${commons-codec-version}
        


        
        
            org.projectlombok
            lombok
        

        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
    
        com.bobycloud
        sc2-parent
        0.0.1-SNAPSHOT
    

 

  • deploy-api只需要简单配置即可

    4.0.0
    deploy-api

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
            
        
    

    
        
            com.bobycloud
            sc2-common
            0.0.1-SNAPSHOT
        
    

    
        com.bobycloud.webdeploy
        sc2-web-deploy
        ${revision}
    

 

  • deploy-core需要引入deploy-api

    4.0.0
    deploy-core
    war
    
    
        
        deploy
        
            
                org.apache.maven.plugins
                maven-checkstyle-plugin
            
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
            
        
        
        
            
                src/main/resources
                true
            
        
        
    
    

        
        
            com.bobycloud.webdeploy
            deploy-api
            ${project.version}
        


    
    
        com.bobycloud.webdeploy
        sc2-web-deploy
        ${revision}
    
  • 项目启动类将配置相关启动
/**
 * @ClassName: CoreApplication
 * @Description: 启动类
 * @author 作者
 * @date 2019年9月24日 下午5:58:01
 */
@Import({SwaggerConfiguration.class, IDSnowflakeCreate.class, IDRedisCreate.class, ExceptionResolver.class})
@MapperScan(basePackages = "com.bobycloud.deploy.service.dao.*")
@EnableDiscoveryClient
@EnableFeignClients
@EnableTurbine
@EnableHystrixDashboard
@EnableCircuitBreaker
@Configuration
@RefreshScope
@SpringBootApplication
public class CoreApplication {
    public static void main(String[] args) {
        SpringApplication.run(CoreApplication.class, args);
    }
}
  • 配置样例的启动文件 application.yaml
server:
  port: 9090
  servlet:
    context-path: /${spring.application.name}
spring:
  application:
    name: deploy
    admin:
      enabled: true
  profiles:
    active: dev
  cloud:
    nacos:
      discovery:
        server-addr: ${NACOS-HOST:172.16.30.184}:${NACOS-PORT:8858}
        metadata:
          management.context-path: /${spring.application.name}/actuator
      config:
        server-addr: ${spring.cloud.nacos.discovery.server-addr}
        file-extension: yml
        shared-configs:
          - ${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
  redis:
    #内网
    host: 172.16.30.184
    port: 6379  #可不配,因为底层默认值为6379
  datasource:
    type: org.apache.tomcat.jdbc.pool.DataSource
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://172.16.30.184:5432/qa_theach
    username: qa_theach
    password: 'qa_theach'
    tomcat:
      testWhileIdle: true
      testOnBorrow: true
      testOnReturn: false
      validationInterval: 30000
      timeBetweenEvictionRunsMillis: 30000
      maxActive: 100
      minIdle: 10
      maxWait: 10000
      initialSize: 10
      removeAbandonedTimeout: 60
      removeAbandoned: true
      logAbandoned: true
      minEvictableIdleTimeMillis: 30000
      jmxEnabled: true
  boot:
    admin:
      client:
        url: "http://localhost:8080"
  mail:
    host: smtp.126.com
    username:
    password:
  zipkin:
    service:
      name: /${spring.application.name}
    base-url: http://172.16.30.184:9411
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS
swagger:
  servletPath: /${spring.application.name}
  basePackage: 'com.bobycloud.deploy'
  title: 'deploy项目API'
  desc: '最终解释权归研发部(软件组)所有'
  version: '1.0'
  serviceTermsUrl: '服务条款对应的网址'
  contactName: '研发部-软件组'
  contactEmail: '[email protected]'
  contactUrl: '作者网址'
  license: '许可'
  licenseUrl: '许可对应的网址'
#id
ids:
  snowflake:
    datacenterId: 1
    workerId: 1
  redis:
    step: 3
feign:
  hystrix:
    enabled: true
#hystrix的超时时间
hystrix:
  command:
    default:
      execution:
        timeout:
          enabled: true
        isolation:
          thread:
            timeoutInMilliseconds: 9000
#ribbon的超时时间
ribbon:
  ReadTimeout: 3000
  ConnectTimeout: 3000
turbine:
  app-config: ${spring.application.name}
  cluster-name-expression: metadata['cluster']
  aggregator:
    clusterConfig: CLOUD-SIMPLE-SERVICE  #turbine监控的服务名称,可以多个

 

 

 

 

你可能感兴趣的:(技术之路)