阿里云企业级互联网架构实践系列-EDAS 开发

2020-08-11  本文已影响0人  技术与健康

1. EDSA中使用spring cloud

如果需要在edas 中使用spring cloud 需要先配置maven 私服,更新maven依赖配置。基于

1.1. Maven 私服配置

注意: Maven 版本要求 3.x 及以上请在你的 Maven 配置文件 Setting 中,加入中间件私服地址

<?xml version="1.0"?>

<settings>

    <!-- 设置用户自己的本地库 -->

    <!-- <localRepository>/Users/../.m2/repository</localRepository> -->

    <profiles>

        <profile>

            <id>nexus</id>

            <repositories>

                <repository>

                    <id>central</id>

                    <url>http://repo1.maven.org/maven2</url>

                    <releases>

                        <enabled>true</enabled>

                    </releases>

                    <snapshots>

                        <enabled>true</enabled>

                    </snapshots>

                </repository>

            </repositories>

            <pluginRepositories>

                <pluginRepository>

                    <id>central</id>

                    <url>http://repo1.maven.org/maven2</url>

                    <releases>

                        <enabled>true</enabled>

                    </releases>

                    <snapshots>

                        <enabled>true</enabled>

                    </snapshots>

                </pluginRepository>

            </pluginRepositories>

        </profile>

        <profile>

            <id>edas.oss.repo</id>

                <repositories>

                <repository>

                    <id>edas-oss-central</id>

                    <name>taobao mirror central</name>

                    <url>http://edas-public.oss-cn-hangzhou.aliyuncs.com/repository</url>

                    <snapshots>

                        <enabled>true</enabled>

                    </snapshots>

                    <releases>

                        <enabled>true</enabled>

                    </releases>

                </repository>

            </repositories>

            <pluginRepositories>

                <pluginRepository>

                    <id>edas-oss-plugin-central</id>

                    <url>http://edas-public.oss-cn-hangzhou.aliyuncs.com/repository</url>

                    <snapshots>

                        <enabled>true</enabled>

                    </snapshots>

                    <releases>

                        <enabled>true</enabled>

                    </releases>

                </pluginRepository>

            </pluginRepositories>

        </profile>

    </profiles>

    <activeProfiles>

        <activeProfile>nexus</activeProfile>

        <activeProfile>edas.oss.repo</activeProfile>

    </activeProfiles>

</settings>

1.2. Maven 依赖

以下列出了几种依赖关系,不同的依赖针对代码编写也不一样。关于以下依赖的版本信息详见 Demo 工程中的父 pom 文件。

1.2.1. 通用依赖

最少依赖,必选。

  <dependency>

      <groupId>org.springframework.cloud</groupId>

      <artifactId>spring-cloud-starter-pandora</artifactId>

      <version>1.0</version>

  </dependency>

FatJar 插件依赖(该插件用于生成 FatJar ,并且默认排除 taobao-hsf.sar 这个 JAR 包,该 JAR 包在生产环境部署时会通过 -D 参数自动指定,无需打包,避免资源浪费),必选。

  <plugin>

      <groupId>com.taobao.pandora</groupId>

      <artifactId>pandora-boot-maven-plugin</artifactId>

      <version>2.1.6.3</version>

      <executions>

          <execution>

              <phase>package</phase>

              <goals>

                  <goal>repackage</goal>

              </goals>

          </execution>

      </executions>

  </plugin>

1.2.2. HSF 服务依赖

使用 Spring Boot 编程模型结合 Pandora Boot开发HSF服务时,需要使用到的依赖(若使用XML配置文件方式,可以不添加),可选。

  <dependency>

      <groupId>org.springframework.cloud</groupId>

      <artifactId>spring-cloud-starter-hsf</artifactId>

  </dependency>

1.2.3. RESTful 服务依赖

为 Spring Cloud 开发的 RESTful 应用在 EDAS 中添加服务注册与发现的支持,必选。 EDAS 中 Spring Cloud 服务注册与发现功能目前只支持自研的 VIPServer。

  <dependency>

      <groupId>org.springframework.cloud</groupId>

      <artifactId>spring-cloud-starter-vipclient</artifactId>

  </dependency>  

