SpringCloud 简介
SpringCloud是一个基于SpringBoot实现的微服务架构开发工具。它为微服务架构中涉及的配置管理、服务治理、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式会话和集群状态管理等操作提供了一种简单的开发方式。

1.vspcloud-starter-parent.pom
 vspcloud-starter-parent是一个父工程,他是管理jar包的版本号,避免jar包不兼容。 <?xml version="1.0" encoding="UTF-8"?> 
 4.0.0
 com.reachauto.vspcloud
 vspcloud-starter-parent
 1.1.0
 pomvspcloud-starter-parent-super-pom
 VspCloud Platform org.springframework.boot spring-boot-starter-parent 2.1.7.RELEASE 
 org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import com.alibaba.cloud spring-cloud-alibaba-dependencies ${spring-cloud-alibaba.version} pom import 
 mysql mysql-connector-java ${mysql.version} 
 org.springframework spring-jdbc ${spring-jdbc.version} com.baomidou mybatis-plus-boot-starter ${mybatis-plus.version} com.alibaba druid-spring-boot-starter ${druid.version} org.springframework.boot spring-boot-starter-web ${spring-boot.version} 
 com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery 
 com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config 
 org.springframework.cloud spring-cloud-starter-netflix-ribbon 
 org.springframework.cloud spring-cloud-starter-openfeign io.github.openfeign feign-okhttp 
 com.alibaba.cloud spring-cloud-starter-alibaba-sentinel com.alibaba.csp sentinel-datasource-nacos ${sentinel-nacos.version} 
 org.springframework.boot spring-boot-starter-actuator 
 org.springframework.boot spring-boot-starter-test test org.mockito mockito-core test org.mockito mockito-inline test org.mockito mockito-junit-jupiter test 
 org.projectlombok lombok 
 com.google.code.gson gson 
 com.fasterxml.jackson.core jackson-core ${jackson.version} com.fasterxml.jackson.core jackson-databind ${jackson.version} com.fasterxml.jackson.core jackson-annotations ${jackson.version}


<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>${alibaba.fastjson.version}</version>
</dependency>
<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
  <groupId>commons-io</groupId>
  <artifactId>commons-io</artifactId>
  <version>${apache.commons-io.version}</version>
</dependency>
<dependency>
  <groupId>commons-beanutils</groupId>
  <artifactId>commons-beanutils</artifactId>
  <version>${apache.commons.beanutils}</version>
</dependency>

compile ${project.artifactId} ${basedir}/src/main/resources true ${basedir}/src/main/java **/*.java **/*.xml org.springframework.boot spring-boot-maven-plugin org.apache.maven.plugins maven-compiler-plugin ${java.version} ${java.version} org.springframework.boot spring-boot-maven-plugin repackage org.apache.maven.plugins maven-archetype-plugin ${maven-archetype-plugin.version} org.codehaus.mojo sonar-maven-plugin ${sonar-maven-plugin.version} 2.vspcloud-common-core vspcloud-common-core项目是一个公共方法api 
3.vspcloud-common-exception
 vspcloud-common-exception项目是一个异常捕获4.vspcloud-common-log
 vspcloud-common-log项目是一个日志管理,用logback拦截输出日志5.vspcloud-vsptrunk-test
 vspcloud-vsptrunk-test.pom <?xml version="1.0" encoding="UTF-8"?> 
 4.0.0

 vspcloud-starter-parent
 com.reachauto.vspcloud
 1.1.0<artifactId>vspcloud-vsptrunk-test</artifactId>
<version>0.0.1</version>
<properties>
    <java.version>1.8</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <artifactId>vspcloud-common-exception</artifactId>
        <groupId>com.reachauto.vspcloud</groupId>
        <version>1.1.0</version>
    </dependency>

    <dependency>
        <artifactId>vspcloud-common-core</artifactId>
        <groupId>com.reachauto.vspcloud</groupId>
        <version>1.0.1</version>
    </dependency>

    <dependency>
        <artifactId>vspcloud-component-redis</artifactId>
        <groupId>com.reachauto.vspcloud</groupId>
        <version>1.0.0</version>
    </dependency>
   
