Skip to content

Commit

Permalink
Polish : apache#321
Browse files Browse the repository at this point in the history
  • Loading branch information
mercyblitz committed Dec 14, 2018
1 parent cc3c0b5 commit fea78cd
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* 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 com.alibaba.boot.dubbo.env;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubboConfig;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubboConfigBinding;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.ContextIdApplicationContextInitializer;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
* The lowest precedence {@link EnvironmentPostProcessor} processes
* {@link SpringApplication#setDefaultProperties(Properties) Spring Boot default properties} for Dubbo
* as late as possible before {@link ConfigurableApplicationContext#refresh() application context refresh}.
*/
public class DubboDefaultPropertiesEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered {

/**
* The name of default {@link PropertySource} defined in SpringApplication#configurePropertySources method.
*/
private static final String PROPERTY_SOURCE_NAME = "defaultProperties";

/**
* The property name of Spring Application
*
* @see ContextIdApplicationContextInitializer
*/
private static final String SPRING_APPLICATION_NAME_PROPERTY = "spring.application.name";

/**
* The property name of {@link ApplicationConfig}
*
* @see EnableDubboConfig
* @see EnableDubboConfigBinding
*/
private static final String DUBBO_APPLICATION_NAME_PROPERTY = "dubbo.application.name";

@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
MutablePropertySources propertySources = environment.getPropertySources();
Map<String, Object> defaultProperties = createDefaultProperties(environment);
addOrReplace(propertySources, defaultProperties);
}

@Override
public int getOrder() {
return LOWEST_PRECEDENCE;
}

private Map<String, Object> createDefaultProperties(ConfigurableEnvironment environment) {
Map<String, Object> defaultProperties = new HashMap<String, Object>();
setDubboApplicationNameProperty(environment, defaultProperties);
return defaultProperties;
}

private void setDubboApplicationNameProperty(Environment environment, Map<String, Object> defaultProperties) {
String springApplicationName = environment.getProperty(SPRING_APPLICATION_NAME_PROPERTY);
defaultProperties.put(DUBBO_APPLICATION_NAME_PROPERTY, springApplicationName);
}

/**
* Copy from BusEnvironmentPostProcessor#addOrReplace(MutablePropertySources, Map)
*
* @param propertySources {@link MutablePropertySources}
* @param map Default Dubbo 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);
}
}

/**
* Process "defaultProperties" {@link PropertySource}
*
* @param defaultPropertySource {@link MapPropertySource}
*/
private void processDefaultProperties(MapPropertySource defaultPropertySource) {
setDubboApplicationNameProperty(defaultPropertySource);
}

private void setDubboApplicationNameProperty(MapPropertySource defaultPropertySource) {
String springApplicationName = (String) defaultPropertySource.getProperty(SPRING_APPLICATION_NAME_PROPERTY);
if (StringUtils.hasLength(springApplicationName)) {
setPropertyIfAbsent(defaultPropertySource, DUBBO_APPLICATION_NAME_PROPERTY, springApplicationName);
}
}

private void setPropertyIfAbsent(MapPropertySource propertySource, String propertyName, Object propertyValue) {
if (!propertySource.containsProperty(propertyName)) {
propertySource.getSource().put(propertyName, propertyValue);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ com.alibaba.boot.dubbo.autoconfigure.DubboAutoConfiguration
org.springframework.context.ApplicationListener=\
com.alibaba.boot.dubbo.context.event.OverrideDubboConfigApplicationListener,\
com.alibaba.boot.dubbo.context.event.WelcomeLogoApplicationListener,\
com.alibaba.boot.dubbo.context.event.AwaitingNonWebApplicationListener
com.alibaba.boot.dubbo.context.event.AwaitingNonWebApplicationListener

org.springframework.boot.env.EnvironmentPostProcessor=\
com.alibaba.boot.dubbo.env.DubboDefaultPropertiesEnvironmentPostProcessor
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
* limitations under the License.
*/
package com.alibaba.boot.dubbo.demo.consumer.controller;

import com.alibaba.boot.dubbo.demo.consumer.DemoService;
import com.alibaba.dubbo.config.annotation.Reference;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -31,8 +33,8 @@
@RestController
public class DemoConsumerController {

@Reference(version = "${demo.service.version}",
application = "${dubbo.application.id}",
@Reference(
version = "${demo.service.version}",
url = "dubbo://localhost:12345")
private DemoService demoService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ management.server.port = 8081
demo.service.version = 1.0.0

# Dubbo Config properties
## ApplicationConfig Bean
dubbo.application.id = dubbo-consumer-demo
dubbo.application.name = dubbo-consumer-demo

## Legacy QOS Config
dubbo.qos.port = 22223

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
*/
@Service(
version = "${demo.service.version}",
application = "${dubbo.application.id}",
protocol = "${dubbo.protocol.id}",
registry = "${dubbo.registry.id}"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ dubbo.scan.basePackages = com.alibaba.boot.dubbo.demo.provider.service

# Dubbo Config properties
## ApplicationConfig Bean
dubbo.application.id = dubbo-provider-demo
dubbo.application.name = dubbo-provider-demo
dubbo.application.qos.port=22222
dubbo.application.qos.port=0
dubbo.application.qos.enable=true

## ProtocolConfig Bean
Expand Down

0 comments on commit fea78cd

Please sign in to comment.