attlas / attlas-scraper

2 stars 2 forks source link

Read port & address to listen .etc from environment variables #14

Open VladyslavKurmaz opened 6 years ago

VladyslavKurmaz commented 6 years ago

https://www.programmergate.com/how-to-change-the-default-port-of-spring-boot-application/

fac30ff commented 6 years ago

default port at application.properties changed

VladyslavKurmaz commented 6 years ago

We need to read property from env variable, otherwise it will be hardcoded at compile time. We should be able to configure port from docker level.

fac30ff commented 6 years ago

We can use server.port=0 for random port of application? Or we need to change it with java -Dserver.port=9090 -jar executable.jar java -jar executable.jar –server.port=9090? Or write class with listener from comand line?

fac30ff commented 6 years ago

Technically I wrote a class that should receive as first string parameter while runing application port number: @Component public class SpringBootServerPortConfig implements CommandLineRunner {

@Override public void run(String... args) throws Exception { new WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>() { @Override public void customize(ConfigurableServletWebServerFactory factory) { factory.setPort(Integer.parseInt(args[0])); } }; } }

VladyslavKurmaz commented 6 years ago

COMPONENT_PARAM_HOST=localhost COMPONENT_PARAM_LSTN=0.0.0.0 COMPONENT_PARAM_PORT=8080 COMPONENT_PARAM_PORTS=80443 COMPONENT_PARAM_CORS=** COMPONENT_PARAM_MONGO_HOST=localhost COMPONENT_PARAM_MONGO_PORT=27017

fac30ff commented 6 years ago

Is there are 2 possible way inject bean with @value of environment variables or process it to application.properties Also in second option can be created numerous number of application."variable".properties files with environment for concrete system

fac30ff commented 6 years ago

so 1st option is: example @Component public class SpringBootServerPortConfig implements CommandLineRunner {

@Value("$(COMPONENT_PARAM_PORT)") private String COMPONENT_PARAM_PORT;

private final Logger logger = LoggerFactory.getLogger(getClass());

@Override public void run(String... args) throws Exception { new WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>() { @Override public void customize(ConfigurableServletWebServerFactory factory) { factory.setPort(Integer.parseInt(COMPONENT_PARAM_PORT)); } }; } }

fac30ff commented 6 years ago

2nd option is: example application.properties: server.port = 9090 <- this is default if another properties do not find

application.unix.properties: server.port = ${COMPONENT_UNIX_PARAM_PORT} <- conf file with load env under unix

application.win.properties: server.port = ${COMPONENT_WIN_PARAM_PORT} <-conf file with load env under win

public class PropertiesUtils {

   ` public static final String SPRING_PROFILES_ACTIVE = "spring.profiles.active";`

   ` public static void initProperties() {`
    `    String activeProfile = System.getProperty(SPRING_PROFILES_ACTIVE);`
        `if (activeProfile == null) {`
            `activeProfile = "dev";`
        `}`
        `PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer`
               ` = new PropertySourcesPlaceholderConfigurer();`
       ` Resource[] resources = new ClassPathResource[]`
                `{new ClassPathResource("application.properties"),`
                   `     new ClassPathResource("application-" + activeProfile + ".properties")};`
        `propertySourcesPlaceholderConfigurer.setLocations(resources);`

    `}`
`} `
fac30ff commented 6 years ago

so we need only 1 additional environment variable spring.profiles.active with value one of default, win, unix etc.

VladyslavKurmaz commented 6 years ago

Let's use annotations with default values, for now https://stackoverflow.com/questions/44803211/read-environment-variable-in-springboot and we should try don't apply any os specific parameters, for example port parameter should work the same way on win/mac/linux

VladyslavKurmaz commented 6 years ago

Plus, there is even more simple solution which allows to read env variables:

import java.util.Optional;

host = Optional.ofNullable(System.getenv("API_HOSTNAME"));