Skip to content

dependency and bootstrap

chengyouling edited this page Sep 11, 2024 · 7 revisions

微服务依赖和boostrap配置

使用 Dependency Management 管理依赖

为了减少在微服务迁移过程中,由于第三方依赖包兼容或者多版本引起的问题,优先在应用pom.xml使用Dependency Management来管理依赖项,具体如下:

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

	<!-- configure spring cloud huawei version -->
	<dependency>
	  <groupId>com.huaweicloud</groupId>
	  <artifactId>spring-cloud-huawei-bom</artifactId>
	  <version>${spring-cloud-huawei.version}</version>
	  <type>pom</type>
	  <scope>import</scope>
	</dependency>
  </dependencies>
</dependencyManagement>

或者

<dependencyManagement>
  <dependencies>
	<!-- configure spring cloud huawei version -->
	<dependency>
	  <groupId>com.huaweicloud</groupId>
	  <artifactId>spring-cloud-huawei-dependencies</artifactId>
	  <version>${spring-cloud-huawei.version}</version>
	  <type>pom</type>
	  <scope>import</scope>
	</dependency>
  </dependencies>
</dependencyManagement>

版本依赖:

<properties>
  <spring.version>5.3.23</spring.version>
  <spring-boot.version>2.6.13</spring-boot.version>
  <spring-cloud.version>2021.0.5</spring-cloud.version>
  <spring-cloud-huawei.version>1.10.11-2021.0.x</spring-cloud-huawei.version>
</properties>

展示的版本依赖仅为示例,对于新项目推荐使用最新版本,具体详情见Supported Version

根据服务类型和中间件选型确定POM依赖

根据实际应用场景,通常分为WebMvc微服务、WebFlux微服务、网关微服务3种微服务类型。这3种微服务类型可以搭配ServiceComb或者Nacos作为注册中心和配置中心。

  • WebMvc微服务 + ServiceComb
<dependencies>
  <dependency>
	<groupId>com.huaweicloud</groupId>
	<artifactId>spring-cloud-starter-huawei-service-engine</artifactId>
  </dependency>
</dependencies>
  • WebFlux微服务 + ServiceComb
<dependencies>
  <dependency>
	<groupId>com.huaweicloud</groupId>
	<artifactId>spring-cloud-starter-huawei-service-engine-webflux</artifactId>
  </dependency>
</dependencies>
  • 网关微服务 + ServiceComb
<dependencies>
  <dependency>
	<groupId>com.huaweicloud</groupId>
	<artifactId>spring-cloud-starter-huawei-service-engine-gateway</artifactId>
  </dependency>
</dependencies>
  • WebMvc微服务 + Nacos
<dependencies>
  <dependency>
	<groupId>com.huaweicloud</groupId>
	<artifactId>spring-cloud-starter-huawei-nacos</artifactId>
  </dependency>
</dependencies>
  • WebFlux微服务 + Nacos
<dependencies>
  <dependency>
	<groupId>com.huaweicloud</groupId>
	<artifactId>spring-cloud-starter-huawei-nacos-webflux</artifactId>
  </dependency>
</dependencies>
  • 网关微服务 + Nacos
<dependencies>
  <dependency>
	<groupId>com.huaweicloud</groupId>
	<artifactId>spring-cloud-starter-huawei-nacos-gateway</artifactId>
  </dependency>
</dependencies>

使用zuul作网关

Zuul网关目前已经不再提供支持。鉴于很多微服务搬迁仍然延用zuul作为网关的需求,spring-cloud-huawei框架提供1.9.4-Hoxton进行支持使用,具体依赖如下:

<dependencies>
  <dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
	<version>2.2.10.RELEASE</version>
  </dependency>
  <dependency>
	<groupId>com.huaweicloud</groupId>
	<artifactId>spring-cloud-starter-huawei-service-engine</artifactId>
  </dependency>
</dependencies>

推荐版本:

