Przeglądaj źródła

修改sonar严重告警

weihf 5 lat temu
rodzic
commit
30c82e24db

+ 33 - 31
ipu-nacos-boot2/src/main/java/com/ai/ipu/server/config/NacosRegisterConfiguration.java

@ -1,31 +1,33 @@
1
package com.ai.ipu.server.config;
2
3
import com.alibaba.nacos.api.annotation.NacosInjected;
4
import com.alibaba.nacos.api.naming.NamingService;
5
6
import org.springframework.beans.factory.annotation.Value;
7
import org.springframework.context.annotation.Configuration;
8
9
import javax.annotation.PostConstruct;
10
11
@Configuration
12
public class NacosRegisterConfiguration {
13
14
	@Value("${server.port}")
15
	private int serverPort;
16
17
	@Value("${spring.application.name}")
18
	private String applicationName;
19
20
	@NacosInjected
21
	private NamingService namingService;
22
23
	@PostConstruct
24
	public void registerInstance() {
25
		try {
26
			namingService.registerInstance(applicationName, "10.1.40.132", serverPort, "DEFAULT");
27
		} catch (Exception e) {
28
			e.printStackTrace();
29
		}
30
	}
31
}
1
package com.ai.ipu.server.config;
2

3
import com.alibaba.nacos.api.annotation.NacosInjected;
4
import com.alibaba.nacos.api.naming.NamingService;
5

6
import org.slf4j.Logger;
7
import org.slf4j.LoggerFactory;
8
import org.springframework.beans.factory.annotation.Value;
9
import org.springframework.context.annotation.Configuration;
10

11
import javax.annotation.PostConstruct;
12

13
@Configuration
14
public class NacosRegisterConfiguration {
15
	private static final Logger logger = LoggerFactory.getLogger(NacosRegisterConfiguration.class);
16
	@Value("${server.port}")
17
	private int serverPort;
18

19
	@Value("${spring.application.name}")
20
	private String applicationName;
21

22
	@NacosInjected
23
	private NamingService namingService;
24

25
	@PostConstruct
26
	public void registerInstance() {
27
		try {
28
			namingService.registerInstance(applicationName, "10.1.40.128", serverPort, "DEFAULT");
29
		} catch (Exception e) {
30
			logger.error(e.getMessage());
31
		}
32
	}
33
}

+ 40 - 38
ipu-nacos-cloud/src/main/java/com/ai/ipu/server/control/NacosConsumerController.java

@ -1,38 +1,40 @@
1
package com.ai.ipu.server.control;
2
3
import org.springframework.beans.factory.annotation.Autowired;
4
import org.springframework.beans.factory.annotation.Value;
5
import org.springframework.cloud.context.config.annotation.RefreshScope;
6
import org.springframework.stereotype.Controller;
7
import org.springframework.web.bind.annotation.GetMapping;
8
import org.springframework.web.bind.annotation.RequestMapping;
9
import org.springframework.web.bind.annotation.ResponseBody;
10
import org.springframework.web.client.RestTemplate;
11
import com.alibaba.nacos.api.exception.NacosException;
12
@Controller
13
@RefreshScope
14
@RequestMapping("/nacos/discovery/consumer")
15
public class NacosConsumerController {
16
17
	 private final RestTemplate restTemplate;
18
19
     @Autowired
20
     public NacosConsumerController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}
21
     
22
	@Value("${spring.application.name}")
23
    private String appName;
24
    @Value(value = "${service002Url:defaultValue}")
25
    private String service002Url;
26
    @Value(value = "${service002Param:defaultValue}")
27
    private String service002Param;
28
    @ResponseBody
29
    @GetMapping(value = "/service002")
30
    public String service002() throws NacosException {	
31
    	try {
32
			return restTemplate.getForObject("http://ipu-nacos-cloud" + service002Url + "?message=" + appName, String.class);
33
		} catch (Exception e) {
34
			e.printStackTrace();
35
		}
36
		return null;
37
    }
38
}
1
package com.ai.ipu.server.control;
2

3
import org.slf4j.Logger;
4
import org.slf4j.LoggerFactory;
5
import org.springframework.beans.factory.annotation.Autowired;
6
import org.springframework.beans.factory.annotation.Value;
7
import org.springframework.cloud.context.config.annotation.RefreshScope;
8
import org.springframework.stereotype.Controller;
9
import org.springframework.web.bind.annotation.GetMapping;
10
import org.springframework.web.bind.annotation.RequestMapping;
11
import org.springframework.web.bind.annotation.ResponseBody;
12
import org.springframework.web.client.RestTemplate;
13
import com.alibaba.nacos.api.exception.NacosException;
14
@Controller
15
@RefreshScope
16
@RequestMapping("/nacos/discovery/consumer")
17
public class NacosConsumerController {
18
	private static final Logger logger = LoggerFactory.getLogger(NacosConsumerController.class);
19
	private final RestTemplate restTemplate;
20

21
    @Autowired
22
    public NacosConsumerController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}
