Skip to content

Commit

Permalink
function mode server code completed
Browse files Browse the repository at this point in the history
  • Loading branch information
xuechaos committed Feb 27, 2019
1 parent 931a0f4 commit 5711ace
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.alibaba.nacos.common.util;

import java.io.InputStream;
import java.util.Properties;

/**
* @author xingxuechao
* on:2019/2/27 12:32 PM
*/
public class VersionUtils {

public static String VERSION;
/**获取当前version*/
public static final String VERSION_DEFAULT = "${pom.version}";


static{
try{
InputStream in = VersionUtils.class.getClassLoader()
.getResourceAsStream("nacos-version.txt");
Properties props = new Properties();
props.load(in);
String val = props.getProperty("version");
if (val != null && !VERSION_DEFAULT.equals(val)) {
VERSION = val;
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* 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 com.alibaba.nacos.console.controller;


import com.alibaba.nacos.common.util.VersionUtils;
import com.alibaba.nacos.core.utils.SystemUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;

/**
* @author xingxuechao
* on:2019/2/27 11:17 AM
*/
@RestController
@RequestMapping("/v1/console/server")
public class ServerStateController {

private static final Logger logger = LoggerFactory.getLogger(ServerStateController.class);


public static String VERSION ;


@ResponseBody
@RequestMapping(value = "state", method = RequestMethod.GET)
public ResponseEntity serverState() {
Map<String,String> serverState = new HashMap<String, String>(3);
serverState.put("standalone_mode",SystemUtils.STANDALONE_MODE ?
SystemUtils.STANDALONE_MODE_ALONE : SystemUtils.STANDALONE_MODE_CLUSTER);

serverState.put("function_mode", SystemUtils.FUNCTION_MODE);
serverState.put("version", VersionUtils.VERSION);

return ResponseEntity.ok().body(serverState);
}



}
37 changes: 37 additions & 0 deletions console/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
# spring

server.contextPath=/nacos
server.servlet.contextPath=/nacos
server.port=8848

nacos.cmdb.dumpTaskInterval=3600
nacos.cmdb.eventTaskInterval=10
nacos.cmdb.labelTaskInterval=300
nacos.cmdb.loadDataAtStart=false


# metrics for prometheus
#management.endpoints.web.exposure.include=*

# metrics for elastic search
management.metrics.export.elastic.enabled=false
#management.metrics.export.elastic.host=http://localhost:9200

# metrics for influx
management.metrics.export.influx.enabled=false
#management.metrics.export.influx.db=springboot
#management.metrics.export.influx.uri=http://localhost:8086
#management.metrics.export.influx.auto-create-db=true
#management.metrics.export.influx.consistency=one
#management.metrics.export.influx.compressed=true

server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D
# default current work dir
server.tomcat.basedir=

## spring security config
### turn off security
#spring.security.enabled=false
#management.security=false
#security.basic.enabled=false
#nacos.security.ignore.urls=/**

nacos.security.ignore.urls=/,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/login,/v1/console/health/**,/v1/cs/**,/v1/ns/**,/v1/cmdb/**,/actuator/**,/v1/console/server/**
1 change: 1 addition & 0 deletions console/src/main/resources/nacos-version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version=${pom.version}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.alibaba.nacos.core.listener;

import com.alibaba.nacos.core.utils.SystemUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
Expand All @@ -31,6 +32,7 @@
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

import static com.alibaba.nacos.core.utils.SystemUtils.FUNCTION_MODE;
import static com.alibaba.nacos.core.utils.SystemUtils.LOCAL_IP;
import static com.alibaba.nacos.core.utils.SystemUtils.NACOS_HOME;
import static com.alibaba.nacos.core.utils.SystemUtils.STANDALONE_MODE;
Expand All @@ -46,7 +48,9 @@ public class StartingSpringApplicationRunListener implements SpringApplicationRu

private static final Logger LOGGER = LoggerFactory.getLogger(StartingSpringApplicationRunListener.class);

private static final String MODE_PROPERTY_KEY = "nacos.mode";
private static final String MODE_PROPERTY_KEY_STAND_MODE = "nacos.mode";

private static final String MODE_PROPERTY_KEY_FUNCTION_MODE = "nacos.function.mode";

private static final String LOCAL_IP_PROPERTY_KEY = "nacos.local.ip";

Expand All @@ -66,11 +70,19 @@ public void starting() {
@Override
public void environmentPrepared(ConfigurableEnvironment environment) {
if (STANDALONE_MODE) {
System.setProperty(MODE_PROPERTY_KEY, "stand alone");
System.setProperty(MODE_PROPERTY_KEY_STAND_MODE, "stand alone");
} else {
System.setProperty(MODE_PROPERTY_KEY, "cluster");
System.setProperty(MODE_PROPERTY_KEY_STAND_MODE, "cluster");
}
if (FUNCTION_MODE == null) {
System.setProperty(MODE_PROPERTY_KEY_FUNCTION_MODE, "All");
} else if(SystemUtils.FUNCTION_MODE_CONFIG.equals(FUNCTION_MODE)){
System.setProperty(MODE_PROPERTY_KEY_FUNCTION_MODE, SystemUtils.FUNCTION_MODE_CONFIG);
} else if(SystemUtils.FUNCTION_MODE_NAMING.equals(FUNCTION_MODE)) {
System.setProperty(MODE_PROPERTY_KEY_FUNCTION_MODE, SystemUtils.FUNCTION_MODE_NAMING);
}


System.setProperty(LOCAL_IP_PROPERTY_KEY, LOCAL_IP);
}

Expand All @@ -96,7 +108,7 @@ public void started(ConfigurableApplicationContext context) {

logFilePath();

LOGGER.info("Nacos started successfully in {} mode.", System.getProperty(MODE_PROPERTY_KEY));
LOGGER.info("Nacos started successfully in {} mode.", System.getProperty(MODE_PROPERTY_KEY_STAND_MODE));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public interface Constants {
*/
String STANDALONE_MODE_PROPERTY_NAME = "nacos.standalone";

/**
* The System property name of Function mode
*/
String FUNCTION_MODE_PROPERTY_NAME = "nacos.functionMode";

/**
* The System property name of prefer hostname over ip
*/
Expand Down
17 changes: 14 additions & 3 deletions core/src/main/java/com/alibaba/nacos/core/utils/SystemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.alibaba.nacos.core.utils.Constants.PREFER_HOSTNAME_OVER_IP_PROPERTY_NAME;
import static com.alibaba.nacos.core.utils.Constants.FUNCTION_MODE_PROPERTY_NAME;
import static com.alibaba.nacos.core.utils.Constants.STANDALONE_MODE_PROPERTY_NAME;
import static org.apache.commons.lang3.CharEncoding.UTF_8;

Expand All @@ -49,6 +47,19 @@ public class SystemUtils {
*/
public static boolean STANDALONE_MODE = Boolean.getBoolean(STANDALONE_MODE_PROPERTY_NAME);

public static final String STANDALONE_MODE_ALONE = "standalone";
public static final String STANDALONE_MODE_CLUSTER = "cluster";

/**
* server
*/
public static String FUNCTION_MODE = System.getProperty(FUNCTION_MODE_PROPERTY_NAME);

public static final String FUNCTION_MODE_CONFIG = "config";
public static final String FUNCTION_MODE_NAMING = "naming";



private static OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean)ManagementFactory
.getOperatingSystemMXBean();

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/resources/banner.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
,--.
,--.'|
,--,: : | Nacos ${application.version}
,`--.'`| ' : ,---. Running in ${nacos.mode} mode
,`--.'`| ' : ,---. Running in ${nacos.mode} mode, ${nacos.function.mode} function modules
| : : | | ' ,'\ .--.--. Port: ${server.port}
: | \ | : ,--.--. ,---. / / | / / ' Pid: ${pid}
| : ' '; | / \ / \. ; ,. :| : /`./ Console: http://${nacos.local.ip}:${server.port}/nacos/index.html
Expand Down
15 changes: 12 additions & 3 deletions distribution/bin/startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ if [ -z "$JAVA_HOME" ]; then
fi

export MODE="cluster"
while getopts ":m:" opt
export FUNCTION_MODE="all"
while getopts ":m:f:" opt
do
case $opt in
m)
MODE=$OPTARG
;;
MODE=$OPTARG;;
f)
FUNCTION_MODE=$OPTARG;;
?)
echo "Unknown parameter"
exit 1;;
Expand All @@ -83,6 +85,13 @@ else

fi

if [[ "${FUNCTION_MODE}" == "config" ]]; then
JAVA_OPT="${JAVA_OPT} -Dnacos.functionMode=config"
elif [[ "${FUNCTION_MODE}" == "naming" ]]; then
JAVA_OPT="${JAVA_OPT} -Dnacos.functionMode=naming"
fi


JAVA_MAJOR_VERSION=$($JAVA -version 2>&1 | sed -E -n 's/.* version "([0-9]*).*$/\1/p')
if [[ "$JAVA_MAJOR_VERSION" -ge "9" ]] ; then
JAVA_OPT="${JAVA_OPT} -cp .:${BASE_DIR}/plugins/cmdb/*.jar"
Expand Down
2 changes: 1 addition & 1 deletion distribution/conf/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ server.tomcat.basedir=
#security.basic.enabled=false
#nacos.security.ignore.urls=/**

nacos.security.ignore.urls=/,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/login,/v1/console/health,/v1/cs/**,/v1/ns/**,/v1/cmdb/**,/actuator/**
nacos.security.ignore.urls=/,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/login,/v1/console/health/**,/v1/cs/**,/v1/ns/**,/v1/cmdb/**,/actuator/**,/v1/console/server/**

0 comments on commit 5711ace

Please sign in to comment.