1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

以2.2.1.BUILD-SNAPSHOT 为基准,适配Greenwich版本的Spring Cloud

This commit is contained in:
theonefx
2020-04-03 14:14:47 +08:00
parent e963a39bd4
commit 9444117f59
654 changed files with 7971 additions and 17442 deletions

View File

@@ -1,3 +1,19 @@
/*
* Copyright 2013-2018 the original author or 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
*
* https://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 com.alibaba.cloud.examples;
/**
@@ -6,6 +22,7 @@ package com.alibaba.cloud.examples;
public class Foo {
private int id;
private String bar;
public Foo() {
@@ -36,4 +53,5 @@ public class Foo {
public String toString() {
return "Foo{" + "id=" + id + ", bar='" + bar + '\'' + '}';
}
}

View File

@@ -1,7 +1,22 @@
/*
* Copyright 2013-2018 the original author or 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
*
* https://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 com.alibaba.cloud.examples;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.Message;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Service;
@@ -12,8 +27,8 @@ import org.springframework.stereotype.Service;
public class ReceiveService {
@StreamListener("input1")
public void receiveInput1(Message message) {
System.out.println("input1 receive: " + message.getPayload() + ", foo header: " + message.getHeaders().get("foo"));
public void receiveInput1(String receiveMsg) {
System.out.println("input1 receive: " + receiveMsg);
}
@StreamListener("input2")

View File

@@ -1,3 +1,19 @@
/*
* Copyright 2013-2018 the original author or 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
*
* https://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 com.alibaba.cloud.examples;
import com.alibaba.cloud.examples.RocketMQConsumerApplication.MySink;
@@ -20,6 +36,15 @@ import org.springframework.messaging.SubscribableChannel;
@EnableBinding({ MySink.class })
public class RocketMQConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(RocketMQConsumerApplication.class, args);
}
@Bean
public ConsumerCustomRunner customRunner() {
return new ConsumerCustomRunner();
}
public interface MySink {
@Input("input1")
@@ -36,15 +61,7 @@ public class RocketMQConsumerApplication {
@Input("input5")
PollableMessageSource input5();
}
public static void main(String[] args) {
SpringApplication.run(RocketMQConsumerApplication.class, args);
}
@Bean
public ConsumerCustomRunner customRunner() {
return new ConsumerCustomRunner();
}
public static class ConsumerCustomRunner implements CommandLineRunner {
@@ -63,6 +80,7 @@ public class RocketMQConsumerApplication {
Thread.sleep(2_000);
}
}
}
}

View File

@@ -4,7 +4,6 @@ spring.cloud.stream.bindings.input1.destination=test-topic
spring.cloud.stream.bindings.input1.content-type=text/plain
spring.cloud.stream.bindings.input1.group=test-group1
spring.cloud.stream.rocketmq.bindings.input1.consumer.orderly=true
spring.cloud.stream.rocketmq.bindings.input1.consumer.trustedPackages=com.alibaba.cloud
spring.cloud.stream.bindings.input2.destination=test-topic
spring.cloud.stream.bindings.input2.content-type=text/plain
@@ -34,4 +33,4 @@ spring.application.name=rocketmq-consume-example
server.port=28082
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
management.endpoint.health.show-details=always

View File

@@ -1,3 +1,19 @@
/*
* Copyright 2013-2018 the original author or 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
*
* https://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 com.alibaba.cloud.examples;
/**
@@ -6,6 +22,7 @@ package com.alibaba.cloud.examples;
public class Foo {
private int id;
private String bar;
public Foo() {
@@ -36,4 +53,5 @@ public class Foo {
public String toString() {
return "Foo{" + "id=" + id + ", bar='" + bar + '\'' + '}';
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,9 +22,12 @@ import org.springframework.cloud.stream.binder.PartitionSelectorStrategy;
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
public class MyPartitionSelectorStrategy implements PartitionSelectorStrategy {
@Override
public int selectPartition(Object key, int partitionCount) {
System.out.println("partition key: " + key + ", partitionCount: " + partitionCount);
return 0;
}
@Override
public int selectPartition(Object key, int partitionCount) {
System.out
.println("partition key: " + key + ", partitionCount: " + partitionCount);
return 0;
}
}

View File

@@ -1,3 +1,19 @@
/*
* Copyright 2013-2018 the original author or 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
*
* https://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 com.alibaba.cloud.examples;
import com.alibaba.cloud.examples.RocketMQProduceApplication.MySource;
@@ -9,7 +25,6 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.context.annotation.Bean;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
@@ -20,20 +35,6 @@ import org.springframework.messaging.support.MessageBuilder;
@EnableBinding({ MySource.class })
public class RocketMQProduceApplication {
public interface MySource {
@Output("output1")
MessageChannel output1();
@Output("output2")
MessageChannel output2();
@Output("output3")
MessageChannel output3();
@Output("output4")
MessageChannel output4();
}
public static void main(String[] args) {
SpringApplication.run(RocketMQProduceApplication.class, args);
}
@@ -48,16 +49,24 @@ public class RocketMQProduceApplication {
return new CustomRunner("output3");
}
@Bean
public CustomRunner customRunner3() {
return new CustomRunner("output4");
}
@Bean
public CustomRunnerWithTransactional customRunnerWithTransactional() {
return new CustomRunnerWithTransactional();
}
public interface MySource {
@Output("output1")
MessageChannel output1();
@Output("output2")
MessageChannel output2();
@Output("output3")
MessageChannel output3();
}
public static class CustomRunner implements CommandLineRunner {
private final String bindingName;
@@ -97,20 +106,13 @@ public class RocketMQProduceApplication {
.send(MessageBuilder.withPayload(msgContent).build());
}
}
else if (this.bindingName.equals("output4")) {
int count = 5;
for (int index = 1; index <= count; index++) {
String msgContent = "partitionMsg-" + index;
Message message = MessageBuilder.withPayload(msgContent)
.setHeader("myPartitionKey", "myPartitionKey").build();
mySource.output4().send(message);
}
}
}
}
public static class CustomRunnerWithTransactional implements CommandLineRunner {
@Autowired
private SenderService senderService;
@@ -125,6 +127,7 @@ public class RocketMQProduceApplication {
// COMMIT_MESSAGE message
senderService.sendTransactionalMsg("transactional-msg4", 4);
}
}
}

View File

@@ -1,11 +1,25 @@
/*
* Copyright 2013-2018 the original author or 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
*
* https://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 com.alibaba.cloud.examples;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.alibaba.cloud.examples.RocketMQProduceApplication.MySource;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.rocketmq.common.message.MessageConst;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
@@ -25,8 +39,6 @@ public class SenderService {
@Autowired
private MySource source;
private ObjectMapper objectMapper = new ObjectMapper();
public void send(String msg) throws Exception {
source.output1().send(MessageBuilder.withPayload(msg).build());
}
@@ -41,7 +53,6 @@ public class SenderService {
public <T> void sendObject(T msg, String tag) throws Exception {
Message message = MessageBuilder.withPayload(msg)
.setHeader(MessageConst.PROPERTY_TAGS, tag)
.setHeader("foo", new Foo(1, "bar"))
.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
.build();
source.output1().send(message);

View File

@@ -1,11 +1,11 @@
/*
* Copyright (C) 2018 the original author or authors.
* Copyright 2013-2018 the original author or 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
* https://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,
@@ -27,6 +27,7 @@ import org.springframework.messaging.Message;
*/
@RocketMQTransactionListener(txProducerGroup = "myTxProducerGroup", corePoolSize = 5, maximumPoolSize = 10)
public class TransactionListenerImpl implements RocketMQLocalTransactionListener {
@Override
public RocketMQLocalTransactionState executeLocalTransaction(Message msg,
Object arg) {
@@ -52,4 +53,5 @@ public class TransactionListenerImpl implements RocketMQLocalTransactionListener
System.out.println("check: " + new String((byte[]) msg.getPayload()));
return RocketMQLocalTransactionState.COMMIT;
}
}
}

