DavinciServerApplication.java 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2019 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. * >>
  17. *
  18. */
  19. package edp;
  20. import com.alibaba.nacos.api.annotation.NacosInjected;
  21. import com.alibaba.nacos.api.config.annotation.NacosValue;
  22. import com.alibaba.nacos.api.exception.NacosException;
  23. import com.alibaba.nacos.api.naming.NamingService;
  24. import com.alibaba.nacos.client.naming.utils.CollectionUtils;
  25. import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
  26. import com.alibaba.nacos.spring.context.annotation.discovery.EnableNacosDiscovery;
  27. import org.springframework.beans.factory.annotation.Value;
  28. import org.springframework.boot.SpringApplication;
  29. import org.springframework.boot.autoconfigure.SpringBootApplication;
  30. import org.springframework.context.annotation.Bean;
  31. import org.springframework.scheduling.annotation.EnableScheduling;
  32. import javax.annotation.PostConstruct;
  33. import java.net.InetAddress;
  34. @SpringBootApplication(exclude = {
  35. org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class
  36. })
  37. @NacosPropertySource(dataId = "taihu-analysis", autoRefreshed = true)
  38. @EnableScheduling
  39. @EnableNacosDiscovery
  40. public class DavinciServerApplication {
  41. @NacosInjected
  42. private NamingService namingService;
  43. @Value("${server.port}")
  44. private int serverPort;
  45. @Value("${server.address}")
  46. private String serverAddress;
  47. // @NacosValue(value = "${person.name}", autoRefreshed = true)
  48. // private String name;
  49. @Value("${spring.application.name}")
  50. private String applicationName;
  51. //curl -X PUT 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=xxx&ip=127.0.0.1&port=8080'
  52. @PostConstruct
  53. public void registerInstance() throws NacosException
  54. {
  55. InetAddress ia=null;
  56. try {
  57. ia= InetAddress.getLocalHost();
  58. String localname=ia.getHostName();
  59. String localip=ia.getHostAddress();
  60. if(CollectionUtils.isEmpty(namingService.getAllInstances(applicationName))){
  61. namingService.registerInstance(applicationName,localip,serverPort);
  62. }
  63. } catch (Exception e) {
  64. // TODO Auto-generated catch block
  65. e.printStackTrace();
  66. }
  67. }
  68. public static void main(String[] args) {
  69. System.setProperty("mail.mime.splitlongparameters", "false");
  70. SpringApplication.run(DavinciServerApplication.class, args);
  71. }
  72. @Value("${jwtToken.secret:Pa@ss@Word}")
  73. private String tokenSecret;
  74. @Bean
  75. public String TOKEN_SECRET() {
  76. return tokenSecret;
  77. }
  78. }