1. 程式人生 > >SpringCloud 多版本以及灰度釋出使用

SpringCloud 多版本以及灰度釋出使用

灰度釋出使用:

灰度服務管理服務:

依賴jar包
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>
org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId
>
</dependency> <dependency> <groupId>cn.springcloud.gray</groupId> <artifactId>spring-cloud-starter-gray-server</artifactId> </dependency> </dependencies>
application.yml
spring:
  application:
    name: gray-server
server: port: 20202 eureka: client: register-with-eureka: true fetch-registry: true serviceUrl: defaultZone: http://localhost:8080/eureka/
啟動類新增@EnableGrayServer
@SpringBootApplication
@EnableEurekaClient
@EnableGrayServer
public class GrayServerApplication
{
    public static void main( String[] args )
    {
        SpringApplication.run(GrayServerApplication.class,args);
    }
}

業務服務端

依賴jar包
    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-feign</artifactId>
            </dependency>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>3.5</version>
            </dependency>

            <dependency>
                <groupId>cn.springcloud.gray</groupId>
                <artifactId>spring-cloud-starter-gray</artifactId>
            </dependency>
        </dependencies>
application.yml
gray:
  client:
    instance:
      grayEnroll: false #是否在啟動後自動註冊成灰度例項
    serverUrl: http://localhost:20202 #灰度服務端的url

呼叫策略組方法:
POST /gray/manager/services/{serviceId}/instance/policyGroup
引數:

serviceId:service0
policyGroupFO:{
  "alias": "1",
  "enable": true,
  "instanceId": "service0:8081",
  "policies": [
    {
      "infos": {"v":"1"},
      "policyId": "1",
      "policyType": "REQUEST_PARAMETER"
    }
  ],
  "policyGroupId": "1"
}

上述策略描述的是: 對服務service0的service0:8081例項添加了策略組。當請求引數中有v=1時,請求路由到該例項。
以上引數中最重要的是:
serviceId,instanceId,infos,poliyType,policyGroupId.
serviceId:服務名
instanceId:服務名:埠號
infos:是一個map
policyType:列舉字串,就四個值:
REQUEST_PARAMETER:請求引數策略
REQUEST_HEADER:請求頭策略
REQUEST_IP :ip策略
CONTEXT_PARAMS 上下文引數策略