【Spring Cloud】小型项目的搭建日记:Spring Cloud Config的搭建

闲叙一下

本来我自己有打算写一个Spring Cloud相关知识的博客(本人技术很渣,小吹一下,嗯…),然后看到网上已经有很多人都在发相关的知识了,我就不再赘述了,首先不浪费自己的时间,也不浪费大家的时间,看着的都是“千篇一律”的文章,心里也不舒服啊。

我现在的工作多是运维相关,码代码的机会也少了,加上最近在看python,Java就基本忘得差不多了,然后在一个朋友(做前端开发的)的邀请下,准备合作一个小的项目(纯是练手的,大牛勿喷!!!),所以今天就动手搭了一下Spring Cloud的配置中心(Config)

步入正题

首先,我是使用IDEA来创建项目的(用过的都清楚怎么来创建一个Spring Boot项目),我就不在这里多说了,配置中心我放在了GitHub上,现在开始:
###config server###

application.yml的配置
server:
  port: 端口号
spring:
  application:
    name: 应用的名称(对于eureka而言的serviceId)
  cloud:
      config:
        server:
          git:
            search-paths: 分支下存放配置文件的文件夹名
            uri: 你的配置中心所在的路径(https://github.com/用户名/项目名)
        label: master(分支名称)
#eureka
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/(默认地址)
#management(配置中心的自动刷新)
management:
  endpoints:
    web:
      exposure:
        include: bus-refresh

如果上面的项目的是private(私有)的,那么需要加上
spring.cloud.config.server.git.username
spring.cloud.config.server.git.password

pom.xml
		<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-server</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-bus-amqp</artifactId>
		</dependency>

然后在启动类加上**@EnableEurekaClient、@EnableConfigServer** 这两个注解,这样配置中心的服务端就搭好了。

###config client###
####bootstrap.yml(这里注意下,是bootstrap,不是application)

server:
  port: 8652
spring:
  application:
    name: config-client
  cloud:
      bus:
        trace:
          enabled: true
      config:
            uri: http://localhost:8651/
            label: master
            profile: dev
            name: config-client
            discovery:
              enabled: true
              serviceId: config-server
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
#eureka
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

#management.security.enabled=false
management:
  endpoints:
    web:
      exposure:
        include: bus-refresh

这里我用的是rabbitmq 来实现配置的同步。

####pom.xml

	<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-netflix-eureka-server</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.retry</groupId>
		<artifactId>spring-retry</artifactId>
	</dependency>

	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-bus-amqp</artifactId>
	</dependency>

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-actuator</artifactId>
	</dependency>

然后在启动类上加上**@RefreshScope、@EnableEurekaClient** 这两个注解,配置中心的客户端也搭好了。
######这个我最早是在方志朋的博客里发现的。(我也来一张图)
这里写图片描述
最后,希望我能坚持下去,把这个练手的项目跟完,也非常感谢能看到这里的人。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值