</dependencies>
<artifactId>vspcloud-vsptrunk-test</artifactId>
<version>0.0.1</version>
<properties>
    <java.version>1.8</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <artifactId>vspcloud-common-exception</artifactId>
        <groupId>com.reachauto.vspcloud</groupId>
        <version>1.1.0</version>
    </dependency>

    <dependency>
        <artifactId>vspcloud-common-core</artifactId>
        <groupId>com.reachauto.vspcloud</groupId>
        <version>1.0.1</version>
    </dependency>

    <dependency>
        <artifactId>vspcloud-component-redis</artifactId>
        <groupId>com.reachauto.vspcloud</groupId>
        <version>1.0.0</version>
    </dependency>
   
</dependencies>

6去除业务微服务POM中的maven打包逻辑、部署仓库描述和远程仓库描述
添加到本地的settings.xml文件中

alimaven
 aliyun maven
 https://maven.aliyun.com/repository/public central


 vspmaven
 vsp maven
 http://47.103.87.97:8083/nexus/content/groups/public/
 public




 aliMaven


 ali-maven
 aliyun maven
 https://maven.aliyun.com/repository/public

 true


 true





 vspMaven


 vsp-maven
 vsp maven
 http://47.103.87.97:8083/nexus/content/groups/public/

 true


 true






 aliMaven
 vspMaven7.bootstrap.yml
 spring:
 application:
 name: vspcloud-vsptrunk-test
 cloud:
 nacos:
 discovery:
 server-addr: 10.10.165.164:8848
 config:
 prefix: {spring.profiles.active}.yml
 group: VSP_GROUP
 refresh: true
 profiles:
 active: dev
 ribbon:
 #对当前实例的重试次数
 MaxAutoRetries: 0
 #切换实例的重试次数
 MaxAutoRetriesNextServer: 0
 #对所有操作请求都进行重试
 OkToRetryOnAllOperations: false
 #启用okhttp
 okhttp:


enabled: true

actuator 健康检测 路径;http://127.0.0.1:10088/actuator/health

management:

查看详细的应用健康信息需要配置 除了always之外还有when-authorized、never,默认值是never。

endpoint:
health:
show-details: always

配置启用所有的监控端点,默认情况是禁用的 可以单独启用,例如,health,info,metrics等

endpoints:
 web:
 exposure:
 include: “*”
 #路径;http://127.0.0.1:10088/actuator/
 endpoints:
 #启用/ refresh端点刷新配置并重新初始化刷新作用域bean。
 refresh:
 enabled: true
 #通过POST将环境更改为/ env。
 env:
 post:
 enabled: true
 #启用/ restart端点重新启动应用程序上下文
 restart:
 enabled: true
 info:
 version: “@project.version@”
 description: “@project.description@”
 8.application.yml
 server:tomcat配置
tomcat:
 max-threads: 200
 min-spare-threads: 100
 accept-count: 100
 max-connections: 1000
 uri-encoding: UTF-8
 max-http-post-size: 4096
 servlet:
 context-path: /
 port: ${vspcloud.vspcloud-vsptrunk-test.port}allow-bean-definition-overriding 置为true时,后定义的bean会覆盖之前定义的相同名称的bean
