不积跬步,无以至千里;不积小流,无以成江海。

spring cloud 项目实战 – 项目构建

SpringCloud 康康 209℃ 0评论

本地host文件添加映射

127.0.0.1 eureka-master

eureka-server 注册中心

pom.xml:

<?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>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>eureka-server</artifactId>
    <version>1.0.RELEASE</version>
    <name>eureka-server</name>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>


    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

配置文件

server.port=2222
eureka.instance.hostname=eureka-master
eureka.instance.prefer-ip-address=true
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false

zuul-router 网关

所有外部的请求全部通过网关进行路由转发,同时在网关进行权限校验,服务service-xxx内部互相调用不走网关

pom.xml

<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>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <groupId>top.springcloud.ZuulRouter</groupId>
    <artifactId>zuul-router</artifactId>
    <version>1.0.RELEASE</version>
    <packaging>jar</packaging>

    <name>zuul-router</name>
    <url>http://maven.apache.org</url>

    <properties>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <!--公共实体以及通用的工具类-->
        <dependency>
            <groupId>com.fm.vege</groupId>
            <artifactId>base-entity</artifactId>
            <version>1.0-RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
        <!--redis 缓存相关-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

配置文件

application.yml

spring:
  application:
    name: zuul-gateway
  profiles:
    active: dev
#配置日志-使用外部配置文件
logging:
  config: classpath:logback-spring.xml

#重设feign超时
ribbon:
  ReadTimeout: 8000
  ConnectTimeout: 8000
  MaxAutoRetries: 0
  MaxAutoRetriesNextServer: 1
hystrix:
  command:
    c4i-store:
      execution:
        timeout:
          enabled: true
        isolation:
          thread:
            timeoutInMilliseconds: 10000
            ribbon:
              ReadTimeout: 10000
              ConnectTimeout: 10000

application-dev.yml

server:
  port: 9000
eureka:
  instance:
    prefer-ip-address: true
  client:
    serviceUrl:
      defaultZone: http://eureka-master:2222/eureka/
