Skip to content

开发第一个Sunrays-Framework应用

1.配置Maven中央仓库

1.打开settings.xml

CleanShot 2025-01-19 at 23.16.59@2x

2.不要配置阿里云,切换为Maven中央仓库,否则下不了依赖

xml
  <mirrors>
    <mirror>
        <id>central</id>
        <mirrorOf>central</mirrorOf> <!-- 直接指定中央仓库 -->
        <name>Maven Central</name>
        <url>https://repo.maven.apache.org/maven2</url>
    </mirror>
</mirrors>

2.创建项目 combinations-quickstart-starter-demo

CleanShot 2025-01-18 at 18.50.39@2x

3.基本目录结构

CleanShot 2025-01-18 at 23.05.33@2x

4.代码

1.pom.xml 引入quickstart依赖

xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>cn.sunxiansheng</groupId>
        <artifactId>sunrays-framework-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>combinations-quickstart-starter-demo</artifactId>

    <!-- 通过properties来指定版本号 -->
    <properties>
        <!-- 指定编译版本 -->
        <java.version>1.8</java.version>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <!-- 指定Sunrays-Framework的版本 -->
        <sunrays.version>1.0.0</sunrays.version>
    </properties>

    <dependencyManagement>
        <!-- 使用sunrays-dependencies来管理依赖,则依赖无需加版本号 -->
        <dependencies>
            <dependency>
                <groupId>cn.sunxiansheng</groupId>
                <artifactId>sunrays-dependencies</artifactId>
                <version>${sunrays.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- 快速入门的一个starter -->
        <dependency>
            <groupId>cn.sunxiansheng</groupId>
            <artifactId>combinations-quickstart-starter</artifactId>
            <!-- 无需指定版本 -->
        </dependency>
    </dependencies>
</project>

2.application.yml 配置日志存储根目录

yml
sun-rays:
  log4j2:
    home: /Users/sunxiansheng/IdeaProjects/sunrays-framework-demo/combinations-quickstart-starter-demo/logs # 日志存储的根目录

3.QuickStartController.java 测试的Controller

java
package cn.sunxiansheng.quickstart.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Description: QuickStartController
 *
 * @Author sun
 * @Create 2025/1/18 19:17
 * @Version 1.0
 */
@RestController
public class QuickStartController {

    /**
     * A test endpoint.
     *
     * @return A sample response.
     */
    @RequestMapping("/test")
    public String test() {
        return "This is a test response from QuickStartController";
    }
}

4.QuickStartApplication.java 启动类

java
package cn.sunxiansheng.quickstart;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Description: QuickStartApplication
 *
 * @Author sun
 * @Create 2025/1/18 18:52
 * @Version 1.0
 */
@SpringBootApplication
public class QuickStartApplication {

    public static void main(String[] args) {
        SpringApplication.run(QuickStartApplication.class, args);
    }
}

5.测试运行

CleanShot 2025-01-18 at 23.00.01@2x

6.请求测试

1.Controller的返回结果是String,默认自动被包装了

CleanShot 2025-01-18 at 22.50.50@2x

2.自带链路追踪以及全链路的日志输出

CleanShot 2025-01-18 at 22.49.06@2x