spring cloud开发Greenwich版:配置客户端(十一)

上文我们已经构建了配置服务器ralab-configserver,那么我们的微服务怎样从该配置服务器获取相关配置信息呢?这里我们改造原来的ralab-client微服务

ralab-client微服务

  • Add the Maven spring-cloud-starter-config denpendency
  • Test example.property value in NlpResource
  • Add configserver info to bootstrap.yml file

其pom.xml文件添加spring-cloud-starter-config依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ralab</groupId>
<artifactId>ralab-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ralab-client</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--添加依赖,获取配置服务器的相关配置信息-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

NlpResource文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.ralab.ralabclient.resources;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/nlp")
@RefreshScope
public class NlpResource {

@Autowired
private RestTemplate restTemplate;

@Value("${example.property}")
private String exampleProperty;

@RequestMapping("/config")
public String home() {
return "exampleProperty:" + exampleProperty;
}

@GetMapping("/service")
@HystrixCommand(fallbackMethod = "getFallbackIndex")
public String index()
{
//String result = restTemplate.getForObject("http://localhost:8090/nlp/api", String.class);
String result = restTemplate.getForObject("http://nlp-service/nlp/api", String.class);
return result;
}

private String getFallbackIndex()
{
//String result = restTemplate.getForObject("http://localhost:8090/nlp/api", String.class);

return "不好意思,没有获取到服务端信息";
}
}

bootstrap.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/

server:
port: 8091
management:
endpoints:
web:
exposure:
include: hystrix.stream

spring:
application:
name: nlp-client
cloud:
config:
uri: http://localhost:8888
name: licensingservice
profile: default
label: master

运行RalabClientApplication,打开http://localhost:8091/nlp/config

补充:如果配置服务器的配置信息作了修改,这时我们的微服务配置客户端可以主动拉取(pull)
在获取配置属性的类上加注解,@RefreshScope,然后发送post请求http://127.0.0.1:8091/actuator/refresh

坚持原创技术分享,您的支持是我前进的动力!