Skip to content

Commit

Permalink
[SCB-2024]support not resolve place holder when running in spring boot
Browse files Browse the repository at this point in the history
  • Loading branch information
liubao68 committed Jun 24, 2020
1 parent 2b4c904 commit e622d17
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 8 deletions.
Expand Up @@ -178,6 +178,11 @@ private Map<String, Object> getAllProperties(Environment environment) {
}

ConfigurableEnvironment configurableEnvironment = (ConfigurableEnvironment) environment;

if (ignoreResolveFailure()) {
configurableEnvironment.setIgnoreUnresolvableNestedPlaceholders(true);
}

for (PropertySource<?> propertySource : configurableEnvironment.getPropertySources()) {
getProperties(configurableEnvironment, propertySource, configFromSpringBoot);
}
Expand All @@ -201,13 +206,9 @@ private void getProperties(ConfigurableEnvironment environment, PropertySource<?
try {
configFromSpringBoot.put(propertyName, environment.getProperty(propertyName, Object.class));
} catch (Exception e) {
if (ignoreResolveFailure()) {
LOGGER.warn("set up spring property source failed.", e);
} else {
throw new RuntimeException(
"set up spring property source failed.If you still want to start up the application and ignore errors, you can set servicecomb.config.ignoreResolveFailure to true.",
e);
}
throw new RuntimeException(
"set up spring property source failed.If you still want to start up the application and ignore errors, you can set servicecomb.config.ignoreResolveFailure to true.",
e);
}
}
return;
Expand Down
@@ -0,0 +1,45 @@
/*
* 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.apache.servicecomb.springboot.springmvc.client;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

import org.apache.servicecomb.demo.TestMgr;
import org.junit.Before;
import org.junit.Test;

public class SpringmvcClientIT {
@Before
public void setUp() throws Exception {
TestMgr.errors().clear();
}

@Test
public void clientGetsNoError() throws Exception {
try {
SpringmvcClient.main(new String[0]);

assertThat(TestMgr.errors().isEmpty(), is(true));
} catch (Throwable e) {
e.printStackTrace();
fail("test case failed, message=" + e.getMessage());
}
}
}
Expand Up @@ -22,7 +22,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>demo-spring-boot-springmvc-server</artifactId>
<name>Java Chassis::Demo::Spring Boot::Spring MVC Client</name>
<name>Java Chassis::Demo::Spring Boot::Spring MVC Server</name>
<parent>
<groupId>org.apache.servicecomb.demo</groupId>
<artifactId>demo-spring-boot</artifactId>
Expand Down
Expand Up @@ -17,16 +17,33 @@

package org.apache.servicecomb.springboot.springmvc.server;

import org.apache.servicecomb.core.SCBEngine;
import org.apache.servicecomb.springboot2.starter.EnableServiceComb;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

import com.netflix.config.DynamicPropertyFactory;

@SpringBootApplication
@EnableServiceComb
public class SpringmvcServer {
private static final Logger LOGGER = LoggerFactory.getLogger(SpringmvcServer.class);

public static void main(final String[] args) throws Exception {
new SpringApplicationBuilder().sources(SpringmvcServer.class).web(WebApplicationType.NONE).build().run(args);

assertPropertyCorrect();
}

private static void assertPropertyCorrect() {
String result = DynamicPropertyFactory.getInstance()
.getStringProperty("test.unresolved.placeholder", null).get();
if (!"jdbc:postgresql://${ip}:${port}/pt".equals(result)) {
LOGGER.error("tests for configuration error, stop");
SCBEngine.getInstance().destroy();
}
}
}
Expand Up @@ -17,3 +17,5 @@

server:
port: 7999

test.unresolved.placeholder: jdbc:postgresql://${ip}:${port}/pt
@@ -0,0 +1,20 @@
## ---------------------------------------------------------------------------
## 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.
## ---------------------------------------------------------------------------

servicecomb:
config:
ignoreResolveFailure: true

0 comments on commit e622d17

Please sign in to comment.