spring:
 main:
 allow-bean-definition-overriding: true
 cloud:
 sentinel:
 transport:
 dashboard: 10.10.142.128:9000
 # 指定和控制台通信的IP,若不配置,会自动选择一个IP注册
 #client-ip: 127.0.0.1
 # 指定和控制台通信的端口哦,默认值8719
 # 若不配置,会自动扫猫从8719开始扫猫,依次+1,知道值找到未被占用的端口
 #port: 8720
 datasource:
 ds:
 nacos:
 server-addr: nacos-server:8848
 dataId: ${spring.application.name}-flow-rules
 groupId: SENTINEL_GROUP
 rule-type: flow
 9.nacos
 Nacos核心提供两个功能:服务注册与发现,动态配置管理。
 \nacos\bin路径下有
 启动起来http://127.0.0.1:8848/nacos/ 账户和密码都是 nacos左上角加号是添加文件 data-id和group 分别对应yml的nacos配置。
 vspcloud-shared-config-dev.yml和vspcloud-vsptrunk-test.yml都在nacos里配置。
 vspcloud-shared-config-dev.yml 公共项目的配置属性
 vspcloud:
 vspcloud-vsptrunk-test:
 name: vspcloud-vsptrunk-testa
 port: 10088
 topic:
 history: vsptrunk-history-topic
 tcpgateway: VSPTRUNK-TCPGATEWAY-TOPIC
 max-file-size:
 history: 1048576
 collect: 1048576
 feign:
 sentinel:
 enabled: true
 client:
 config:
 default:
 connectTimeout: 2000
 readTimeout: 5000
 global:
 kafka:
 consumer:
 bootstrap-servers: kafka:9092
 group-id: your_group_id
 offset: earliest
 key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
 value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
 producer:
 bootstrap-servers: kafka:9092
 key-deserializer: org.apache.kafka.common.serialization.StringSerializer
 value-deserializer: org.apache.kafka.common.serialization.StringSerializer
 redis:
 host: 10.10.142.125
 port: 6379
 password:
 oss:
 endpoint: http://oss-cn-shanghai.aliyuncs.com
 access-key-id: LTAIa2hLUo17VOvY
 access-key-secret: VMpLzlTMLZcXYnfwPecpGwjcpP7Gv4
 aliyun:
 mq:
 accessKey: LTAI4FhjHFVwktXLJ6ENGAgp
 secretKey: THIL1nnMvbQjwxLnKzSlN71ufqcxBW
 nameSrvAddr: http://onsaddr.mq-internet-access.mq-internet.aliyuncs.com:80
 hdp:
 kafka:
 consumer:
 bootstrap-servers: hdc-data1.hadoop:6667,hdc-data2.hadoop:6667,hdc-data3.hadoop:6667
 group-id: vsptrunk-kafka-consumer
 producer:
 bootstrap-servers: hdc-data1.hadoop:6667,hdc-data2.hadoop:6667,hdc-data3.hadoop:6667

系统操作日志topic

operationlog:
 topic: vsp_portalHistory
 ota:
 mysql:
 url: jdbc:mysql://10.10.142.125:3306/fota?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=Asia/Shanghai
 dbname: fota-pre
 username: fota
 password: fota2019
 oss:
 bucket-name: honda-vsp
 proxy-enable: false
 proxy-host: proxy.neusoft.com
 proxy-port: 8080
 aliyun:
 mq:
 groupId: GID-VSP-FOTA-OTA-PUBLIC
 topic: VSP-FOTA-OTA-TEST
 user:
 redis:
 db: 0
 mysql:
 dbname: userinfo
 username: userinfo
 password: NsrUser2019
 url: jdbc:mysql://10.10.142.125:3306/userinfo?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=Asia/Shanghai&useSSL=false
 oss:
 bucket-name: vspclouduserservice
 proxy-enable: false
 proxy-host: proxy.neusoft.com
 proxy-port: 8080
 aliyun:
 mq:
 groupId: GID-VSPCLOUD-USER-SERVICE
 topic: TOPIC-USER-SERVICE-DEV
 app:
 redis:
 db: 0
 mysql:
 dbname: userinfo
 username: userinfo
 password: NsrUser2019
 url: jdbc:mysql://10.10.142.125:3306/userinfo?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=Asia/Shanghai&useSSL=false
 kafka:
 bootstrap-servers: hdc-data1.hadoop:6667
 consumer:
 group-id: test-consumer-group
 auto-offset-reset: earliest
 smsmodel: SMS_174811940
 app-datasyncservice:
 redis:
 db: 0
 mysql:
 dbname: userinfo
 username: userinfo
 password: NsrUser2019
 url: jdbc:mysql://10.10.142.125:3306/userinfo?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=Asia/Shanghai&useSSL=false
 kafka:
 bootstrap-servers: hdc-data1.hadoop:6667
 vehicle-topic: jv_vehicle
 vehicle-model-topic: jv_vehicleModel
 vehicle-owner-topic: jv_vehicleOwner
 consumer:
 group-id: test-consumer-group
 auto-offset-reset: earliest
 smsmodel: SMS_174811940
 vsptrunk:
 redis:
 db: 0
 mysql:
 dbname: vsp
 username: vsp
 password: NsrVsp2019
 url: jdbc:mysql://10.10.142.125:3306/vsp?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=Asia/Shanghai&useSSL=false
 hbase:
 zkParent: /hbase-unsecure
 zkHost: hdc-data1.hadoop,hdc-data2.hadoop,hdc-data3.hadoop
 zkPort: 2181
 appstore:
 mysql:
 url: jdbc:mysql://10.10.142.125:3306/appstore?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=Asia/Shanghai
 dbname: appstore
 username: appstore
 password: NsrAppStore2019
 oss:
 bucket-name: honda-vsp-appstore
 proxy-enable: false
 proxy-host: proxy.neusoft.com
 proxy-port: 8080datasync kafka topic settings
