Springboot 整合 SpringCloud组件-Config 配置中心 ConfigClient (七)

青旅半醒 2021-12-15 05:15 444阅读 0赞

在上一篇https://blog.csdn.net/qq_35387940/article/details/94616604 我们已经将配置中心 ConfigServer成功完成整合,也和github打通,那么这篇我们将实现ConfigClient ,通过 ConfigServer去读取github里的相关值。

大致流程如下图:

Azure (2).png

不多说,我们开始整合,创建一个springboot项目,起名config-client:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM1Mzg3OTQw_size_16_color_FFFFFF_t_70

pom.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.6.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.cloud</groupId>
  12. <artifactId>config-client</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>config-client</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.cloud</groupId>
  23. <artifactId>spring-cloud-starter-config</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-web</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.cloud</groupId>
  31. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-test</artifactId>
  36. <scope>test</scope>
  37. </dependency>
  38. </dependencies>
  39. <dependencyManagement>
  40. <dependencies>
  41. <dependency>
  42. <groupId>org.springframework.cloud</groupId>
  43. <artifactId>spring-cloud-dependencies</artifactId>
  44. <version>${spring-cloud.version}</version>
  45. <type>pom</type>
  46. <scope>import</scope>
  47. </dependency>
  48. </dependencies>
  49. </dependencyManagement>
  50. <build>
  51. <plugins>
  52. <plugin>
  53. <groupId>org.springframework.boot</groupId>
  54. <artifactId>spring-boot-maven-plugin</artifactId>
  55. </plugin>
  56. </plugins>
  57. </build>
  58. </project>

接着我们写一个controller, 写一个接口来测试读取GitHub的配置文件值,ConfigClientController.java:

  1. import org.springframework.beans.factory.annotation.Value;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. @RestController
  5. public class ConfigClientController {
  6. @Value("${testValue}")
  7. String testValue;
  8. @RequestMapping(value = "/getTestValue")
  9. public String getTestValue(){
  10. return "获取到的配置文件值为:"+testValue;
  11. }
  12. }

然后是配置文件,这里我们需要创建一个bootstrap.yml (或者bootstrap.properties):

  1. eureka:
  2. instance:
  3. #以IP地址注册到服务中心,相互注册使用IP地址
  4. preferIpAddress: true
  5. instance-id: ${spring.cloud.client.ip-address}:${server.port}
  6. client:
  7. serviceUrl:
  8. defaultZone: http://localhost:8761/eureka/
  9. spring:
  10. application:
  11. #和git里的文件名(application)对应
  12. name: config-client
  13. cloud:
  14. config:
  15. #分支
  16. label: master
  17. #环境值
  18. profile: dev
  19. #config-server的地址
  20. uri: http://localhost:8888/
  21. server:
  22. port: 8881

在启动类加上注解,也将这个服务注册到Eureka注册中心去:

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  4. @EnableEurekaClient
  5. @SpringBootApplication
  6. public class ConfigClientApplication {
  7. public static void main(String[] args) {
  8. SpringApplication.run(ConfigClientApplication.class, args);
  9. }
  10. }

OK,到这里已经准备完毕,将config-client服务也跑起来,

访问http://localhost:8881/getTestValue :

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM1Mzg3OTQw_size_16_color_FFFFFF_t_70 1

OK,git上面的配置值成功获取。

PS: 补充2个错误,项目无法正常运行

一、如果你在运行项目出现以下这类错误:

  1. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configClientController': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'testValue' in value "${testValue}"

不用想,你的config-client 已经在项目跑起来尝试地去访问config-server,然后尝试读取git里面的值,发现找不到才报的错。

那么怎么解决, 1.请对比配置文件里面有没有这个配置项的key 2.请对照好git里面配置文件的名字 是不是跟我们这个项目里的服务名一致. 3.config-server服务有没有正常运行,端口是不是对应起来了 4.这个服务用的配置文件是bootstrap.yml,优先级大于application.yml,有没有配置信息搞乱了。

二、如果你在运行项目出现以下这类错误:

  1. 2019-07-04 15:36:46.738 INFO 16380 --- [ Thread-15] com.netflix.discovery.DiscoveryClient : Shutting down DiscoveryClient ...
  2. 2019-07-04 15:36:46.752 INFO 16380 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_CLIENT-TEST/192.168.2.246:8881 - registration status: 204
  3. 2019-07-04 15:36:46.753 INFO 16380 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_CLIENT-TEST/192.168.2.246:8881: registering service...
  4. 2019-07-04 15:36:46.756 INFO 16380 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_CLIENT-TEST/192.168.2.246:8881 - registration status: 204
  5. 2019-07-04 15:36:46.756 INFO 16380 --- [ Thread-15] com.netflix.discovery.DiscoveryClient : Unregistering ...
  6. 2019-07-04 15:36:46.761 INFO 16380 --- [ Thread-15] com.netflix.discovery.DiscoveryClient : DiscoveryClient_CLIENT-TEST/192.168.2.246:8881 - deregister status: 200
  7. 2019-07-04 15:36:46.769 INFO 16380 --- [ Thread-15] com.netflix.discovery.DiscoveryClient : Completed shut down of DiscoveryClient

不用想,这多半是pom.xml里面忘记导入spring-boot-starter-web依赖了,添加依赖即可。

发表评论

表情:
评论列表 (有 0 条评论,444人围观)

还没有评论,来说两句吧...

相关阅读