zoukankan      html  css  js  c++  java
  • springcloud 新增微服务

    个人记录

    记录公司微服务项目,模块添加的步骤

    一  创建Module

    选择maven

    groupid和artifactid 参考 pom文件

    <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>
            <!-- 父项目 -->
            <artifactId>kps.parent</artifactId>
            <!-- 父项目id -->
            <groupId>com.kps</groupId>
            <version>1.0</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
     
        <!-- 项目id -->
        <artifactId>kps.appAPIScan</artifactId>
        <!-- 项目名 -->
        <name>kps.appAPIScan</name>
        <url>http://maven.apache.org</url>
        <properties>
            <imageName>apiscan:${project.version}</imageName>
        </properties>
     
     
        <dependencies>
            <dependency>
                <groupId>com.kps</groupId>
                <artifactId>kps.web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
            <!--
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-netflix-eureka-client</artifactId>
            </dependency>
            -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-config-client</artifactId>
            </dependency>
     
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
     
        </dependencies>
     
        <!-- 打boot结构jar插件  -->
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
                <plugin>
                    <groupId>com.spotify</groupId>
                    <artifactId>docker-maven-plugin</artifactId>
                    <configuration>
                        <imageName>${imageName}</imageName>
                    </configuration>
                </plugin>
            </plugins>
        </build>
     
    </project>

    二  增加启动应用

    这里注意,要和controller文件夹同级

    代码:

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.ServletComponentScan;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.context.annotation.ComponentScan;
     
    @EnableEurekaClient // 配置本应用将使用服务注册和服务发现
    @SpringBootApplication
    @ComponentScan(basePackages={"com.kps"},lazyInit=true)
    @ServletComponentScan(basePackages={"com.kps"})
    public class AppAPIScanApp {
        public static void main( String[] args )
        {
            SpringApplication.run(AppAPIScanApp.class, args);
        }
    }

    PS:如果SpringApplication报错:cannot access org.springframework.core.env

    那么把Module重新导入一下就好了。

    三  增加配置文件

    application.yml

    spring:
       application:
           name: kps.appAPIScan
     
    server:
       port: 9100
     
    #Security authentication is enabled by default above springboot 1.5.x
    management:
       security:
          enabled: false
     
     
    eureka:
        client:
            serviceUrl:
               defaultZone: ${registry.url}
        instance:
              lease-expiration-duration-in-seconds: 60
              lease-renewal-interval-in-seconds: 30
              preferIpAddress: true
              instanceId: ${spring.cloud.client.ipAddress}:${server.port}

    bootstrap.yml

    spring:
        http:
            encoding:
                charset: UTF-8
                enabled: true
                force: true
        cloud:
            config:
                uri: http://${host:localhost}:9089
                name: config
                profile: ${active:dev}

    四  修改zuul网管配置

    application.yml

    spring:
       profiles:
          active: ${active:dev}
       application:
          name: springCloud.zuul
       thymeleaf:
               prefix: classpath:/templates/
               suffix: .html
               mode: LEGACYHTML5
               encoding: UTF-8
               content-type: text/html
               cache: false
     
     
    server:
       port: 9090
    #   context-path: /zuul
     
    #Security authentication is enabled by default above springboot 1.5.x
    management:
       security:
          enabled: false
          
    eureka:
       client:
          serviceUrl:
                defaultZone: ${registry.url}      
          registry-fetch-interval-seconds: 5 # 拉取服务注册信息间隔时间 (默认为30秒)
       instance:
              lease-expiration-duration-in-seconds: 60  # 注册中心超过这个时间没收到心跳,会视为无效节点(默认为90秒)
              lease-renewal-interval-in-seconds: 30  # 发送心跳间隔时间(默认30秒)
              preferIpAddress: true
              instanceId: ${spring.cloud.client.ipAddress}:${server.port}      
          
     
    zuul:
      add-host-header: true #webui重定向  请求头host显示为网关的(eg:localhost:9090)而非webui的
      ignoredServices: '*' #禁用服务名路游
      sensitive-headers:   #传递头信息
      retryable: true  #负载均衡时,路游的服务重启时,可通过重试到其他相同服务
    #  host:
    #     socket-timeout-millis: 60000
    #     connect-timeout-millis: 60000
      routes:
          sys:
             path: /sys/**
             serviceId: kps.webAPISYS
          common:
             path: /common/**
             serviceId: kps.webAPICommon
          po:
             path: /po/**
             serviceId: kps.webAPIPO
          wms:
             path: /wms/**
             serviceId: kps.webAPIWMS  
          eq:
             path: /eq/**
             serviceId: kps.webAPIEQ      
          kps:
             path: /kps/**
             serviceId: kps.webUI
          helka:
             path: /helka/**
             serviceId: kps.helka
          abc:
             path: /abc/**
             serviceId: kps.abc
          app:
            path: /app/**
            serviceId: kps.appAPIScan
     
     

    其实只增加了

        app:
            path: /app/**
            serviceId: kps.appAPIScan

    五  看一下config中心

    如果需要feign接口,那么就需要配置config,

    (这里我没有配置,因为不需要)

    看一下 config-dev

     #registry url
    registry:
       url: http://${host:localhost}:9088/eureka/
     
     
    ## DataSource
    spring:
        datasource:
            url: ***
            username: ***
            password: ***
        jpa:
           generate-ddl: false
           hibernate:
              ddl-auto: none
           database: mysql
           show-sql: false
        resources:
           chain:
              strategy:
                 content:
                    enabled: true
                    paths: /**
     
        redis:
           host: ***
           database: 1
           pool:
              max-active: 20
              min-idle: 1
              max-idle: 1
              max-wait: 1
     
    #druid connect pool
    db:
      druid:
         url: ${spring.datasource.url}
         username: ${spring.datasource.username}
         password: ${spring.datasource.password}
         filters: stat,wall
         max-active: 60
         initial-size: 10
         max-wait: 60000
         min-idle: 10
         time-between-eviction-runs-millis: 600000
         min-evictable-idle-time-millis: 300000
         test-while-idle: true
         test-on-borrow: false
         test-on-return: false
         pool-prepared-statements: false
         max-open-prepared-statements: 20
     
    #file
    file:
       accessPath: /file/**  #访问文件前缀
       uploadFolder: d://uploadFiles/  #上传文件存放路径
       
    isDebug: false
     
    #app scan
    appSign: ***
     
    feign:
        sysService: kps.webAPISYS
        poService: kps.webAPIPO
        wmsService: kps.webAPIWMS
        commonService: kps.webAPICommon
        eqService: kps.webAPIEQ
        
    kintechWebAPIURL: http://localhost:9004/kps/api/

    application.yml

    spring:
       profiles:
          active: native
       application:
              name: springCloud.config
     
    server:
        port: 9089
     
    endpoints:
       health:
         enabled: true
     
    #Security authentication is enabled by default above springboot 1.5.x
    management:
        security:
           enabled: false
           
    info:
      author: cjh
      fileName: config
      
     
  • 相关阅读:
    HTML页面跳转的5种方法
    利用jquery来进行表单的多向提交
    HTML设置超链接字体颜色和点击后的字体颜色
    bootstrap精简教程
    类型的转换的神秘
    [转载] mysql 索引中的USING BTREE 的意义
    [转载]WeeksInAYear、WeeksInYear、DaysInAYear、DaysInAMonth、DaysInYear、DaysInMonth
    范围内取随机数
    [转载]asp.net中,<%#%>,<%=%>和<%%>分别是什么意思,有什么区别
    [转载]localStorage使用总结
  • 原文地址:https://www.cnblogs.com/hanjun0612/p/10248377.html
Copyright © 2011-2022 走看看