datasync:
 kafka:
 vehicle-topic: jv_vehicle
 vehicle-model-topic: jv_vehicleModel
 vehicle-owner-topic: jv_vehicleOwnervspcloud-vsptrunk-test.yml 自己项目需要的配置
 mybatis-plus:
 global-config:
 db-config:
 logic-delete-value: 1
 logic-not-delete-value: 0
 insert-strategy: not_empty
 update-strategy: not_empty
 select-strategy: not_empty
 mapper-locations: classpath:/sqlmapper/*Mapper.xml
 spring:
 datasource:
 type: com.alibaba.druid.pool.DruidDataSource
 driver-class-name: com.mysql.cj.jdbc.Driver
 url: ${vsptrunk.mysql.url}
 username: ${vsptrunk.mysql.username}
 password: ${vsptrunk.mysql.password}
 druid:
 filters: stat
 maxActive: 5
 initialSize: 5
 maxWait: 60000
 minIdle: 1
 timeBetweenEvictionRunsMillis: 60000
 minEvictableIdleTimeMillis: 300000
 validationQuery: SELECT 1 FROM DUAL
 testWhileIdle: true
 testOnBorrow: false
 testOnReturn: false
 poolPreparedStatements: true
 maxOpenPreparedStatements: 5
 redis:
 host: ${global.redis.host}
 port: ${global.redis.port}
 password: ${global.redis.password}
 database: ${vsptrunk.redis.db}10.Feign
 Feign是一个声明式的Web服务客户端,使用Feign可使得Web服务客户端的写入更加方便。
 它具有可插拔注释支持,包括Feign注解和JAX-RS注解、Feign还支持可插拔编码器和解码器、Spring Cloud增加了对Spring MVC注释的支持,并HttpMessageConverters在Spring Web中使用了默认使用的相同方式。Spring Cloud集成了Ribbon和Eureka,在使用Feign时提供负载平衡的http客户端加入pom文件

 org.springframework.cloud
 spring-cloud-starter-openfeign
 2.1.2.RELEASE@FeignClient(value = “${vspcloud.vspcloud-vsptrunk-remote.name}”, path = “/carCon”, configuration = MultipartSupportConfig.class)
 Value为项目名 path+mapping里的value拼接成接口路径
 public interface GatewayToCarConFeignApi {
 @PostMapping(value = “/authority”)
 Response authority(@RequestBody VehicleInformationParameter vehicleInformationParameter);
 }项目的主入口要加@EnableFeignClients(basePackages = “com.reachauto.vspcloud.feign”)
@EnableFeignClients(basePackages = “com.reachauto.vspcloud.feign”)
 public class Application {
 public static void main(String[] args) {
 SpringApplication.run(Application.class, args);
 }
 }11.Sentinel
 Sentinel,中文翻译为哨兵,是为微服务提供流量控制、熔断降级的功能,它和Hystrix提供的功能一样,可以有效的解决微服务调用产生的“雪崩”效应,为微服务系统提供了稳定性的解决方案。随着Hytrxi进入了维护期,不再提供新功能,Sentinel是一个不错的替代方案。通常情况,Hystrix采用线程池对服务的调用进行隔离,Sentinel才用了用户线程对接口进行隔离,二者相比,Hystrxi是服务级别的隔离,Sentinel提供了接口级别的隔离,Sentinel隔离级别更加精细,另外Sentinel直接使用用户线程进行限制,相比Hystrix的线程池隔离,减少了线程切换的开销。另外Sentinel的DashBoard提供了在线更改限流规则的配置,也更加的优化。 com.alibaba.cloud spring-cloud-starter-alibaba-sentinel com.alibaba.csp sentinel-datasource-nacos ${sentinel-nacos.version} 可以从这个地址:https://github.com/alibaba/Sentinel/releases 下载控制台应用。因为下载速度较慢,给出一个我下载的版本(1.6.3) 运行命令启动控制台:java -Dserver.port=8333 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.4.2.jar • -Dserver.port 指定控制台的端口 • -Dproject.name 指定接入的应用名称 • -Dsentinel.dashboard.auth.username=sentinel 用于指定控制台的登录用户名为 sentinel; • -Dsentinel.dashboard.auth.password=123456用于指定控制台的登录密码为 123456;如果省略这两个参数,默认用户和密码均为 sentinel; • -Dserver.servlet.session.timeout=7200 用于指定 Spring Boot 服务端 session 的过期时间,如 7200 表示 7200 秒;60m 表示 60 分钟,默认为 30 分钟; • 打开浏览器,输入http://localhost:8080进入控制台页面(账号密码默认sentinel) spring: cloud: sentinel: transport: dashboard: 10.10.142.128:9000 # 指定和控制台通信的IP,若不配置,会自动选择一个IP注册 client-ip: 127.0.0.1 # 指定和控制台通信的端口哦,默认值8719 # 若不配置,会自动扫猫从8719开始扫猫,依次+1,知道值找到未被占用的端口 port: 8720 datasource: ds: nacos: server-addr: nacos-server:8848 dataId: ${spring.application.name}-flow-rules groupId: SENTINEL_GROUP rule-type: flow vspcloud-vsptrunk-test-flow-rules [ { "resource": "findVehicleStatus", "controlBehavior": 2, "count": 5, "grade": 1, "limitApp": "default", "strategy": 0 } ] resource 资源名,资源名是限流规则的作用对象 count 限流阈值 grade 限流阈值类型,QPS 或线程数模式 limitApp 流控针对的调用来源 strategy default,代表不区分调用来源 controlBehavior 流控效果(直接拒绝 / 排队等待 / 慢启动模式)


点击流控按钮,便可以为应用设置流控规则

• 资源名:唯一名称,默认请求路径
• 针对来源:Sentinel可以针对调用者进行限流,填写微服务名,默认default(不区分来源)
• 阈值类型/单机阈值:
o QPS(每秒钟的请求数量):当调用该api的QPS达到阈值的时候,进行限流
o 线程数:当调用该api的线程数达到阈值的时候,进行限流
• 是否集群:不需要集群,暂不研究
• 流控模式:
o 直接:api达到限流条件时,直接限流
o 关联:当关联的资源达到阈值时,就限流自己
o 链路:只记录指定链路上的流量(指定资源从入口资源进来的流量,如果达到阈值,就进行限流)【api级别的针对来源】
• 流控效果:
o 快速失败:直接失败,抛异常
o Warm Up:根据codeFactor(冷加载因子,默认3)的值,从阈值/codeFactor,经过预热时长,才达到设置的QPS阈值
o 排队等待:匀速排队,让请求以匀速的速度通过,阈值类型必须设置为QPS,否则无效

点击降级按钮,便可以为应用设置降级规则

降级策略:
• RT:平均响应时间(秒级统计)超出阈值且在时间窗口内的请求 >= 5时,触发降级;时间窗口结束后,关闭降级【Sentinel默认最大的RT为4900ms,可以通过-Dcsp.sentinel.statistic.max.rt=xxx修改】
• 异常比例:QPS >= 5 且异常比例(秒级统计)超过阈值时,触发降级;时间窗口结束后,关闭降级
• 异常数:异常数(分钟统计)超过阈值时,触发降级;时间窗口结束后,关闭降级【时间窗口 < 60秒可能会出现问题】