在spring-cloud-config-server和spring-cloud-config-client中配置eureka

在configserver中配置eureka可以使用eureka来查找所需要的配置,不用自己每一个单独配置。

首先是configserver中的pom.xml中使用的jar包。使用的springboot版本为1.5.10.RELEASE

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.10.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-config-server</artifactId>
		<version>1.4.1.RELEASE</version>
	</dependency>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		<version>1.4.1.RELEASE</version>
	</dependency>

然后是application.yml中的配置内容:

spring:
  application:
    name: spring-cloud-config-server-eureka
  cloud:
    config:
      server:
        git: #设置git仓库的位置,上面有配置文件。
          uri: https://github.com/XXXXXXX/spring-cloud-config-server-test.git 

server:
  port: 8058
  
#设置eurekaclient的注册地址。

eureka:
  client: 
    service-url:
      defaultZone: http://localhost:80/eureka/
  instance:
     prefer-ip-address: true

然后在启动类上面写上@EnableConfigServer @EnableDiscoveryClient,前面一个是开启configserver后面一个是eureka的客户端。

启动后在eurekaserver的界面中能看到,启动成功。

然后配置client端jar中的需要换为client:

		<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-config</artifactId>
		<version>1.4.1.RELEASE</version>
	</dependency>
	

由于configclient不是一个独立的jar在搜索框中能够看到,被集成在了spring-cloud-starter-config

然后在bootstrap.yml中的配置内容。

spring: 
  cloud: 
    config:
      discovery:
        enabled: true
        service-id: spring-cloud-config-server-eureka
#当使用了eureka后就不需要config来配置,会通过application.name的值在configserver中查找相应的配置文件。  
#    config: 
#      uri: http://localhost:8052
#      profile: production
#      label: master  #当configserver的后端存储是Git时,默认是master
  application:
    name: foobar #会在git上找和name相同的配置文件,如果找不到,就是默认的配置文件。也将名字改为foobar-dev就可以命中相应的配置文件,这样感觉不是很好。
    
    
eureka:
  client: 
    service-url:
      defaultZone: http://localhost:80/eureka/
  instance:
     prefer-ip-address: true

server.port被我单独配置在了application.yml中

server:
  port: 8059

为了测试configclient的配置成功,还简单的写了一个controller。

@RestController
public class ConfigClientController {

	@Value("${profile}")
	private String profile;
	
	@RequestMapping("/profile")
	public String getProfile() {
		return this.profile;
		
	}
	
}

用于获取配置文件中的profile属性的值。

启动时报错,启动失败,说明配置失败或configserver未启动导致启动失败。原因是找不到配置的该属性,如下:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'profile' in value "${profile}"

在启动configserver和configclient的时候需要注意,要先启动configserver然后启动configclient,不然也会启动失败,因为没有上面配置的profile属性而失败。

 

如果启动成功在日志中会有如下内容:

上面信息说明了本项目的名称和从configserver中获取到的内容。

然后再访问controller中的/profile。

说明配置成功了。

最后是git上面的内容:

application.yml中的内容:

foobar-dev.yml中的内容:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值