为 Spring Cloud 开发的 RESTful 应用在 EDAS 中服务添加链路跟踪的依赖。可选,添加后才能使用 服务监控 功能。

  <dependency>

      <groupId>org.springframework.cloud</groupId>

      <artifactId>spring-cloud-starter-eagleeye</artifactId>

  </dependency>

为 Spring Cloud 开发的 RESTful 应用在 EDAS 中服务添加限流支持的依赖。可选,添加后才能使用 限流降级 功能。

  <dependency>

      <groupId>org.springframework.cloud</groupId>

      <artifactId>spring-cloud-starter-sentinel</artifactId>

  </dependency>

为 Spring Cloud 开发的 RESTful 应用在 EDAS 中服务添加服务调用鉴权的依赖,可选。

  <dependency>

      <groupId>org.springframework.cloud</groupId>

      <artifactId>spring-cloud-starter-dauth</artifactId>

  </dependency>  

注意:若应用没有依赖 spring-boot-starter-web ,将不会引入嵌入式 Tomcat ,导致 EDAS 页面上面的一些 Tomcat 相关参数及健康检查无法使用。

1.3. 服务开发

需要添加applicaiton.properties 和在启动类里面添加潘多拉容器的启动。

1.3.1. 添加配置

使用方式:在 application.properties 添加 EDAS 服务的相关配置,样例配置如下:

    spring.application.name=spring-cloud-provider

    server.port=18080

