Przeglądaj źródła

添加nacos服务消费代码。nacos通过springboot注册服务有bug:服务注册后权重为0,元数据为空,导致服务消费查不到此服务。因此后续服务注册在ipu-nacos-cloud里提供。

weihf 5 lat temu
rodzic
commit
3b1da40354

+ 22 - 17
ipu-rest-scaffold/pom.xml

@ -37,7 +37,7 @@
37 37
		<jdk>1.8</jdk>
38 38
		<junit>4.12</junit>
39 39
		<spring-session.version>1.3.2.RELEASE</spring-session.version>
40
		<nacos-config.version>0.1.1</nacos-config.version>
40
		<nacos.version>0.1.1</nacos.version>
41 41
	</properties>
42 42
43 43
	<dependencies>
@ -79,20 +79,25 @@
79 79
			<groupId>org.springframework.boot</groupId>
80 80
			<artifactId>spring-boot-starter-actuator</artifactId>
81 81
		</dependency>
82
		<!--devtools热部署-->
83
        <dependency>
84
            <groupId>org.springframework.boot</groupId>
85
            <artifactId>spring-boot-devtools</artifactId>
86
            <optional>true</optional>
87
            <scope>true</scope>
88
        </dependency>
89
        <!-- 引入nacos服务注册及配置中心 -->
90
        <dependency>
91
            <groupId>com.alibaba.boot</groupId>
92
            <artifactId>nacos-config-spring-boot-starter</artifactId>
93
            <version>${nacos-config.version}</version>
94
        </dependency>
95
82
		<!--devtools热部署  -->
83
		<dependency>
84
			<groupId>org.springframework.boot</groupId>
85
			<artifactId>spring-boot-devtools</artifactId>
86
			<optional>true</optional>
87
			<scope>runtime</scope>
88
		</dependency>
89
		<!-- 引入nacos服务配置中心 -->
90
		<dependency>
91
			<groupId>com.alibaba.boot</groupId>
92
			<artifactId>nacos-config-spring-boot-starter</artifactId>
93
			<version>${nacos.version}</version>
94
		</dependency>
95
		<!-- 引入nacos服务注册中心 -->
96
		<dependency>
97
			<groupId>com.alibaba.boot</groupId>
98
			<artifactId>nacos-discovery-spring-boot-starter</artifactId>
99
			<version>${nacos.version}</version>
100
		</dependency>
96 101
	</dependencies>
97 102
98 103
	<build>
@ -101,8 +106,8 @@
101 106
				<groupId>org.springframework.boot</groupId>
102 107
				<artifactId>spring-boot-maven-plugin</artifactId>
103 108
				<configuration>
104
                    <fork>true</fork>
105
                </configuration>
109
					<fork>true</fork>
110
				</configuration>
106 111
			</plugin>
107 112
		</plugins>
108 113
	</build>

+ 17 - 1
ipu-rest-scaffold/readme.md

@ -109,11 +109,27 @@ SQL被统一存放在classpath的sql路径下,如:sql.ipu.demo。
109 109
        ```
110 110
- 2.nacos配置中心的配置文件为:application.properties
111 111
        ```properties
112
        nacos.config.server-addr=192.168.1.101:8848
112
        nacos.config.server-addr=127.0.0.1:8848
113 113
        ```
114 114

115 115
- 3.nacos配置中心使用案例在NacosConfigController中。此案例只支持springboot,springcloud的示例不在当前工程。
116 116

117
- 4.nacos注册中心组件的引入
118
    - 以maven为例,请在pom.xml中引入nacos组件。
119
        ```xml
120
        <dependency>
121
            <groupId>com.alibaba.boot</groupId>
122
            <artifactId>nacos-discovery-spring-boot-starter</artifactId>
123
            <version>0.1.1</version>
124
        </dependency>
125
        ```
126
- 5.nacos注册中心的配置文件为:application.properties
127
        ```properties
