配置用心
配置中心为服务提供一个不同配置管理的组件。 采用git来存储配置,然后通过服务里面配置git地址,用户名,密码来获取远程git仓库地址
的配置。 结合spring boot的端点可以进行配置的刷新。
cloud config server
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
spring.application.name=config-server
server.port=8888
spring.cloud.config.server.git.uri=https://gitee.com/ninuxGithub/spring-cloud-config/
spring.cloud.config.server.git.searchPaths=config
spring.cloud.config.label=master
spring.cloud.config.server.git.username=username #需要修改
spring.cloud.config.server.git.password=password #需要修改
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
@EnableEurekaClient
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
cloud config client
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri= http://localhost:8888/
server.port=8881
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
spring.rabbitmq.host=10.1.51.96
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
spring.cloud.bus.enabled=true
spring.cloud.bus.trace.enabled=true
management.endpoints.web.exposure.include=bus-refresh
package com.example.demo;
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.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@RestController
@RefreshScope
public class ConfigClientApplication {
/**
* 访问地址 http://localhost:8762/actuator/hystrix.stream
*
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
@Value("${foo}")
String foo;
@Value("${democonfigclient.message}")
String message;
/**
* spring cloud config 测试的demo
* foo message 变量从远程仓库加载
* 直接访问properties:
* http://localhost:8888/config-client-dev.properties
*
* 修改远程仓库的配置,然后请求
* post访问:http://localhost:8881/actuator/bus-refresh?destination=config-client
* 然后在访问http://localhost:8881/hi 会发现结果也发送了变化 是远程仓库的修改后的配置
*
* 作用: 修改上线的服务的配置
*
* @return
*/
@RequestMapping(value = "/hi")
public Map<String,String> hi() {
Map<String,String> map = new HashMap<>();
map.put("foo",foo);
map.put("message",message);
return map;
}
}