23
     
24
	@Value("${spring.application.name}")
25
    private String appName;
26
    @Value(value = "${service002Url:defaultValue}")
27
    private String service002Url;
28
    @Value(value = "${service002Param:defaultValue}")
29
    private String service002Param;
30
    @ResponseBody
31
    @GetMapping(value = "/service002")
32
    public String service002() throws NacosException {	
33
    	try {
34
			return restTemplate.getForObject("http://ipu-nacos-cloud" + service002Url + "?message=" + appName, String.class);
35
		} catch (Exception e) {
36
			logger.error(e.getMessage());
37
		}
38
		return null;
39
    }
40
}

+ 4 - 5
ipu-nacos-cloud/src/main/java/com/ai/ipu/server/control/RestConsumerController.java

@ -1,11 +1,10 @@
1 1
package com.ai.ipu.server.control;
2 2

3
import org.slf4j.Logger;
4
import org.slf4j.LoggerFactory;
3 5
import org.springframework.beans.factory.annotation.Autowired;
4 6
import org.springframework.beans.factory.annotation.Value;
5
import org.springframework.cloud.client.ServiceInstance;
6
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
7 7
import org.springframework.cloud.context.config.annotation.RefreshScope;
8
import org.springframework.http.ResponseEntity;
9 8
import org.springframework.web.bind.annotation.GetMapping;
10 9
import org.springframework.web.bind.annotation.RequestMapping;
11 10
import org.springframework.web.bind.annotation.ResponseBody;
@ -22,7 +21,7 @@ import com.alibaba.nacos.api.exception.NacosException;
22 21
@RefreshScope
23 22
@RequestMapping("/nacos/discovery/restConsumer")
24 23
public class RestConsumerController {
25

24
	private static final Logger logger = LoggerFactory.getLogger(RestConsumerController.class);
26 25

27 26
    @Autowired
28 27
    private RestTemplate restTemplate;
@ -39,7 +38,7 @@ public class RestConsumerController {
39 38
		try {
40 39
			return restTemplate.getForObject("http://ipu-nacos-cloud" + config.getService002Url() + "?message=" + appName, String.class);
41 40
		} catch (Exception e) {
42
			e.printStackTrace();
41
			logger.error(e.getMessage());
43 42
		}
44 43
		
45 44
        return null;

+ 4 - 2
ipu-rest-scaffold/src/main/java/com/ai/ipu/server/config/NacosRegisterConfiguration.java

@ -5,6 +5,8 @@ import com.alibaba.nacos.api.annotation.NacosInjected;
5 5
import com.alibaba.nacos.api.naming.NamingService;
6 6
import com.alibaba.nacos.api.naming.pojo.Instance;
7 7

8
import org.slf4j.Logger;
9
import org.slf4j.LoggerFactory;
8 10
import org.springframework.beans.factory.annotation.Value;
9 11
import org.springframework.context.annotation.Configuration;
10 12

@ -12,7 +14,7 @@ import javax.annotation.PostConstruct;
12 14

13 15
@Configuration
14 16
public class NacosRegisterConfiguration {
15

17
	private static final Logger log = LoggerFactory.getLogger(NacosRegisterConfiguration.class);
16 18
	@Value("${server.port}")
17 19
	private int serverPort;
18 20

@ -42,7 +44,7 @@ public class NacosRegisterConfiguration {
42 44
			//IpUtil.getHostAddress()在有多块网卡时,取不是.1结尾并且ip地址排序最小的做为当前ip
43 45
			namingService.registerInstance(applicationName, IpUtil.getHostAddress(), serverPort, "DEFAULT");
44 46
		} catch (Exception e) {
45
			e.printStackTrace();
47
			log.error(e.getMessage());
46 48
		}
47 49
	}
48 50
}

+ 2 - 1
ipu-rest-scaffold/src/main/java/com/ai/ipu/server/control/NacosConfigController.java

@ -20,7 +20,8 @@ import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySources;
20 20
@RequestMapping("/nacos/config")
21 21
@NacosPropertySources({
22 22
@NacosPropertySource(dataId = "mysql", autoRefreshed = true),
23
@NacosPropertySource(dataId = "myService", autoRefreshed = true)})
23
@NacosPropertySource(dataId = "myService", autoRefreshed = true),
24
@NacosPropertySource(dataId = "myxml", autoRefreshed = true)})
24 25
public class NacosConfigController {
25 26
	private static final Logger logger = LoggerFactory.getLogger(NacosConfigController.class);
26 27