Config配置中心
config配置中心,也是比较好用的一块,简单说一下它的作用吧。
我们在开发时会经常改动配置文件,而每一次改动都需要重新启动项目,项目小的话还好,如果项目比较大,每次启动一个项目都需要好久,这是非常浪费时间的,因此我们引入了Config,使得服务可以到git自动拉取配置,不需要我们再去重启服务,后面我们还会讲到结合Spring Cloud Bus +rabbit MQ实现这个功能。
一)创建项目
创建模块主要分为两部分server模块的创建和client模块的创建。
A、Server模块的创建
1、创建一个新的模块,和前面几讲的一样,打开上一篇创建的项目microservice选中,并单击右键New→Module,选择Spring Initializr默认下一步;
2、进入该页面填Group和Artifact,Group要写对和上次的要一样,Artifact 我命名为config-bus-server,config指的是该模块为配置模块;bus是下一讲会用到的一个模块,它和config是连在一起使用的;server指的是这是一个server模块,后面还有client模块。
如图1所示:
3、点击Next,选择相关依赖如下图2所示:
4、继续Next→Finish,等待下载依赖。完成后,首先进行启动类ConfigBusServerApplication的配置,如下图3所示:
代码如下所示:
@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class ConfigBusServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigBusServerApplication.class, args);
}
}
5、接下来配置application.properties文件。如图4所示:
代码如下所示:
spring.application.name=config-bus-server
eureka.client.service-url.defaultZone= http://localhost:8081/eureka/
server.port=8082
#git地址配置
spring.cloud.config.server.git.uri=https://github.com/gjen1996/microservice
spring.cloud.config.server.git.search-paths=config-bus-client/src/main/resources/
spring.cloud.config.label=master
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
a)spring.cloud.config.server.git.uri=https://github.com/gjen1996/microservice:这个是你项目git的地址,没有的话自己可以搭建或者去github注册一个,很多人纠结https://github.com/gjen1996/microservice还是https://github.com/gjen1996/microservice.git,本人亲测两个都是可以的 b)spring.cloud.config.server.git.search-paths=config-bus-client/src/main/resources/:这个是你的配置文件的地址这个地址如下图5所示,你可以在github上找到。
c)spring.cloud.config.label=master:这个是你git的分支,一般默认选择master
d)spring.cloud.config.server.git.username=:这个是你git的账号
e)spring.cloud.config.server.git.password=:这个是你git的密码
到此server的创建就完成了。
B、Client模块的创建
1、创建一个新的模块,和前面几讲的一样,打开上一篇创建的项目microservice选中,并单击右键New→Module,选择Spring Initializr默认下一步;
2、进入该页面填Group和Artifact,Group要写对和上次的要一样,Artifact 我命名为config-bus-client,config指的是该模块为配置模块;bus是下一讲会用到的一个模块,它和config是连在一起使用的;server指的是这是一个client模块,和前面的模块区分,如图6所示。
3、点击Next,选择相关依赖如下图7所示:
4、继续Next→Finish,等待下载依赖。完成后,首先进行启动类ConfigBusServerApplication的配置,如下图8所示:
代码如下所示:
@ComponentScan("com.glen")
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigBusClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigBusClientApplication.class, args);
}
}
5、接下来需要改写application.properties,在这个里面只写一句话后面帮助我们做测试,如下图9所示:
代码如下所示:
neo.hello= hello,world,test is mistake
6、然后在application.properties的同级目录下创建一个名为bootstrap.properties
如下图10所示:
代码如下所示:
eureka.client.serviceUrl.defaultZone=http://localhost:8081/eureka/
server.port=8083
spring.application.name=config-bus-client
spring.cloud.config.name=config-properties
spring.cloud.config.profile=dev
spring.cloud.config.uri=http://localhost:8082
spring.cloud.config.label=master
spring.cloud.config.discovery.service-id=config-bus-server
spring.cloud.config.discovery.enabled=true
关于代码的说明:
a)eureka.client.serviceUrl.defaultZone=http://localhost:8081/eureka/:eureka的注册中心地址
b)server.port=8083:服务的端口号
c)spring.application.name=config-bus-client:应用的名称
d)spring.cloud.config.name=config-properties:配置的名称,前面服务调用会用得着
e)spring.cloud.config.profile=dev:项目启动的环境,一般会分为,测试环境、正式环境等
f)spring.cloud.config.uri=http://localhost:8082:config的服务中心,也就是上面配置的config-bus-server的端口号
g)spring.cloud.config.label=master:git的分支为maste
h)spring.cloud.config.discovery.service-id=config-bus-server:config的服务中心,也就是上面配置的config-bus-server的服务名称
i)spring.cloud.config.discovery.enabled=true:该配置能否被发现,无关紧要,写true就好了。
7、选择com.glen.configbusclient单击右键New→Package,将该包命名为controller,并在该包下面创建ConfigBusClientController ,配置完的项目如图11所示:
代码如下所示:
@RestController
public class ConfigBusClientController {
@Value("${neo.hello}")
private String hello;
@RequestMapping("/hello")
public String from() {
return this.hello;
}
}
二)启动项目
我们依次启动EurekaServerApplication、ConfigBusServerApplication、ConfigBusClientApplication三个项目,在注册中心可以看到如图12内容,表示启动成功:
三)测试项目
接下来我们做测试:
1新打开一个地址栏,输入http://localhost:{config-bus-server的port}/{config-bus-client中spring.cloud.config.name的值}/{启动环境},例如 根据我的配置就是http://localhost:8082/config-properties/dev,然后回车,我们会发现出现如图13所示的内容:
当然每个现实的结构可能不一样,但是我圈红框的内容必须要有,才证明成功了,这个内容就是我们在config-bus-client的application.properties中neo.hello的值。此时我们打开控制台,可以发现如图14所示的内容:
这表示config-bus-server已经到远端去拉取了最新的项目存在了本地。
2、我们新建一个新的地址栏在地址栏输入http://localhost:{config-bus-client的端口}/{ConfigBusClientController中@RequestMapping的值},以我的为例就是http://localhost:8083/hello,回车我们会看到如图15所示的内容:
3、此时我们修改config-bus-client的application.properties中neo.hello的值并提交git,比如我们改成:
neo.hello= hello,world,this is updatetest
我们再次刷新http://localhost:8082/config-properties/dev,会发现圈红的地方已经改变,但并未进行重启服务,如图16所示
同时,我们在ConfigBusServerApplication的控制台我们也可以看到图17的内容,说明确实到远端进行的拉取。
然后我们刷新http://localhost:8083/hello,如图18所示,发现client并未改变
那如何解决这个问题哪?我们下一篇进行解答
SpringCloud从零构建(一)——Eureka注册中心
SpringCloud从零构建(二)——创建服务端Server
SpringCloud从零构建(三)——创建消费者Customer
SpringCloud从零构建(四)——Feign实现负载均衡
SpringCloud从零构建(五)——Config配置中心
SpringCloud从零构建(六)——消息总线Bus+Rabbit MQ实现动态刷新
github地址:https://github.com/gjen1996/microservice
如果有问题欢迎小伙伴留言和我沟通交流。