其他相关参数 (可选)

    spring.edas.dauth.enable=true #接口是否开启鉴权,默认为true

    spring.edas.dauth.whitelist.paths=/appwl #白名单,不做鉴权的路径,只判断第一层路径

    spring.sentinel.filter.urlPatterns=/* #需要进行限流的url,这里添加所有都进行限流

    vipserver.register.doms=my-provider,sc-provider,service-name #vipserver #注册的服务名

    vipserver.register.enabled=false #关闭服务注册的功能

说明:

spring.edas.dauth.enable: 鉴权相关的配置,默认为开启,可不填。

spring.sentinel.filter.urlPatterns:限流降级相关的配置,在这里可以指定需要监控的URL后,可以通过EDAS控制台对这些URL进行限流降级规则配置。

vipserver.register.doms:服务名的配置,用英文逗号隔开。样例中的三个服务名是对等的关系,订阅者可以使用其中任意一个调用。若不填写,默认为应用名称。

vipserver.register.enabled:服务发布的开关,如果此工程只有服务消费者,没有服务提供者,应该将此值设置为false。

1.3.1.1. 服务端启动-添加潘多拉容器启动

    @EnableDiscoveryClient

    @SpringBootApplication

    public class ProviderApplication {

        public static void main(String[] args) {

            // 启动Pandora Boot 用于加载 Pandora 容器

            PandoraBootstrap.run(args);

            // 启动Spring Boot

            SpringApplication.run(ProviderApplication.class, args);

            // 标记服务启动完成,并设置线程 wait。防止用户业务代码运行完毕退出后,导致容器退出。

            PandoraBootstrap.markStartupAndWait();

        }

    }

1.3.1.2. 消费端启动-添加潘多拉容器启动

    @EnableDiscoveryClient

    @EnableFeignClients

    @SpringBootApplication

    public class ConsumerApplication {

        public static void main(String[] args) {

            PandoraBootstrap.run(args);

            SpringApplication.run(ConsumerApplication.class, args);

            PandoraBootstrap.markStartupAndWait();

        }

    }

1.3.2. 服务发布

首先使用注解@EnableDiscoveryClient激活服务发布和订阅的功能,发布的服务名在application.properties中配置

vipserver.register.doms=my-provider,sc-provider,service-name

注意:确保使用的注解不是@EnableEurekaClient 而是 @EnableDiscoveryClient

1.3.3. 部署

1.3.3.1. 本地部署

可以下载轻量级配置中心:

本地部署时,必须先启动轻量级配置中心,同时,将hosts中的jmenv.tbsite.net地址,设为轻量级配置中心所在的ip地址,详情见安装轻量配置中心

使用本地轻量级配置中心时,必须通过在启动时添加JVM参数来指定如下配置:

-Daddress.server.domain=127.0.0.1 (轻量级配置中心的ip地址)

-Daddress.server.port=8080 (地址服务器端口)

-Dvipserver.client.port=8080 (Vipserver服务的监听端口)

-Dvipserver.server.port=8080

在 IDE 中,通过 main 方法直接启动。

本地打包 FatJar ,通过 JAVA 命令启动。

排除 taobao-hsf.sar 依赖启动方式(加入-D 指定 SAR 位置)

  #java -jar -Dpandora.location=/Users/yizhan/.m2/repository/com/taobao/pandora/taobao-hsf.sar/dev.3.5/taobao-hsf.sar-dev.3.5.jar spring-cloud-provider-0.0.1-SNAPSHOT.jar

注意 : -Dpandora.location 指定的路径必须是全路径

不排除 taobao-hsf.sar 依赖启动方式(通过插件设置)通过 pandora-boot-maven-plugin 插件,把 excludeSar 设置为 false ,默认是 true ,打包时就会自动包含该 SAR 包。

  <plugin>

    <groupId>com.taobao.pandora</groupId>

    <artifactId>pandora-boot-maven-plugin</artifactId>

    <version>2.1.6.3</version>

    <configuration>

      <excludeSar>false</excludeSar>

   </configuration>

    <executions>

        <execution>

            <phase>package</phase>

            <goals>

                <goal>repackage</goal>

            </goals>

        </execution>

    </executions>

  </plugin>

直接启动

  java -jar spring-cloud-provider-0.0.1-SNAPSHOT.jar

1.3.3.2. EDAS 中部署

在应用管理中,创建的应用需要选择支持 FatJar 功能的容器(3.2.5及以上)。选择部署应用,上传 FatJar 应用即可部署。那么以后默认该应用只能上传 FatJar 了,不再支持 WAR 。

2. 普通Tomcat 开发

添加依赖:

<dependency>

  <groupId>com.alibaba.edas</groupId>

  <artifactId>edas-container-sdk</artifactId>

  <version>1.2.0</version>

</dependency>

3. 配置中心

3.1. 依赖包

a) pandora 升级到1.2

<dependency>     <groupId>org.springframework.cloud</groupId>     <artifactId>spring-cloud-starter-pandora</artifactId>     <version>1.2</version>  </dependency>

b) pandora maven plugin 升级到 2.1.7.8

<build>        <plugins>           <plugin>              <groupId>com.taobao.pandora</groupId>              <artifactId>pandora-boot-maven-plugin</artifactId>              <version>2.1.7.8</version>              <executions>                 <execution>                    <phase>package</phase>                    <goals>                       <goal>repackage</goal>                    </goals>                 </execution>              </executions>           </plugin>        </plugins>     </build>

c) 加入ACM依赖包

<dependency>     <groupId>com.alibaba.cloud</groupId>     <artifactId>spring-cloud-starter-acm</artifactId>     <version>1.0.0</version>  </dependency>

3.2. 配置修改

修改 application.properties (或者对应的yml)

d) 确定spring.application.name , spring.application.group。spring.application.group的配置必须遵守com.{公司名}.{部门名}比如:com.alibaba.acm; com.dtdream.aliware

spring.application.name=sc-provider spring.application.group=com.dtdream.aliware

e) 添加 diamond group 和 acm group

spring.diamond.group=test alibaba.acm.group=test

3.3. 添加全局配置

在edas console上面进行操作具体如下:

f) 添加服务分组:服务市场->服务分组->创建服务组

g) 在全局配置推送页面先选择刚才创建的分组,之后点击创建配置。

h) 输入的DataId和content。DataId 的格式为:之前在配置修改阶段配置的 {spring.application.group}:{spring.application.name}.properties. 安装上面的配置的group和name 为例子则 DataId应该为:com.dtdream.aliware:sc-provider.properties Content中存放需要使用的键值对 比如user.id = 54321, user.name=name12345

3.4. 使用配置

通过spring 的@value 来加载配置。通过spring的@RefreshScope 来进行配置的实时刷新

需要跟新配置的时候只需要在页面修改之后配置内容会自动推送到相应的程序。

4. Gradle改造

4.1. 添加 gradle配置

repositories {

        flatDir {

            dirs 'D:/uap/common-lib'

        }

        mavenCentral()

        maven { url "http://edas-public.oss-cn-hangzhou.aliyuncs.com/repository" }

    }

    dependencies {

        //classpath group: 'io.spring.gradle', name: 'dependency-management-plugin', version: '0.6.1.RELEASE'

        //classpath group: 'org.springframework.boot', name: 'spring-boot-loader-tools', version: '1.4.1.RELEASE'

        //classpath group: 'org.springframework', name: 'spring-core', version: '4.3.3.RELEASE'

        //classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.1.RELEASE")

        classpath("com.taobao.pandora:pandora-boot-gradle-plugin:2.1.7.8-SNAPSHOT")

}

subprojects {

    apply plugin: 'java'

    //apply plugin: 'eclipse'

    apply plugin: 'idea'

    //apply plugin: 'spring-boot'

    //  apply plugin: "pandora-boot"

    // 使用 io.spring.dependency-management 来引入maven bom

    apply plugin: "io.spring.dependency-management"   <===

apply plugin: 'com.taobao.pandora.boot'                   <===

pandoraBoot {

        // 配置打包sar

        excludeSar = false

    }

    bootRepackage {

        // mainClass = 'demo.Application'

        // 配置打包sar,也可以在这里配置

        // excludeSar = false

    }

    dependencyManagement {

        imports {

            // 引入 spring boot bom

            mavenBom 'org.springframework.boot:spring-boot-dependencies:1.4.1.RELEASE'

        }

    }

4.2. 添加依赖:

compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-pandora', version: '1.2'

    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-dauth', version: '1.0'

    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-sentinel', version: '1.0'

    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-eagleeye', version: '1.1'

    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-vipclient', version: '1.1'

4.3. 去掉 spring cloud的gradle配置

sleuth, zipkin, eureka-core, eureka-client,config-client等

eureka:

//compile group: 'com.netflix.eureka', name: 'eureka-client', version: '1.4.11'

    //compile group: 'com.netflix.eureka', name: 'eureka-core', version: '1.4.11'

// compile group: 'org.springframework.cloud', name: 'spring-cloud-netflix-eureka-client', version: '1.2.0.RELEASE'

    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-archaius', version: '1.2.0.RELEASE'

    //compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-eureka', version: '1.2.0.RELEASE'

config-client:

//    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-config', version: '1.2.0.RELEASE'

//    compile group: 'org.springframework.cloud', name: 'spring-cloud-config-client', version: '1.2.0.RELEASE'

sleuth, zipkin:

//    compile group: 'org.springframework.cloud', name: 'spring-cloud-sleuth-core', version: '1.0.9.RELEASE'

//    compile group: 'org.springframework.cloud', name: 'spring-cloud-sleuth-zipkin', version: '1.0.9.RELEASE'

//    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-sleuth', version: '1.0.9.RELEASE'

//    compile group: 'io.zipkin.java', name: 'zipkin', version: '1.11.1'

//    compile group: 'io.zipkin.reporter', name: 'zipkin-reporter', version: '0.5.0'

//    compile group: 'io.zipkin.reporter', name: 'zipkin-sender-urlconnection', version: '0.5.0'

4.4. 添加spring boot 启动配置

 public static void main(String[] args) {

    PandoraBootstrap.run(args);

    ConfigurableApplicationContext context = SpringApplication.run(StudentServiceApplication.class, args);

    ConfigurableEnvironment env = context.getEnvironment();

    PandoraBootstrap.markStartupAndWait();

  }

确定使用的 注册中心的客户端的注解是@EnableDiscoveryClient,不要使用其他注解:如:@EnableEurekaClient

上一篇下一篇

猜你喜欢

热点阅读