Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

add sofa-serverless-adapter-logback #305

Merged
merged 9 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions samples/springboot-samples/logging/logback/base/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
ADD ${JAR_FILE} app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]
69 changes: 69 additions & 0 deletions samples/springboot-samples/logging/logback/base/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.alipay.sofa</groupId>
<artifactId>springboot-samples</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../../pom.xml</relativePath> <!-- lookup parent from repository -->
</parent>
<groupId>com.alipay.sofa.logging.logback</groupId>
<artifactId>base-logback</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>base-logback</name>
<description>logging base for logback</description>
<properties>
<sofa.serverless.runtime.version>0.5.5</sofa.serverless.runtime.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 一定要排除logback,因为基座cp中的ark-all包含了该jar,
会导致该jar中的类被app cl和container cl都能加载到,会出现class not find-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- 这里添加动态模块相关依赖 -->
<dependency>
<groupId>com.alipay.sofa.serverless</groupId>
<artifactId>sofa-serverless-base-starter</artifactId>
</dependency>


<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>web-ark-plugin</artifactId>
</dependency>
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>${disruptor.version}</version>
</dependency>
<dependency>
<groupId>com.alipay.sofa.serverless</groupId>
<artifactId>sofa-serverless-adapter-logback</artifactId>
<version>${sofa.serverless.runtime.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.alipay.sofa.web.base;

import ch.qos.logback.classic.ClassicConstants;
import com.alipay.sofa.serverless.logback.SOFAServerlessLogbackLogContextSelector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ImportResource;

@ImportResource({ "classpath*:META-INF/spring/service.xml"})
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class})
public class BaseApplication {
private static Logger LOGGER ;

public static void main(String[] args) {
//建议加到jvm 参数中
System.setProperty(ClassicConstants.LOGBACK_CONTEXT_SELECTOR,
SOFAServerlessLogbackLogContextSelector.class.getName());
LOGGER = LoggerFactory.getLogger(BaseApplication.class);
ConfigurableApplicationContext context = SpringApplication.run(
BaseApplication.class, args);
context.getBean("sampleService");
LOGGER.info("BaseApplication start!");
LOGGER.info("Spring Boot Version: " + SpringApplication.class.getPackage().getImplementationVersion());
LOGGER.info("BaseApplication classLoader: " + BaseApplication.class.getClassLoader());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.alipay.sofa.web.base.facade;

public interface SampleService {

/**
* a simple facade
* @return
*/
String service();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.alipay.sofa.web.base.impl;

import com.alipay.sofa.web.base.facade.SampleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

@Service
public class SampleServiceImpl implements SampleService {
private static Logger LOGGER = LoggerFactory.getLogger(SampleServiceImpl.class);

@Autowired
private ApplicationContext applicationContext;

@Override
public String service() {
String appName = applicationContext.getId();

LOGGER.info("{} web test: into a service", appName);
return "A Sample Service";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.alipay.sofa.web.base.rest;

import com.alipay.sofa.web.base.facade.SampleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SampleController {
private static Logger LOGGER = LoggerFactory.getLogger(SampleController.class);

@Autowired
private ApplicationContext applicationContext;

@Autowired
private SampleService sampleService;

@RequestMapping(value = "/", method = RequestMethod.GET)
public String hello() {
String appName = applicationContext.getId();
LOGGER.info("{} web test: into sample controller", appName);

sampleService.service();

return String.format("hello to %s deploy", appName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="sampleService" class="com.alipay.sofa.web.base.impl.SampleServiceImpl"/>

</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.application.name=base
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<springProperty scope="context" name="appname" source="spring.application.name"/>
<property name="level" value="${logLevel:-info}"/>
<!-- <property name="appid" value="${appname}"/>-->
<property name="the3rdLevel" value="${the3rdLevel:-WARN}"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${appname} 000 %date %5level %6relative --- [%15thread] [%-40logger{40}] [%C:%L] : [%X{traceId:-0}] %msg%n</pattern>
</encoder>
</appender>
<logger name="org.hibernate" level="${the3rdLevel}"/>
<logger name="org.springframework" level="${the3rdLevel}"/>
<logger name="com.alibaba" level="${the3rdLevel}"/>
<logger name="org.apache" level="${the3rdLevel}"/>
<root level="${level}">
<appender-ref ref="CONSOLE"/>
</root>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.alipay.sofa.web.base;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class BaseApplicationTests {

@Test
void contextLoads() {
}

}
80 changes: 80 additions & 0 deletions samples/springboot-samples/logging/logback/biz1/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.alipay.sofa</groupId>
<artifactId>springboot-samples</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../../pom.xml</relativePath> <!-- lookup parent from repository -->
</parent>
<groupId>com.alipay.sofa.logging.logback</groupId>
<artifactId>biz1-logback</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>biz1-logback</name>
<description>biz1</description>
<properties>
<sofa.serverless.runtime.version>0.5.5</sofa.serverless.runtime.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- <dependency>-->
<!-- <groupId>com.alipay.sofa.serverless</groupId>-->
<!-- <artifactId>sofa-serverless-app-starter</artifactId>-->
<!-- <version>${sofa.serverless.runtime.version}</version>-->
<!-- <scope>provided</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>com.alipay.sofa.serverless</groupId>
<artifactId>sofa-serverless-adapter-logback</artifactId>
<version>${sofa.serverless.runtime.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-ark-maven-plugin</artifactId>
<version>${sofa.ark.version}</version>
<executions>
<execution>
<id>default-cli</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<skipArkExecutable>true</skipArkExecutable>
<outputDirectory>./target</outputDirectory>
<bizName>biz1-logback</bizName>
<webContextPath>biz1</webContextPath>
<declaredMode>true</declaredMode>
<!-- 打包、安装和发布 ark biz-->
<!-- 静态合并部署需要配置-->
<!-- <attach>true</attach>-->
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.alipay.sofa.web.biz1;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Biz1Application {
private static Logger LOGGER = LoggerFactory.getLogger(Biz1Application.class);

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

LOGGER.info("BaseApplication start!");
LOGGER.info("Spring Boot Version: " + SpringApplication.class.getPackage().getImplementationVersion());
LOGGER.info("BaseApplication classLoader: " + Biz1Application.class.getClassLoader());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.alipay.sofa.web.biz1.rest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SampleController {
private static final Logger LOGGER = LoggerFactory.getLogger(SampleController.class);

@Autowired
private ApplicationContext applicationContext;

@RequestMapping(value = "/", method = RequestMethod.GET)
public String hello() {
String appName = applicationContext.getApplicationName();
LOGGER.info("{} web test: into sample controller", appName);
return String.format("hello to %s deploy", appName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.application.name=biz1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<springProperty scope="context" name="appname" source="spring.application.name"/>
<property name="level" value="${logLevel:-info}"/>
<!-- <property name="appid" value="${appname}"/>-->
<property name="the3rdLevel" value="${the3rdLevel:-WARN}"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${appname} 111 %date %5level %6relative --- [%15thread] [%-40logger{40}] [%C:%L] : [%X{traceId:-0}] %msg%n</pattern>
</encoder>
</appender>
<logger name="org.hibernate" level="${the3rdLevel}"/>
<logger name="org.springframework" level="${the3rdLevel}"/>
<logger name="com.alibaba" level="${the3rdLevel}"/>
<logger name="org.apache" level="${the3rdLevel}"/>
<root level="${level}">
<appender-ref ref="CONSOLE"/>
</root>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.alipay.sofa.web.biz1;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class Biz1ApplicationTests {

@Test
void contextLoads() {
}

}
Loading