版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012727852/article/details/78775797
一:开发前准备:
1、搭建ZK
启动命令:zkServer.sh start
2、搭建Kafka或rabbitmq
kafka启动命令:bin/kafka-server-start.sh config/server.properties &
3、搭建git,这里直接用了github
二:Service端:
1、pom文件里加入:
<parent>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-parent
</artifactId>
<version>
1.5.9.RELEASE
</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-dependencies
</artifactId>
<version>
Edgware.RELEASE
</version>
<type>
pom
</type>
<scope>
import
</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-config-server
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-config-monitor
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-starter-bus-kafka
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-actuator
</artifactId>
</dependency>
</dependencies>
2、创建application.properties
#git地址可以换成svn
#在配置的文件名字下查找
spring.cloud.config.server.git.searchPaths
=
wmbusapi
#kafka地址,可以好用rabbitmq代替
spring.cloud.stream.kafka.binder.brokers
=
192.168.54.152:9092
#kafka需要的zk地址
spring.cloud.stream.kafka.binder.zk-nodes
=
192.168.54.152:2181
3、ConfigServerApplication.java
添加
EnableConfigServer 表示这是一个springcloudconfigserver
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
/**
* Created by IntelliJ IDEA.
* User: GongQi
* Date: 2017/12/11
*/
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
三:Client端
1、在pom文件中加入:
<parent>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-parent
</artifactId>
<version>
1.5.9.RELEASE
</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-dependencies
</artifactId>
<version>
Edgware.RELEASE
</version>
<type>
pom
</type>
<scope>
import
</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-starter-config
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-starter-bus-kafka
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-web
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-actuator
</artifactId>
</dependency>
</dependencies>
2、ConfigClientApplication
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by IntelliJ IDEA.
* User: GongQi
* Date: 2017/12/11
*/
@SpringBootApplication
@RestController
@RefreshScope
public class ConfigClientApplication {
@Value("${cache.couchbase.servers}")
private String name;
@RequestMapping("/")
public String home() {
System.out.println(name);
return name;
}
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3、创建bootstrap.properties
#远程配置中心地址
#配置中心中对应的application
spring.cloud.config.name
=
EtradeConfig
#当前环境,可通过启动脚本设置参数来控制
spring.cloud.config.profile
=
dev
spring.cloud.stream.kafka.binder.brokers
=
192.168.54.152:9092
spring.cloud.stream.kafka.binder.zk-nodes
=
192.168.54.152:2181
server.port
=
8888
#不校验验证,不然401
management.security.enabled
=
false
四:启动
分别启动对应的main方法即可
五:说明
1、客户端启动后通过浏览器可以访问,看到返回的地址是配置文件里的。
2、可以通过post请求:(通过postman发起)
http://localhost:8888/env 参数:cache.couchbase.servers:192.168.1.1可以修改对客户端节点的参数配置
3、修改参数后不会立即生效,可以通过pos请求:
http://localhost:8888/refresh ,来刷新对应客户端的配置。再访问客户端,配置文件就动态修改了。
注意:这里是访问客户端来刷配置,如果需要对所有节点生效可以访问配置中心服务端:
http://localhost:8080/bus/env 来修改所有节点配置、用
http:
//localhost:8080/bus/refresh 来更新所有节点