• 介绍

    项目是基于spring boot的maven项目。

    1. 新建maven项目
    2. 添加项目依赖
    3. 添加默认配置文件

    创建maven项目

    本地新建一个空的 maven 项目choerodon-todo-service

    $ mkdir -p choerodon-todo-service
    $ cd choerodon-todo-service
    

    添加项目依赖

    创建pom.xml 文件。

    $ touch pom.xml
    

    修改pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://maven.apache.org/POM/4.0.0"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <groupId>io.choerodon</groupId>
        <artifactId>choerodon-todo-service</artifactId>
        <version>1.0.0</version>
        <!--choerodon-framework-parent dependency-->
        <parent>
            <groupId>io.choerodon</groupId>
            <artifactId>choerodon-framework-parent</artifactId>
            <version>0.6.0.RELEASE</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <!--choerodon-starters dependency-->
        <properties>
            <choerodon.starters.version>0.5.2.RELEASE</choerodon.starters.version>
        </properties>
        <dependencies>
            <!--spring boot-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-undertow</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-tomcat</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
            <!--spring cloud-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
    
            <!--choerodon-->
            <dependency>
                <groupId>io.choerodon</groupId>
                <artifactId>choerodon-starter-core</artifactId>
                <version>${choerodon.starters.version}</version>
            </dependency>
            <dependency>
                <groupId>io.choerodon</groupId>
                <artifactId>choerodon-starter-oauth-resource</artifactId>
                <version>${choerodon.starters.version}</version>
            </dependency>
            <dependency>
                <groupId>io.choerodon</groupId>
                <artifactId>choerodon-starter-swagger</artifactId>
                <version>${choerodon.starters.version}</version>
            </dependency>
    
        </dependencies>
    
        <build>
            <finalName>choerodon-todo-service</finalName>
        </build>
    
    </project>
    

    根据子级模块所需jar包添加需要的依赖。

    更多choerodon-starter的依赖可以参考choerodon-starters

    添加默认配置文件

    在根目录下创建源码文件夹和资源文件夹。

    $ mkdir -p src/main/java
    $ mkdir -p src/main/resources
    

    项目采用spring boot 进行管理。需要在子项目中配置默认的配置项。

    resource文件夹中创建 application.yml, bootstrap.yml

    $ cd src/main/resources
    $ touch application.yml
    $ touch bootstrap.yml
    
    # bootstrap.yml
    server:
      port: 18080
    spring:
      application:
        name: choerodon-todo-service
      cloud:
        config:
          failFast: true
          retry:
            maxAttempts: 6
            multiplier: 1.5
            maxInterval: 2000
          uri: localhost:8010
          enabled: false
    management:
      port: 18081
      security:
        enabled: false
    security:
      basic:
        enabled: false
    
    # application.yml
    eureka:
      instance:
        preferIpAddress: true
        leaseRenewalIntervalInSeconds: 10
        leaseExpirationDurationInSeconds: 30
        metadata-map:
          VERSION: v1
      client:
        serviceUrl:
          defaultZone: ttp://localhost:8000/eureka/
        registryFetchIntervalSeconds: 10
    mybatis:
      mapperLocations: classpath*:/mapper/*.xml
      configuration: # 数据库下划线转驼峰配置
        mapUnderscoreToCamelCase: true
    

    编写TodoServiceApplication类

    src/main/java中创建TodoServiceApplication。

    $ mkdir -p src/main/java/io/choerodon/todo
    $ touch src/main/java/io/choerodon/todo/TodoServiceApplication
    

    添加main 函数。

    package io.choerodon.todo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import io.choerodon.resource.annoation.EnableChoerodonResourceServer;
    
    @SpringBootApplication
    // 是否允许注册到注册中心,暂时注释掉
    //@EnableEurekaClient
    @RestController
    // 是否开启猪齿鱼资源服务器
    // @EnableChoerodonResourceServer
    public class TodoServiceApplication {
        public static void main(String[] args) {
            SpringApplication.run(TodoServiceApplication.class, args);
        }
    
        @GetMapping
        @Permission(level = ResourceLevel.SITE, permissionPublic = true)
        @ApiOperation(value = "demo")
        public ResponseEntity<String> hello() {
            return new ResponseEntity<String>("hello world", HttpStatus.OK);
        }
    }
    

    启动应用

    项目根目录下执行命令。

    $ mvn clean spring-boot:run
    

    控制台打印出如下信息,则表示启动成功。

    Started TodoServiceApplication in 20.651 seconds (JVM running for 24.976)
    

    此时可以打开浏览器,在浏览器输入:http://localhost:18081/health

    返回如下信息:

    {
    status: "UP",
        diskSpace: {
            status: "UP"
        },
        db: {
            status: "UP",
            database: "MySQL",
            hello: 1
        },
        refreshScope: {
            status: "UP"
        },
        hystrix: {
            status: "UP"
        }
    }
    

    在浏览器输入:http://localhost:18080/hello,页面打印 hello world

    这样,一个简单的spring boot 应用就已经搭建成功。