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

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

大致流程如下图:

Azure (2).png

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

pom.xml:

<?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 http://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.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.cloud</groupId>
	<artifactId>config-client</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>config-client</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<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.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>

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

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigClientController {


    @Value("${testValue}")
    String testValue;
    @RequestMapping(value = "/getTestValue")
    public String getTestValue(){

        return "获取到的配置文件值为:"+testValue;
    }

}

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


eureka:
  instance:
#以IP地址注册到服务中心,相互注册使用IP地址
    preferIpAddress: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
#和git里的文件名(application)对应
    name: config-client
  cloud:
    config:
#分支
      label: master
#环境值
      profile: dev
#config-server的地址
      uri: http://localhost:8888/
server:
  port: 8881

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

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient
@SpringBootApplication
public class ConfigClientApplication {

	public static void main(String[] args) {
		SpringApplication.run(ConfigClientApplication.class, args);
	}

}

 

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

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

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

 

 

 

 

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

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

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,有没有配置信息搞乱了。

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

2019-07-04 15:36:46.738  INFO 16380 --- [      Thread-15] com.netflix.discovery.DiscoveryClient    : Shutting down DiscoveryClient ...
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
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...
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
2019-07-04 15:36:46.756  INFO 16380 --- [      Thread-15] com.netflix.discovery.DiscoveryClient    : Unregistering ...
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
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
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小目标青年

对你有帮助的话,谢谢你的打赏。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值