View File

@@ -6,7 +6,6 @@ spring.cloud.stream.bindings.output1.destination=test-topic
spring.cloud.stream.bindings.output1.content-type=application/json
spring.cloud.stream.rocketmq.bindings.output1.producer.group=binder-group
spring.cloud.stream.rocketmq.bindings.output1.producer.sync=true
spring.cloud.stream.rocketmq.bindings.output1.producer.sendMessageTimeout=30000
spring.cloud.stream.bindings.output2.destination=TransactionTopic
spring.cloud.stream.bindings.output2.content-type=application/json
@@ -17,15 +16,9 @@ spring.cloud.stream.bindings.output3.destination=pull-topic
spring.cloud.stream.bindings.output3.content-type=text/plain
spring.cloud.stream.rocketmq.bindings.output3.producer.group=pull-binder-group
spring.cloud.stream.bindings.output4.destination=partition-topic
spring.cloud.stream.bindings.output4.content-type=text/plain
spring.cloud.stream.bindings.output4.producer.partition-key-expression=headers['myPartitionKey']
spring.cloud.stream.bindings.output4.producer.partition-selector-class=com.alibaba.cloud.examples.MyPartitionSelectorStrategy
spring.cloud.stream.rocketmq.bindings.output4.producer.group=partition-binder-group
spring.application.name=rocketmq-produce-example
server.port=28081
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
management.endpoint.health.show-details=always