mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Merge pull request #372 from xiaolongzuo/master
Add statistics for nacos discovery and test case
This commit is contained in:
commit
5da1fbb6c1
@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 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
|
||||||
|
*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xiaolongzuo
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
public class HelloController {
|
||||||
|
|
||||||
|
@RequestMapping("/")
|
||||||
|
@ResponseBody
|
||||||
|
public String hello() {
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -16,25 +16,25 @@
|
|||||||
|
|
||||||
package org.springframework.cloud.alibaba.cloud.examples;
|
package org.springframework.cloud.alibaba.cloud.examples;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
||||||
import com.alibaba.edas.schedulerx.ProcessResult;
|
import com.alibaba.edas.schedulerx.ProcessResult;
|
||||||
import com.alibaba.edas.schedulerx.ScxSimpleJobContext;
|
import com.alibaba.edas.schedulerx.ScxSimpleJobContext;
|
||||||
import com.alibaba.edas.schedulerx.ScxSimpleJobProcessor;
|
import com.alibaba.edas.schedulerx.ScxSimpleJobProcessor;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author xiaolongzuo
|
* @author xiaolongzuo
|
||||||
*/
|
*/
|
||||||
public class SimpleTask implements ScxSimpleJobProcessor {
|
public class SimpleTask implements ScxSimpleJobProcessor {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private TestService testService;
|
private TestService testService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ProcessResult process(ScxSimpleJobContext context) {
|
public ProcessResult process(ScxSimpleJobContext context) {
|
||||||
System.out.println("-----------Hello world---------------");
|
testService.test();
|
||||||
testService.test();
|
ProcessResult processResult = new ProcessResult(true);
|
||||||
ProcessResult processResult = new ProcessResult(true);
|
return processResult;
|
||||||
return processResult;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 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
|
||||||
|
*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xiaolongzuo
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
public class TestController {
|
||||||
|
|
||||||
|
static AtomicInteger atomicInteger = new AtomicInteger(0);
|
||||||
|
|
||||||
|
@RequestMapping("/test")
|
||||||
|
@ResponseBody
|
||||||
|
public String test() {
|
||||||
|
return String.valueOf(atomicInteger.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -24,7 +24,7 @@ import org.springframework.stereotype.Service;
|
|||||||
@Service
|
@Service
|
||||||
public class TestService {
|
public class TestService {
|
||||||
|
|
||||||
public void test() {
|
public void test() {
|
||||||
System.out.println("---------IOC Success--------");
|
TestController.atomicInteger.incrementAndGet();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,15 +15,15 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.cloud.alicloud.context.nacos;
|
package org.springframework.cloud.alicloud.context.nacos;
|
||||||
|
|
||||||
import com.alibaba.cloud.context.edas.EdasChangeOrderConfiguration;
|
import java.util.Properties;
|
||||||
import com.alibaba.cloud.context.edas.EdasChangeOrderConfigurationFactory;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
|
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
|
||||||
import org.springframework.cloud.alicloud.context.listener.AbstractOnceApplicationListener;
|
import org.springframework.cloud.alicloud.context.listener.AbstractOnceApplicationListener;
|
||||||
|
|
||||||
import java.util.Properties;
|
import com.alibaba.cloud.context.edas.EdasChangeOrderConfiguration;
|
||||||
|
import com.alibaba.cloud.context.edas.EdasChangeOrderConfigurationFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author pbting
|
* @author pbting
|
||||||
@ -54,7 +54,7 @@ public class NacosDiscoveryParameterInitListener
|
|||||||
}
|
}
|
||||||
// initialize nacos configuration
|
// initialize nacos configuration
|
||||||
Properties properties = System.getProperties();
|
Properties properties = System.getProperties();
|
||||||
|
properties.setProperty("spring.cloud.nacos.discovery.server-mode", "EDAS");
|
||||||
// step 1: set some properties for spring cloud alibaba nacos discovery
|
// step 1: set some properties for spring cloud alibaba nacos discovery
|
||||||
properties.setProperty("spring.cloud.nacos.discovery.server-addr", "");
|
properties.setProperty("spring.cloud.nacos.discovery.server-addr", "");
|
||||||
properties.setProperty("spring.cloud.nacos.discovery.endpoint",
|
properties.setProperty("spring.cloud.nacos.discovery.endpoint",
|
||||||
|
@ -31,13 +31,33 @@ import com.aliyun.oss.ClientBuilderConfiguration;
|
|||||||
@ConfigurationProperties("spring.cloud.alicloud.oss")
|
@ConfigurationProperties("spring.cloud.alicloud.oss")
|
||||||
public class OssProperties {
|
public class OssProperties {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Authorization Mode, please see <a href=
|
||||||
|
* "https://help.aliyun.com/document_detail/32010.html?spm=a2c4g.11186623.6.659.29f145dc3KOwTh">oss
|
||||||
|
* docs</a>.
|
||||||
|
*/
|
||||||
@Value("${spring.cloud.alicloud.oss.authorization-mode:AK_SK}")
|
@Value("${spring.cloud.alicloud.oss.authorization-mode:AK_SK}")
|
||||||
private AliCloudAuthorizationMode authorizationMode;
|
private AliCloudAuthorizationMode authorizationMode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Endpoint, please see <a href=
|
||||||
|
* "https://help.aliyun.com/document_detail/32010.html?spm=a2c4g.11186623.6.659.29f145dc3KOwTh">oss
|
||||||
|
* docs</a>.
|
||||||
|
*/
|
||||||
private String endpoint;
|
private String endpoint;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sts token, please see <a href=
|
||||||
|
* "https://help.aliyun.com/document_detail/32010.html?spm=a2c4g.11186623.6.659.29f145dc3KOwTh">oss
|
||||||
|
* docs</a>.
|
||||||
|
*/
|
||||||
private StsToken sts;
|
private StsToken sts;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Client Configuration, please see <a href=
|
||||||
|
* "https://help.aliyun.com/document_detail/32010.html?spm=a2c4g.11186623.6.659.29f145dc3KOwTh">oss
|
||||||
|
* docs</a>.
|
||||||
|
*/
|
||||||
private ClientBuilderConfiguration config;
|
private ClientBuilderConfiguration config;
|
||||||
|
|
||||||
public AliCloudAuthorizationMode getAuthorizationMode() {
|
public AliCloudAuthorizationMode getAuthorizationMode() {
|
||||||
|
@ -26,8 +26,18 @@ import com.alibaba.cloud.context.scx.ScxConfiguration;
|
|||||||
@ConfigurationProperties("spring.cloud.alicloud.scx")
|
@ConfigurationProperties("spring.cloud.alicloud.scx")
|
||||||
public class ScxProperties implements ScxConfiguration {
|
public class ScxProperties implements ScxConfiguration {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Group id, please see <a href=
|
||||||
|
* "https://help.aliyun.com/document_detail/35359.html?spm=a2c4g.11186623.6.721.69ca5763p9IJly">scx
|
||||||
|
* docs</a>.
|
||||||
|
*/
|
||||||
private String groupId;
|
private String groupId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Domain name, please see <a href=
|
||||||
|
* "https://help.aliyun.com/document_detail/35359.html?spm=a2c4g.11186623.6.721.69ca5763p9IJly">scx
|
||||||
|
* docs</a>.
|
||||||
|
*/
|
||||||
private String domainName;
|
private String domainName;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -48,7 +48,9 @@ public class StatisticsTaskStarter implements InitializingBean {
|
|||||||
|
|
||||||
private static final String NACOS_CONFIG_SERVER_MODE_KEY = "spring.cloud.nacos.config.server-mode";
|
private static final String NACOS_CONFIG_SERVER_MODE_KEY = "spring.cloud.nacos.config.server-mode";
|
||||||
|
|
||||||
private static final String NACOS_CONFIG_SERVER_MODE_VALUE = "EDAS";
|
private static final String NACOS_DISCOVERY_SERVER_MODE_KEY = "spring.cloud.nacos.discovery.server-mode";
|
||||||
|
|
||||||
|
private static final String NACOS_SERVER_MODE_VALUE = "EDAS";
|
||||||
|
|
||||||
@Autowired(required = false)
|
@Autowired(required = false)
|
||||||
private AliCloudEdasSdk aliCloudEdasSdk;
|
private AliCloudEdasSdk aliCloudEdasSdk;
|
||||||
@ -106,10 +108,14 @@ public class StatisticsTaskStarter implements InitializingBean {
|
|||||||
if (acmContextBootstrapConfiguration != null && acmEnableEdas) {
|
if (acmContextBootstrapConfiguration != null && acmEnableEdas) {
|
||||||
components.add("SC-ACM");
|
components.add("SC-ACM");
|
||||||
}
|
}
|
||||||
if (NACOS_CONFIG_SERVER_MODE_VALUE
|
if (NACOS_SERVER_MODE_VALUE
|
||||||
.equals(System.getProperty(NACOS_CONFIG_SERVER_MODE_KEY))) {
|
.equals(System.getProperty(NACOS_CONFIG_SERVER_MODE_KEY))) {
|
||||||
components.add("SC-NACOS-CONFIG");
|
components.add("SC-NACOS-CONFIG");
|
||||||
}
|
}
|
||||||
|
if (NACOS_SERVER_MODE_VALUE
|
||||||
|
.equals(System.getProperty(NACOS_DISCOVERY_SERVER_MODE_KEY))) {
|
||||||
|
components.add("SC-NACOS-DISCOVERY");
|
||||||
|
}
|
||||||
return components;
|
return components;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,23 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 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
|
||||||
|
*
|
||||||
|
* 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.nacos;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xiaolongzuo
|
||||||
|
*/
|
||||||
|
public class NacosDiscoveryAutoConfiguration {
|
||||||
|
}
|
@ -16,8 +16,7 @@
|
|||||||
|
|
||||||
package org.springframework.cloud.alicloud.context.nacos;
|
package org.springframework.cloud.alicloud.context.nacos;
|
||||||
|
|
||||||
import com.alibaba.cloud.context.ans.AliCloudAnsInitializer;
|
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||||
import com.alibaba.cloud.context.edas.EdasChangeOrderConfigurationFactory;
|
|
||||||
|
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -25,35 +24,39 @@ import org.powermock.core.classloader.annotations.PrepareForTest;
|
|||||||
import org.springframework.cloud.alicloud.context.BaseAliCloudSpringApplication;
|
import org.springframework.cloud.alicloud.context.BaseAliCloudSpringApplication;
|
||||||
import org.springframework.cloud.alicloud.utils.ChangeOrderUtils;
|
import org.springframework.cloud.alicloud.utils.ChangeOrderUtils;
|
||||||
|
|
||||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
import com.alibaba.cloud.context.ans.AliCloudAnsInitializer;
|
||||||
|
import com.alibaba.cloud.context.edas.EdasChangeOrderConfigurationFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author xiaolongzuo
|
* @author xiaolongzuo
|
||||||
*/
|
*/
|
||||||
@PrepareForTest({EdasChangeOrderConfigurationFactory.class,
|
@PrepareForTest({ EdasChangeOrderConfigurationFactory.class,
|
||||||
NacosConfigParameterInitListener.class, AliCloudAnsInitializer.class})
|
NacosDiscoveryParameterInitListener.class, AliCloudAnsInitializer.class })
|
||||||
public class NacosDiscoveryParameterInitListenerTests extends BaseAliCloudSpringApplication {
|
public class NacosDiscoveryParameterInitListenerTests
|
||||||
|
extends BaseAliCloudSpringApplication {
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void setUp() {
|
public static void setUp() {
|
||||||
ChangeOrderUtils.mockChangeOrder();
|
ChangeOrderUtils.mockChangeOrder();
|
||||||
System.getProperties().setProperty("webContext", "/vipserver");
|
}
|
||||||
System.getProperties().setProperty("serverPort", "80");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNacosParameterInitListener() {
|
public void testNacosParameterInitListener() {
|
||||||
assertThat(System.getProperty("spring.cloud.nacos.config.server-addr"))
|
assertThat(System.getProperty("spring.cloud.nacos.discovery.server-mode"))
|
||||||
.isEqualTo("");
|
.isEqualTo("EDAS");
|
||||||
assertThat(System.getProperty("spring.cloud.nacos.config.endpoint"))
|
assertThat(System.getProperty("spring.cloud.nacos.discovery.server-addr"))
|
||||||
.isEqualTo("testDomain");
|
.isEqualTo("");
|
||||||
assertThat(System.getProperty("spring.cloud.nacos.config.namespace"))
|
assertThat(System.getProperty("spring.cloud.nacos.discovery.endpoint"))
|
||||||
.isEqualTo("testTenantId");
|
.isEqualTo("testDomain");
|
||||||
assertThat(System.getProperty("spring.cloud.nacos.config.access-key"))
|
assertThat(System.getProperty("spring.cloud.nacos.discovery.namespace"))
|
||||||
.isEqualTo("testAK");
|
.isEqualTo("testTenantId");
|
||||||
assertThat(System.getProperty("spring.cloud.nacos.config.secret-key"))
|
assertThat(System.getProperty("spring.cloud.nacos.discovery.access-key"))
|
||||||
.isEqualTo("testSK");
|
.isEqualTo("testAK");
|
||||||
assertThat(System.getProperties().getProperty("webContext")).isEqualTo("/vipserver");
|
assertThat(System.getProperty("spring.cloud.nacos.discovery.secret-key"))
|
||||||
assertThat(System.getProperties().getProperty("serverPort")).isEqualTo("80");
|
.isEqualTo("testSK");
|
||||||
}
|
assertThat(System.getProperties().getProperty("nacos.naming.web.context"))
|
||||||
|
.isEqualTo("/vipserver");
|
||||||
|
assertThat(System.getProperties().getProperty("nacos.naming.exposed.port"))
|
||||||
|
.isEqualTo("80");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user