diff --git a/pom.xml b/pom.xml index f1e621908..a2a7aa0fd 100644 --- a/pom.xml +++ b/pom.xml @@ -69,6 +69,7 @@ 2.0.2.RELEASE 2.0.2.RELEASE 2.0.2.RELEASE + 2.0.0.RELEASE 4.12 3.0 @@ -99,6 +100,7 @@ spring-cloud-alicloud-oss spring-cloud-alicloud-acm spring-cloud-alicloud-ans + spring-cloud-starter-bus-rocketmq @@ -143,6 +145,14 @@ import + + org.springframework.cloud + spring-cloud-bus-dependencies + ${spring-cloud-bus.version} + pom + import + + diff --git a/spring-cloud-alibaba-dependencies/pom.xml b/spring-cloud-alibaba-dependencies/pom.xml index 320cb5999..4d2b1ce27 100644 --- a/spring-cloud-alibaba-dependencies/pom.xml +++ b/spring-cloud-alibaba-dependencies/pom.xml @@ -9,6 +9,7 @@ 2.0.4.RELEASE + spring-cloud-alibaba-dependencies 0.2.1.BUILD-SNAPSHOT pom @@ -25,7 +26,6 @@ 1.0.0 2.16.0 4.3.1 - 4.0.3 @@ -76,6 +76,13 @@ ${nacos.version} + + + org.apache.rocketmq + rocketmq-client + ${rocketmq.version} + + com.alibaba.csp @@ -127,11 +134,6 @@ sentinel-dubbo-api ${project.version} - - org.apache.rocketmq - rocketmq-client - ${rocketmq.version} - @@ -231,18 +233,17 @@ ${project.version} - - io.dropwizard.metrics - metrics-core - ${metrics.core} + org.springframework.cloud + spring-cloud-starter-bus-rocketmq + ${project.version} - + spring diff --git a/spring-cloud-alibaba-examples/pom.xml b/spring-cloud-alibaba-examples/pom.xml index 103e15b2a..7d6b3b263 100644 --- a/spring-cloud-alibaba-examples/pom.xml +++ b/spring-cloud-alibaba-examples/pom.xml @@ -6,6 +6,7 @@ org.springframework.cloud spring-cloud-alibaba 0.2.1.BUILD-SNAPSHOT + ../pom.xml 4.0.0 @@ -28,6 +29,7 @@ ans-example/ans-provider-example acm-example/acm-local-example rocketmq-example + spring-cloud-bus-rocketmq-example diff --git a/spring-cloud-alibaba-examples/rocketmq-example/pom.xml b/spring-cloud-alibaba-examples/rocketmq-example/pom.xml index 1afc1ab97..bec837c36 100644 --- a/spring-cloud-alibaba-examples/rocketmq-example/pom.xml +++ b/spring-cloud-alibaba-examples/rocketmq-example/pom.xml @@ -6,6 +6,7 @@ org.springframework.cloud spring-cloud-alibaba-examples 0.2.1.BUILD-SNAPSHOT + ../pom.xml 4.0.0 @@ -25,6 +26,7 @@ org.springframework.boot spring-boot-starter-web + org.springframework.boot spring-boot-starter-actuator diff --git a/spring-cloud-alibaba-examples/spring-cloud-bus-rocketmq-example/pom.xml b/spring-cloud-alibaba-examples/spring-cloud-bus-rocketmq-example/pom.xml new file mode 100644 index 000000000..22dbca4be --- /dev/null +++ b/spring-cloud-alibaba-examples/spring-cloud-bus-rocketmq-example/pom.xml @@ -0,0 +1,54 @@ + + + + spring-cloud-alibaba-examples + org.springframework.cloud + 0.2.1.BUILD-SNAPSHOT + ../pom.xml + + 4.0.0 + + org.springframework.cloud + spring-cloud-bus-rocketmq-example + Spring Cloud Bus RocketMQ Example + + + + + + org.springframework.cloud + spring-cloud-starter-bus-rocketmq + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-actuator + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-deploy-plugin + ${maven-deploy-plugin.version} + + true + + + + + + \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/spring-cloud-bus-rocketmq-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/rocketmq/RocketMQBusApplication.java b/spring-cloud-alibaba-examples/spring-cloud-bus-rocketmq-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/rocketmq/RocketMQBusApplication.java new file mode 100644 index 000000000..c4a05a8ba --- /dev/null +++ b/spring-cloud-alibaba-examples/spring-cloud-bus-rocketmq-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/rocketmq/RocketMQBusApplication.java @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.springframework.cloud.alibaba.cloud.examples.rocketmq; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.cloud.bus.event.AckRemoteApplicationEvent; +import org.springframework.cloud.bus.jackson.RemoteApplicationEventScan; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.event.EventListener; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +/** + * RocketMQ Bus Spring Application + * + * @author Mercy + * @since 0.2.1 + */ +@RestController +@EnableAutoConfiguration +@RemoteApplicationEventScan(basePackages = "org.springframework.cloud.alibaba.cloud.examples.rocketmq") +public class RocketMQBusApplication { + + public static void main(String[] args) { + new SpringApplicationBuilder(RocketMQBusApplication.class) + .properties("server.port=0") // Random server port + .properties("management.endpoints.web.exposure.include=*") // exposure includes all + .properties("spring.cloud.bus.trace.enabled=true") // Enable trace + .run(args); + } + + @Autowired + private ApplicationEventPublisher publisher; + + @Value("${spring.cloud.bus.id}") + private String originService; + + @Value("${server.port}") + private int localServerPort; + + @Autowired + private ObjectMapper objectMapper; + + /** + * Publish the {@link UserRemoteApplicationEvent} + * + * @param name the user name + * @param destination the destination + * @return If published + */ + @GetMapping("/bus/event/publish/user") + public boolean publish(@RequestParam String name, @RequestParam(required = false) String destination) { + User user = new User(); + user.setId(System.currentTimeMillis()); + user.setName(name); + publisher.publishEvent(new UserRemoteApplicationEvent(user, originService, destination)); + return true; + } + + /** + * Listener on the {@link UserRemoteApplicationEvent} + * + * @param event {@link UserRemoteApplicationEvent} + */ + @EventListener + public void onEvent(UserRemoteApplicationEvent event) { + System.out.printf("Server [port : %d] listeners on %s\n", localServerPort, event.getUser()); + } + + @EventListener + public void onAckEvent(AckRemoteApplicationEvent event) throws JsonProcessingException { + System.out.printf("Server [port : %d] listeners on %s\n", localServerPort, objectMapper.writeValueAsString(event)); + } +} diff --git a/spring-cloud-alibaba-examples/spring-cloud-bus-rocketmq-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/rocketmq/User.java b/spring-cloud-alibaba-examples/spring-cloud-bus-rocketmq-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/rocketmq/User.java new file mode 100644 index 000000000..ae22c92b7 --- /dev/null +++ b/spring-cloud-alibaba-examples/spring-cloud-bus-rocketmq-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/rocketmq/User.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.springframework.cloud.alibaba.cloud.examples.rocketmq; + +/** + * User Domain + * + * @author Mercy + * @since 0.2.1 + */ +public class User { + + private Long id; + + private String name; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return "User{" + + "id=" + id + + ", name='" + name + '\'' + + '}'; + } +} diff --git a/spring-cloud-alibaba-examples/spring-cloud-bus-rocketmq-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/rocketmq/UserRemoteApplicationEvent.java b/spring-cloud-alibaba-examples/spring-cloud-bus-rocketmq-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/rocketmq/UserRemoteApplicationEvent.java new file mode 100644 index 000000000..919e00ead --- /dev/null +++ b/spring-cloud-alibaba-examples/spring-cloud-bus-rocketmq-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/rocketmq/UserRemoteApplicationEvent.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.springframework.cloud.alibaba.cloud.examples.rocketmq; + +import org.springframework.cloud.bus.event.RemoteApplicationEvent; + +/** + * {@link User} {@link RemoteApplicationEvent} + * + * @author Mercy + * @since 0.2.1 + */ +public class UserRemoteApplicationEvent extends RemoteApplicationEvent { + + public UserRemoteApplicationEvent(User user, String originService, + String destinationService) { + super(user, originService, destinationService); + } + + public User getUser() { + return (User) getSource(); + } +} diff --git a/spring-cloud-alibaba-examples/spring-cloud-bus-rocketmq-example/src/main/resources/bootstrap.properties b/spring-cloud-alibaba-examples/spring-cloud-bus-rocketmq-example/src/main/resources/bootstrap.properties new file mode 100644 index 000000000..1b944de95 --- /dev/null +++ b/spring-cloud-alibaba-examples/spring-cloud-bus-rocketmq-example/src/main/resources/bootstrap.properties @@ -0,0 +1,4 @@ +spring.application.name=spring-cloud-bus-rocketmq-example +spring.cloud.stream.rocketmq.binder.namesrv-addr=127.0.0.1:9876 +server.port=8080 +spring.cloud.bus.id=${spring.application.name}:${server.port} \ No newline at end of file diff --git a/spring-cloud-starter-alibaba/pom.xml b/spring-cloud-starter-alibaba/pom.xml index c9bb0bb31..8726971a8 100644 --- a/spring-cloud-starter-alibaba/pom.xml +++ b/spring-cloud-starter-alibaba/pom.xml @@ -5,6 +5,7 @@ org.springframework.cloud spring-cloud-alibaba 0.2.1.BUILD-SNAPSHOT + ../pom.xml spring-cloud-starter-alibaba pom @@ -15,5 +16,6 @@ spring-cloud-starter-alibaba-nacos-discovery spring-cloud-starter-alibaba-sentinel spring-cloud-starter-stream-rocketmq + spring-cloud-starter-bus-rocketmq \ No newline at end of file diff --git a/spring-cloud-starter-alibaba/spring-cloud-starter-bus-rocketmq/pom.xml b/spring-cloud-starter-alibaba/spring-cloud-starter-bus-rocketmq/pom.xml new file mode 100644 index 000000000..f0f176436 --- /dev/null +++ b/spring-cloud-starter-alibaba/spring-cloud-starter-bus-rocketmq/pom.xml @@ -0,0 +1,33 @@ + + + + org.springframework.cloud + spring-cloud-starter-alibaba + 0.2.1.BUILD-SNAPSHOT + ../pom.xml + + 4.0.0 + + org.springframework.cloud + spring-cloud-starter-bus-rocketmq + Spring Cloud Alibaba Bus RocketMQ + + + + + + org.springframework.cloud + spring-cloud-stream-binder-rocketmq + + + + + org.springframework.cloud + spring-cloud-bus + + + + + \ No newline at end of file diff --git a/spring-cloud-starter-alibaba/spring-cloud-starter-bus-rocketmq/src/main/java/org/springframework/cloud/bus/rocketmq/env/RocketMQBusEnvironmentPostProcessor.java b/spring-cloud-starter-alibaba/spring-cloud-starter-bus-rocketmq/src/main/java/org/springframework/cloud/bus/rocketmq/env/RocketMQBusEnvironmentPostProcessor.java new file mode 100644 index 000000000..384db7271 --- /dev/null +++ b/spring-cloud-starter-alibaba/spring-cloud-starter-bus-rocketmq/src/main/java/org/springframework/cloud/bus/rocketmq/env/RocketMQBusEnvironmentPostProcessor.java @@ -0,0 +1,106 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.springframework.cloud.bus.rocketmq.env; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.env.EnvironmentPostProcessor; +import org.springframework.cloud.bus.BusEnvironmentPostProcessor; +import org.springframework.core.Ordered; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.MutablePropertySources; +import org.springframework.core.env.PropertySource; + +import java.util.HashMap; +import java.util.Map; + +import static org.springframework.cloud.bus.SpringCloudBusClient.INPUT; + +/** + * The lowest precedence {@link EnvironmentPostProcessor} configures default RocketMQ Bus Properties that will be + * appended into {@link SpringApplication#defaultProperties} + * + * @author Mercy + * @see BusEnvironmentPostProcessor + * @since 0.2.1 + */ +public class RocketMQBusEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered { + + /** + * The name of {@link PropertySource} of {@link SpringApplication#defaultProperties} + */ + private static final String PROPERTY_SOURCE_NAME = "defaultProperties"; + + @Override + public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { + + addDefaultPropertySource(environment); + + } + + private void addDefaultPropertySource(ConfigurableEnvironment environment) { + + Map map = new HashMap(); + + configureDefaultProperties(map); + + addOrReplace(environment.getPropertySources(), map); + } + + private void configureDefaultProperties(Map source) { + // Required Properties + String groupBindingPropertyName = createBindingPropertyName(INPUT, "group"); + source.put(groupBindingPropertyName, "rocketmq-bus-group"); + } + + private String createBindingPropertyName(String channel, String propertyName) { + return "spring.cloud.stream.bindings." + channel + "." + propertyName; + } + + /** + * Copy from {@link BusEnvironmentPostProcessor#addOrReplace(MutablePropertySources, Map)} + * + * @param propertySources {@link MutablePropertySources} + * @param map Default RocketMQ Bus Properties + */ + private void addOrReplace(MutablePropertySources propertySources, + Map map) { + MapPropertySource target = null; + if (propertySources.contains(PROPERTY_SOURCE_NAME)) { + PropertySource source = propertySources.get(PROPERTY_SOURCE_NAME); + if (source instanceof MapPropertySource) { + target = (MapPropertySource) source; + for (String key : map.keySet()) { + if (!target.containsProperty(key)) { + target.getSource().put(key, map.get(key)); + } + } + } + } + if (target == null) { + target = new MapPropertySource(PROPERTY_SOURCE_NAME, map); + } + if (!propertySources.contains(PROPERTY_SOURCE_NAME)) { + propertySources.addLast(target); + } + } + + @Override + public int getOrder() { + return LOWEST_PRECEDENCE; + } +} diff --git a/spring-cloud-starter-alibaba/spring-cloud-starter-bus-rocketmq/src/main/resources/META-INF/spring.factories b/spring-cloud-starter-alibaba/spring-cloud-starter-bus-rocketmq/src/main/resources/META-INF/spring.factories new file mode 100644 index 000000000..c5a9fb956 --- /dev/null +++ b/spring-cloud-starter-alibaba/spring-cloud-starter-bus-rocketmq/src/main/resources/META-INF/spring.factories @@ -0,0 +1,3 @@ +# EnvironmentPostProcessor +org.springframework.boot.env.EnvironmentPostProcessor=\ +org.springframework.cloud.bus.rocketmq.env.RocketMQBusEnvironmentPostProcessor \ No newline at end of file diff --git a/spring-cloud-starter-alibaba/spring-cloud-starter-stream-rocketmq/pom.xml b/spring-cloud-starter-alibaba/spring-cloud-starter-stream-rocketmq/pom.xml index 5b8ba75eb..39b5dd76b 100644 --- a/spring-cloud-starter-alibaba/spring-cloud-starter-stream-rocketmq/pom.xml +++ b/spring-cloud-starter-alibaba/spring-cloud-starter-stream-rocketmq/pom.xml @@ -6,6 +6,7 @@ org.springframework.cloud spring-cloud-starter-alibaba 0.2.1.BUILD-SNAPSHOT + ../pom.xml spring-cloud-starter-stream-rocketmq Spring Cloud Starter Stream RocketMQ diff --git a/spring-cloud-stream-binder-rocketmq/pom.xml b/spring-cloud-stream-binder-rocketmq/pom.xml index 9ff00a155..438d27d47 100644 --- a/spring-cloud-stream-binder-rocketmq/pom.xml +++ b/spring-cloud-stream-binder-rocketmq/pom.xml @@ -6,6 +6,7 @@ org.springframework.cloud spring-cloud-alibaba 0.2.1.BUILD-SNAPSHOT + ../pom.xml 4.0.0