spring boot的版本为1.5.8
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
step 1: eureka注册中心配置,修改POM文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
新建启动类
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
修改yml文件
spring:
application:
name: sato-eureka
server:
port: 7001
eureka:
instance:
#使用IP访问注册中心
prefer-ip-address: true
#在注册中心status的时候显示的格式,这里是 ip:端口
instance-id: ${spring.cloud.client.ipAddress}:${server.port}
hostname: ${spring.cloud.client.ipAddress}
client:
#是否向自己注册
register-with-eureka: false
fetch-registry: false
#集群配置,需要配置其他eureka注册中心的地址,不过一般都放到启动参数,而不放置到配置文件中
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
step2:配置中心服务端配置,修改POM文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
修改yml文件
spring:
application:
name: config-server
cloud:
config:
server:
#git地址配置
git:
uri: https://gitlab.xuebastudy.com/zhuwei/config-pro.git
username: ***
password: ****
bus:
enabled: true
trace:
enabled: true
#rabbit配置
rabbitmq:
host: ***
port: 5672
username: ****
password: ****
server:
port: 7100
#注册中心配置
eureka:
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ipAddress}:${server.port}
hostname: ${spring.cloud.client.ipAddress}
client:
service-url:
defaultZone: http://127.0.0.1:7001/eureka/
management:
security:
enabled: false
修改启动类
@SpringBootApplication
//启动为配置服务的服务端
@EnableConfigServer
@EnableEurekaClient
public class ConfigServerApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
step3:新建客户端测试,修改POM文件
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
新建bootstrap.yml配置
spring:
cloud:
config:
name: config-client
profile: dev
label: master
discovery:
enabled: true
service-id: config-server
#注册中心配置
eureka:
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ipAddress}:${server.port}
hostname: ${spring.cloud.client.ipAddress}
client:
service-url:
defaultZone: http://127.0.0.1:7001/eureka/
新建application.yml文件
spring:
application:
name: config-client
cloud:
bus:
trace:
enabled: true
enabled: true
#rabbit配置
rabbitmq:
host: ****
port: 5672
username: ****
password: *****
server:
port: 7101
management:
security:
enabled: false
这里需要注意加载配置的内容一定要放在bootstarp.yml文件里面,这个涉及到加载顺序的问题
新建启动类
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientTestApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(ConfigClientTestApplication.class, args);
}
}
新建测试Controller
@RestController
@RefreshScope
public class HelloController {
@Value("${info.profile:error}")
private String profile;
@GetMapping("/info")
public String hello() {
return profile;
}
}
返回 {"profile":"dev"}获取配置成功
修改dev 为dev update,提交到git,此时我们去获取到的配置内容是没有变化的。
使用POST请求调用刷新:http://localhost:7101/refresh
返回
[
"config.client.version",
"info.profile"
]
重新访问,发现获取到最新的值