步骤一:
配置文件都放到这里面去
https://github.com/bluewelkin/springcloudconfig
步骤二:
项目一的Eureka启动
以下为项目二 config-server项目
步骤三:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</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-web</artifactId>
</dependency>
package com.imooc.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/bluewelkin/springcloudconfig
username: bluewelkin
password: xxxxxx
basedir: /SpringCloud_Sell/config/basedir
search-paths: config
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8761/eureka/
#management:
# endpoints:
# web:
# expose: "*"
就是少写了这个目录,所以没有chse成功。search-paths: config
步骤四:如此访问项目
http://localhost:8080/order-b.yml
如果全部出来了就OK,如果是要给花括号{} 则是search-paths: config 这个地方没有配置
代码:https://gitee.com/dgx555/springcloud/tree/master/springcloud_sevencsdn
新建项目三或者用spring_cloud_duoshili那个里面的config_client
bootstrap.yml
spring:
application:
name: order-dev #配置文件拉取的yml前缀
cloud:
config:
#uri: http://localhost:8769
fail-fast: true
discovery:
enabled: true
serviceId: config-server2
label: master
profiles:
active: dev
然后启动选项路面调用即可打印请求出来。
package com.tx;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
@EnableEurekaClient
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
@Value("${env}")
String env;
@RequestMapping(value = "/env")
public String hi(){
return env;
}
}