Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: layotto springboot #8

Merged
merged 6 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
15 changes: 15 additions & 0 deletions examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@
<groupId>io.mosn.layotto</groupId>
<artifactId>runtime-sdk</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>io.mosn.layotto</groupId>
<artifactId>layotto-sdk-springboot</artifactId>
<version>1.1.0-SNAPSHOT</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.mosn.layotto.examples.pubsub.springboot;

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

@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(Application.class);
app.run();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.mosn.layotto.examples.pubsub.springboot;

import io.mosn.layotto.v1.Topic;
import org.springframework.stereotype.Component;
import spec.sdk.runtime.v1.domain.pubsub.TopicEventRequest;


@Component
public class SubscribeMethod {
@Topic(name = "hello", pubsubName = "redis")
public void method1(TopicEventRequest request) {
System.out.println(new String(request.getData()));
}

@Topic(name = "topic1", pubsubName = "redis")
public void method2(TopicEventRequest request) {
System.out.println(new String(request.getData()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
package io.mosn.layotto.examples.pubsub.subscriber;

import com.alibaba.fastjson.JSON;
import io.mosn.layotto.examples.pubsub.subscriber.impl.RawSubscriber;
import io.mosn.layotto.v1.RuntimeServerGrpc;
import io.mosn.layotto.v1.callback.component.pubsub.DefaultSubscriber;

import java.util.concurrent.Semaphore;

Expand All @@ -29,7 +29,7 @@ public class Subscriber {
*/
public static void main(String[] args) throws Exception {
RuntimeServerGrpc srv = new RuntimeServerGrpc(9999);
RawSubscriber pubsub = new RawSubscriber("redis");
DefaultSubscriber pubsub = new DefaultSubscriber("redis");
pubsub.subscribe("hello", request -> {
String value = new String(request.getData());
assertEquals(value, "world");
Expand Down
1 change: 1 addition & 0 deletions examples/src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
layotto.subscriber.port=9999
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<module>examples</module>
<module>sdk-reactor</module>
<module>spec</module>
<module>sdk-springboot</module>
</modules>

<properties>
Expand Down Expand Up @@ -118,7 +119,7 @@
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<plugin>
<groupId>com.googlecode.maven-java-formatter-plugin</groupId>
<artifactId>maven-java-formatter-plugin</artifactId>
<version>0.4</version>
Expand Down
64 changes: 64 additions & 0 deletions sdk-springboot/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?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">
<parent>
<artifactId>runtime-sdk-parent</artifactId>
<groupId>io.mosn.layotto</groupId>
<version>1.1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>layotto-sdk-springboot</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<springboot.version>2.3.5.RELEASE</springboot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.mosn.layotto</groupId>
<artifactId>runtime-sdk</artifactId>
<version>1.1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${springboot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.mosn.layotto</groupId>
<artifactId>runtime-sdk</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<scope>compile</scope>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2021 Layotto Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.mosn.layotto.springboot;

import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
* layotto Spring Boot AutoConfiguration.
*/
@Configuration
@ConditionalOnWebApplication
@ComponentScan("io.mosn.layotto.springboot")
ZLBer marked this conversation as resolved.
Show resolved Hide resolved
public class LayottoAutoConfiguration {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright 2021 Layotto Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.mosn.layotto.springboot;

import io.mosn.layotto.v1.Topic;
import io.mosn.layotto.v1.callback.component.pubsub.DefaultSubscriber;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.config.EmbeddedValueResolver;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
* LayottoBeanPostProcessor to record @Topic method
*/
@Component
public class LayottoBeanPostProcessor implements BeanPostProcessor {

private static final Logger LOGGER = LoggerFactory.getLogger(LayottoBeanPostProcessor.class.getName());

private final EmbeddedValueResolver embeddedValueResolver;

static Map<String, DefaultSubscriber> subscriberMap = new ConcurrentHashMap<>();

LayottoBeanPostProcessor(ConfigurableBeanFactory beanFactory) {
embeddedValueResolver = new EmbeddedValueResolver(beanFactory);
}

/**
* {@inheritDoc}
*/
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

subscribeToTopics(bean.getClass(), bean, embeddedValueResolver);

return bean;
}

/**
* {@inheritDoc}
*/
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

return bean;
}


private static void subscribeToTopics(Class clazz, Object bean, EmbeddedValueResolver embeddedValueResolver) {
if (clazz == null) {
return;
}

subscribeToTopics(clazz.getSuperclass(), bean, embeddedValueResolver);
for (Method method : clazz.getDeclaredMethods()) {
Topic topic = method.getAnnotation(Topic.class);
if (topic == null) {
continue;
}
String topicName = embeddedValueResolver.resolveStringValue(topic.name());
String pubSubName = embeddedValueResolver.resolveStringValue(topic.pubsubName());
if (StringUtils.isNotEmpty(topicName) && StringUtils.isNotEmpty(pubSubName)) {

if (!subscriberMap.containsKey(pubSubName)) {
subscriberMap.put(pubSubName, new DefaultSubscriber(pubSubName));
}
subscriberMap.get(pubSubName).subscribe(topicName, new LayottoEventListener(bean, method));
ZLBer marked this conversation as resolved.
Show resolved Hide resolved
LOGGER.info("Layotto pubsub subscribe succeeded,PubSubName:{},TopicName:{}", pubSubName, topicName);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2021 Layotto Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.mosn.layotto.springboot;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
* to save spring properties
*/
@Component
public class LayottoConfig {


public static final int DEFAULT_SUBSCRIBER_PORT = 9999;

@Value("${layotto.subscriber.port}")
static public Integer port;

static public int getPort() {
return port == null ? DEFAULT_SUBSCRIBER_PORT : port;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.mosn.layotto.springboot;

/*
* Copyright 2021 Layotto Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import io.mosn.layotto.v1.callback.component.pubsub.EventListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import spec.sdk.runtime.v1.domain.pubsub.TopicEventRequest;

import java.lang.reflect.Method;


/**
* listener for a topic, then invoke toInvokeMethod of this bean
*/
public class LayottoEventListener implements EventListener {
private static final Logger logger = LoggerFactory.getLogger(LayottoEventListener.class.getName());

private final Object bean;
private final Method toInvokeMethod;

public LayottoEventListener(Object bean, Method toInvokeMethod) {
this.bean = bean;
this.toInvokeMethod = toInvokeMethod;
}

@Override
public void onEvent(TopicEventRequest request) throws Exception {
try {
toInvokeMethod.invoke(bean, request);
} catch (Exception e) {
logger.error("layotto subscriber callback method [{}] err:{ }", toInvokeMethod.getName(),e.getMessage());
throw e;
}
}
}
Loading