Merge pull request #119 from mercyblitz/master

[Feature] Add Spring Cloud Bus module for Apache RocketMQ
pull/128/head
format 6 years ago committed by GitHub
commit 47b1f05e86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -69,6 +69,7 @@
<spring-cloud-commons.version>2.0.2.RELEASE</spring-cloud-commons.version>
<spring-cloud-netflix.version>2.0.2.RELEASE</spring-cloud-netflix.version>
<spring-cloud-openfeign.version>2.0.2.RELEASE</spring-cloud-openfeign.version>
<spring-cloud-bus.version>2.0.0.RELEASE</spring-cloud-bus.version>
<junit.version>4.12</junit.version>
<javax-servlet-api>3.0</javax-servlet-api>
@ -99,6 +100,7 @@
<module>spring-cloud-alicloud-oss</module>
<module>spring-cloud-alicloud-acm</module>
<module>spring-cloud-alicloud-ans</module>
<module>spring-cloud-starter-bus-rocketmq</module>
</modules>
<dependencyManagement>
@ -143,6 +145,14 @@
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus-dependencies</artifactId>
<version>${spring-cloud-bus.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

@ -9,6 +9,7 @@
<version>2.0.4.RELEASE</version>
<relativePath/>
</parent>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.1.BUILD-SNAPSHOT</version>
<packaging>pom</packaging>
@ -25,7 +26,6 @@
<alicloud.context.version>1.0.0</alicloud.context.version>
<aliyun.sdk.edas.version>2.16.0</aliyun.sdk.edas.version>
<rocketmq.version>4.3.1</rocketmq.version>
<metrics.core>4.0.3</metrics.core>
</properties>
<dependencyManagement>
@ -76,6 +76,13 @@
<version>${nacos.version}</version>
</dependency>
<!-- Apache RocketMQ -->
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>${rocketmq.version}</version>
</dependency>
<!-- Sentinel -->
<dependency>
<groupId>com.alibaba.csp</groupId>
@ -127,11 +134,6 @@
<artifactId>sentinel-dubbo-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>${rocketmq.version}</version>
</dependency>
<!-- Aliyun OSS dependencies -->
@ -231,18 +233,17 @@
<version>${project.version}</version>
</dependency>
<!-- Third dependencies -->
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>${metrics.core}</version>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-rocketmq</artifactId>
<version>${project.version}</version>
</dependency>
<!-- Testing Dependencies -->
</dependencies>
</dependencyManagement>
<profiles>
<profile>
<id>spring</id>

@ -6,6 +6,7 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba</artifactId>
<version>0.2.1.BUILD-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
@ -28,6 +29,7 @@
<module>ans-example/ans-provider-example</module>
<module>acm-example/acm-local-example</module>
<module>rocketmq-example</module>
<module>spring-cloud-bus-rocketmq-example</module>
</modules>
<build>

@ -6,6 +6,7 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-examples</artifactId>
<version>0.2.1.BUILD-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
@ -25,6 +26,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>

@ -0,0 +1,54 @@
<?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>spring-cloud-alibaba-examples</artifactId>
<groupId>org.springframework.cloud</groupId>
<version>0.2.1.BUILD-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus-rocketmq-example</artifactId>
<name>Spring Cloud Bus RocketMQ Example</name>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-rocketmq</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>${maven-deploy-plugin.version}</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>

@ -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 <a href="mailto:mercyblitz@gmail.com">Mercy</a>
* @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));
}
}

@ -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 <a href="mailto:mercyblitz@gmail.com">Mercy</a>
* @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 + '\'' +
'}';
}
}

@ -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 <a href="mailto:mercyblitz@gmail.com">Mercy</a>
* @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();
}
}

@ -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}

@ -5,6 +5,7 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba</artifactId>
<version>0.2.1.BUILD-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>spring-cloud-starter-alibaba</artifactId>
<packaging>pom</packaging>
@ -15,5 +16,6 @@
<module>spring-cloud-starter-alibaba-nacos-discovery</module>
<module>spring-cloud-starter-alibaba-sentinel</module>
<module>spring-cloud-starter-stream-rocketmq</module>
<module>spring-cloud-starter-bus-rocketmq</module>
</modules>
</project>

@ -0,0 +1,33 @@
<?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>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba</artifactId>
<version>0.2.1.BUILD-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-rocketmq</artifactId>
<name>Spring Cloud Alibaba Bus RocketMQ</name>
<dependencies>
<!-- Spring Cloud Stream RocketMQ -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rocketmq</artifactId>
</dependency>
<!-- Spring Cloud Bus -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
</dependencies>
</project>

@ -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 <a href="mailto:mercyblitz@gmail.com">Mercy</a>
* @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<String, Object> map = new HashMap<String, Object>();
configureDefaultProperties(map);
addOrReplace(environment.getPropertySources(), map);
}
private void configureDefaultProperties(Map<String, Object> 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<String, Object> 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;
}
}

@ -0,0 +1,3 @@
# EnvironmentPostProcessor
org.springframework.boot.env.EnvironmentPostProcessor=\
org.springframework.cloud.bus.rocketmq.env.RocketMQBusEnvironmentPostProcessor

@ -6,6 +6,7 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba</artifactId>
<version>0.2.1.BUILD-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
<name>Spring Cloud Starter Stream RocketMQ</name>

@ -6,6 +6,7 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba</artifactId>
<version>0.2.1.BUILD-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

Loading…
Cancel
Save