<properties>
  <spring-boot.version>2.3.12.RELEASE</spring-boot.version>
  <spring-cloud.version>Hoxton.SR12</spring-cloud.version>
  <spring-cloud-huawei.version>1.9.4-Hoxton</spring-cloud-huawei.version>
</properties>

bootstrap.yml配置

bootstrap.yml指定了应用程序启动必须的信息,这些信息通常无法通过配置中心下发,只能使用环境变量和配置文件 进行配置。 bootstrap.yml的信息包括:

  • 微服务的基本信息:应用名称,微服务名称,微服务版本,环境,其他属性。
  • 微服务实例的基本信息:初始状态,其他属性。
  • 注册中心信息:注册中心地址及其参数。
  • 配置中心信息: 配置中心地址及其参数, 包括配置中心类型等。

ServiceComb

spring:
  application:
    # 微服务名称。 微服务名称定义好以后,后续不能变更。
    name: basic-provider
  cloud:
    servicecomb:
      # 微服务的基本信息
      service:
        # 微服务名称,和spring.application.name保持一致。
        name: ${spring.application.name}
        # 微服务版本号,本示例使用ServiceStage环境变量。建议保留这种配置方式,部署的时候,不用手工修改版本号,防止契约注册失败。
        # 注意:如果配置文件没有CAS_INSTANCE_VERSION环境变量,SDK框架升级、契约改变的时候,手动更改版本号,否则无法更新。
        version: ${CAS_INSTANCE_VERSION:0.0.1}
        # 应用名称。默认情况下只有应用名称相同的微服务之间才可以相互发现。
        application: ${CAS_APPLICATION_NAME:basic-application}
        # 环境名称。只有环境名称相同的微服务之间才可以相互发现。
        # 可以取值 development, testing, acceptance, production
        environment: production
      # 注册发现相关配置         
      discovery:
        # 注册中心地址,本示例使用ServiceStage环境变量。建议保留这种配置方式,
        # 部署的时候,不用手工修改地址。
        address: ${PAAS_CSE_SC_ENDPOINT:http://127.0.0.1:30100}
        # 微服务向CSE发送心跳间隔时间,单位秒
        # 注意:注册中心对服务实例健康检查间隔时间最小5秒,SDK设置小于5秒时,以5秒作为检测时间
        healthCheckInterval: 15
        # 拉取实例的轮询时间,单位毫秒
        pollInterval: 15000
        # 优雅停机设置。优雅停机后,先从注册中心注销自己。这个时间表示注销自己后等待的时间,这个时间后才退出。
        waitTimeForShutDownInMillis: 15000
      config:
        # 配置中心地址,本示例使用ServiceStage环境变量。建议保留这种配置方式,
        # 部署的时候,不用手工修改地址。
        serverAddr: ${PAAS_CSE_CC_ENDPOINT:http://127.0.0.1:30110}
        serverType: kie
        # 自定义配置
        kie:
          customLabel: ${spring.application.name}
          customLabelValue: ${INSTANCE_TAG:default}
        # 自定义配置,使用文本的key/value配置项作为yaml格式配置
        fileSource: governance.yaml,application.yaml
      dashboard:
        # 仪表盘配置,本地开发默认不开启
        enabled: false
        address: ${PAAS_CSE_DS_ENDPOINT:http://127.0.0.1:30109}

注意:以上配置模板要求适配版本大于等于1.10.3-2021.0.x,更低版本下载源码查看DiscoveryBootstrapProperties内容适配下发配置

Nacos

spring:
  application:
    # 微服务名称。 微服务名称定义好以后,后续不能变更。
    name: basic-provider
  cloud:
    nacos:
      discovery:
        enabled: true
        server-addr: http://127.0.0.1:8848
        service: ${spring.application.name}
      config:
        enabled: true
        server-addr: http://127.0.0.1:8848
        service: ${spring.application.name}
Clone this wiki locally