128
        nacos.discovery.server-addr=127.0.0.1:8848
129
        ```
130

131
- 6.nacos注册中心使用案例为NacosConsumerControl.java(服务消费/调用)。nacos通过springboot注册服务有bug:服务注册后权重为0,元数据为空,导致服务消费查不到此服务。此案例只支持springboot,springcloud的示例在工程ipu-nacos_cloud中。查看服务注册后,注册中心保存的注册信息命令为curl -X GET '127.0.0.1:8848/nacos/v1/ns/instance?serviceName=ipu-rest-scaffold&ip=10.1.234.70&port=9999&cluster=DEFAULT'
132

117 133
###打包运行
118 134
java -Dipu.lic.path=ipu.lic -jar ipu-rest-scaffold.jar --server.port=8080
119 135


+ 5 - 5
ipu-rest-scaffold/src/main/java/com/ai/ipu/server/config/NacosListenerConfiguration.java

@ -30,7 +30,7 @@ public class NacosListenerConfiguration {
30 30
    public void init() throws NacosException {
31 31

32 32
        Listener listener = new AbstractListener() {
33
//            @Override
33
            @Override
34 34
            public void receiveConfigInfo(String configInfo) {
35 35
            }
36 36
        };
@ -46,7 +46,7 @@ public class NacosListenerConfiguration {
46 46
    @Bean
47 47
    public ApplicationListener<NacosConfigReceivedEvent> nacosConfigReceivedEventListener() {
48 48
        return new ApplicationListener<NacosConfigReceivedEvent>() {
49
//            @Override
49
            @Override
50 50
            public void onApplicationEvent(NacosConfigReceivedEvent event) {
51 51
                logger.info("Listening on NacosConfigReceivedEvent -  dataId : {} , groupId : {} , " + "content : {} , "
52 52
                        + "source : {}", event.getDataId(), event.getGroupId(), event.getContent(), event.getSource());
@ -57,7 +57,7 @@ public class NacosListenerConfiguration {
57 57
    @Bean
58 58
    public ApplicationListener<NacosConfigRemovedEvent> nacosConfigRemovedEventListener() {
59 59
        return new ApplicationListener<NacosConfigRemovedEvent>() {
60
//            @Override
60
            @Override
61 61
            public void onApplicationEvent(NacosConfigRemovedEvent event) {
62 62
                logger.info("Listening on NacosConfigRemovedEvent -  dataId : {} , groupId : {} , " + "removed : {} , "
63 63
                        + "source : {}", event.getDataId(), event.getGroupId(), event.isRemoved(), event.getSource());
@ -68,7 +68,7 @@ public class NacosListenerConfiguration {
68 68
    @Bean
69 69
    public ApplicationListener<NacosConfigListenerRegisteredEvent> nacosConfigListenerRegisteredEventListener() {
70 70
        return new ApplicationListener<NacosConfigListenerRegisteredEvent>() {
71
//            @Override
71
            @Override
72 72
            public void onApplicationEvent(NacosConfigListenerRegisteredEvent event) {
73 73
                logger.info("Listening on NacosConfigListenerRegisteredEvent -  dataId : {} , groupId : {} , " + "registered : {} , "
74 74
                        + "source : {}", event.getDataId(), event.getGroupId(), event.isRegistered(), event.getSource());
@ -79,7 +79,7 @@ public class NacosListenerConfiguration {
79 79
    @Bean
80 80
    public ApplicationListener<NacosConfigPublishedEvent> nacosConfigPublishedEvent() {
81 81
        return new ApplicationListener<NacosConfigPublishedEvent>() {
82
//            @Override
82
            @Override
83 83
            public void onApplicationEvent(NacosConfigPublishedEvent event) {
84 84
                logger.info("Listening on NacosConfigPublishedEvent -  dataId : {} , groupId : {} , " + "published : {} , "
85 85
                        + "source : {}", event.getDataId(), event.getGroupId(), event.isPublished(), event.getSource());

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

@ -49,7 +49,7 @@ public class NacosConfigController {
49 49
	            return false;
50 50
	        }
51 51
	 }
52
	 /* 用于测试热加载 */
52
	 /* 用于测试热加载 
53 53
	 @ResponseBody
54 54
	 @RequestMapping("/removeSql")
55 55
	 public boolean removeSql(JMap params) throws Exception {
@ -61,5 +61,6 @@ public class NacosConfigController {
61 61
	            e.printStackTrace();
62 62
	            return false;
63 63
	        }
64
	 }
64
	 }*/
65
	 
65 66
}

+ 48 - 0
ipu-rest-scaffold/src/main/java/com/ai/ipu/server/control/NacosConsumerControl.java

@ -0,0 +1,48 @@
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.Value;
6
import org.springframework.http.ResponseEntity;
7
import org.springframework.stereotype.Controller;
8
import org.springframework.web.bind.annotation.GetMapping;
9
import org.springframework.web.bind.annotation.RequestMapping;
10
import org.springframework.web.bind.annotation.ResponseBody;
11
import org.springframework.web.client.RestTemplate;
12

13
import com.alibaba.nacos.api.annotation.NacosInjected;
14
import com.alibaba.nacos.api.exception.NacosException;
15
import com.alibaba.nacos.api.naming.NamingService;
16
import com.alibaba.nacos.api.naming.pojo.Instance;
17
@Controller
18
@RequestMapping("/nacos/discovery/consumer")
19
public class NacosConsumerControl {
20
	private static final Logger logger = LoggerFactory.getLogger(NacosConsumerControl.class);
21
	
22
	@NacosInjected
23
    private NamingService namingService;
24
	private RestTemplate restTemplate = new RestTemplate();
25
	
26
	@Value("${spring.application.name}")
27
    private String appName;
28
	@ResponseBody
29
    @GetMapping(value = "/echo")
30
    public String echo() throws NacosException {
31
		try {
32
			if (namingService != null) {
33
				// 选择user_service服务的一个健康的实例(可配置负载均衡策略)
34
				Instance instance = namingService.selectOneHealthyInstance("ipu-nacos_cloud");
35
				// 拼接请求接口url并请求选取的实例
36
				String url = "http://" + instance.getIp() + ":" + instance.getPort() + "/provider/echo?message=" + appName;
37
				ResponseEntity<String> entity = restTemplate.getForEntity(url, String.class);
38
		        System.out.println(String.format("请求URL:%s,响应结果:%s", url, entity.getBody()));
39
				return entity.getBody();
40
			}
41
		} catch (Exception e) {
42
			logger.error("get instance from nacos error", e);
43
		}
44
        return null;
45
    }
46
	
47

48
}

+ 11 - 4
ipu-rest-scaffold/src/main/resources/dev/application.properties

@ -32,6 +32,16 @@ management.context-path=/MyActuator
32 32
management.port=12581
33 33
# 不允许远程管理连接(不允许外部调用保证安全)
34 34
management.address=127.0.0.1
35
#管理安全设置,不推荐设置为false,缺省为true
36
#false表示允许任何人访问管理节点
37
#true则只允许指定用户密码才能访问
38
management.security.enabled=false
39

40
###配置nacos配置中心
41
nacos.config.server-addr=10.1.40.130:8848
42

43
###配置nacos注册中心
44
nacos.discovery.server-addr=10.1.40.130:8848
35 45

36 46
#设置开启热部署
37 47
spring.devtools.restart.enabled=true
@ -39,7 +49,4 @@ spring.devtools.restart.enabled=true
39 49
spring.devtools.restart.additional-paths=src/main/java
40 50
spring.devtools.restart.exclude=WEB-INF/**
41 51
#页面热加载
42
spring.freemarker.cache=false 
43

44
###配置nacos
45
nacos.config.server-addr=10.1.234.70:8848
52
spring.freemarker.cache=false