zuul:
  host:
    socket-timeout-millis: 5000
    connect-timeout-millis: 4000
  routes:
    #针对url路径 选择路由的服务
    user-api:
      path: /user-api/**
      serviceId: service-user
      sensitiveHeaders: "*"
    order-api:
      path: /order-api/**
      serviceId: service-order
      sensitiveHeaders: "*"
    goods-api:
      path: /goods-api/**
      serviceId: service-goods
      sensitiveHeaders: "*"
    pay-api:
      path: /pay-api/**
      serviceId: service-pay
      sensitiveHeaders: "*"
    web-api:
      path: /web/**
      serviceId: admin-web
      sensitiveHeaders: "*"

service-user 服务(服务就拿这个举例)

pom.xml

<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>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <name>service-user</name>
    <groupId>com.fm.vege</groupId>
    <artifactId>service-user</artifactId>
    <version>1.0.RELEASE</version>
    <packaging>jar</packaging>


    <properties>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
        <HikariCP.version>3.2.0</HikariCP.version>
        <mysql.connector.version>5.1.46</mysql.connector.version>
    </properties>


    <dependencies>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.5</version>
        </dependency>
        <!-- 分页助手 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.3</version>
        </dependency>
       <!--公共实体以及通用的工具类-->
        <dependency>
            <groupId>com.fm.vege</groupId>
            <artifactId>base-entity</artifactId>
            <version>1.0-RELEASE</version>
        </dependency>
        <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.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.2</version>
        </dependency>
        <!-- HikariCP连接池 -->
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <version>${HikariCP.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.connector.version}</version>
        </dependency>
        <!--redis 缓存相关-->
        <dependency>
           <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
       </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

配置文件

application.yml

spring:
  application:
    name: service-user
  profiles:
    active: dev
#hystrix的超时时间
hystrix:
    command:
        default:
            execution:
              timeout:
                enabled: true
              isolation:
                    thread:
                        timeoutInMilliseconds: 6000
ribbon:
  ReadTimeout: 5000
  ConnectTimeout: 5000

application-dev.yml

server:
  port: 8081

eureka:
  instance:
    prefer-ip-address: true
  client:
    serviceUrl:
      #注册中心集群
      defaultZone: http://eureka-master:2222/eureka/
    #instance:
    #instance-id: ${spring.application.name}:${random.int}

spring:
  datasource:
    platform: mysql
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://172.31.121.149:3306/fm_user_db?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: Nongshu@113
    hikari:
      maxLifetime: 1765000
      maximumPoolSize: 15
  redis:
    host: 172.31.121.149
    port: 6379
    password: Nongshu@113
    timeout: 6000ms
    lettuce:
      pool:
        max-active: 8
        max-wait: -1ms
        max-idle: 8
        min-idle: 0
    database: 0
#分页组件
pagehelper:
  helperDialect: mysql
  reasonable: true
  supportMethodsArguments: true
  params: count=countSql

mybatis:
  mapperLocations: classpath:mapper/*.xml
  type-aliases-package: com.fm.vege.user.mapper.po

#配置日志-使用外部配置文件
logging:
  config: classpath:logback-spring.xml

 

日志配置(logback)

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
   <!-- 定义变量 , 文件输出格式  -->
   <springProperty scope="context" name="app.name" source="spring.application.name" defaultValue="app-service"/>
   <property name="PATTERN" value="%-12(%d{yyyy-MM-dd HH:mm:ss.SSS}) |-%-5level [%thread] %c [%L] -| %msg%n" />
    <!-- 定义变量 ,日志文件输出路径 -->
   <property name="logs.path" value="logs" />

   <!-- 输出到控制台 -->
   <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
      <encoder>
         <pattern>${PATTERN}</pattern>
      </encoder>
   </appender>

   <!--错误日志单独输出-->
   <appender name="FILE-ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
      <file>${logs.path}/${app.name}-error.log</file>
      <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
          <!-- 文件名称 -->
         <fileNamePattern>${logs.path}/${app.name}-error.%i.log</fileNamePattern>
         <minIndex>1</minIndex>
         <maxIndex>10</maxIndex>
       </rollingPolicy>
      <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
         <maxFileSize>20MB</maxFileSize>
      </triggeringPolicy>
      <layout class="ch.qos.logback.classic.PatternLayout">
         <pattern>${PATTERN}</pattern>
      </layout>
   </appender>


   <!-- 输出到文件 -->
   <appender name="FILE-INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
      <!-- 文件路径 -->
      <file>${logs.path}/${app.name}-info.log</file>
      <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
         <!-- 文件名称 -->
         <fileNamePattern>${logs.path}/${app.name}-info.%i.log</fileNamePattern>
         <minIndex>1</minIndex>
         <maxIndex>10</maxIndex>
      </rollingPolicy>
      <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
         <maxFileSize>20MB</maxFileSize>
      </triggeringPolicy>
      <layout class="ch.qos.logback.classic.PatternLayout">
         <pattern>${PATTERN}</pattern>
      </layout>
   </appender>

   <!--开发环境日志输出到控制台-->
   <springProfile name="dev">
       <root level="info">
         <appender-ref ref="CONSOLE" />
         <appender-ref ref="FILE-INFO" />
         <appender-ref ref="FILE-ERROR" />
       </root>
      <logger name="com.fm.vege" level="info" />
   </springProfile>
     
    <!--测试环境  -->  
    <springProfile name="test">  
         <root level="info">
          <appender-ref ref="CONSOLE" />
          <appender-ref ref="FILE-INFO" />
          <appender-ref ref="FILE-ERROR" />
         </root>
      <logger name="com.fm.vege" level="info" />
    </springProfile>
    
    <!-- 生产环境-->
    <springProfile name="prod">  
     <root level="error">
       <appender-ref ref="FILE-INFO" />
     </root>
      <logger name="com.fm.vege" level="info" />
    </springProfile>
     
 </configuration>

 

转载请注明:左手代码右手诗 » spring cloud 项目实战 – 项目构建

喜欢 (1)or分享 (0)
头像
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
隐藏