微服务搭建Spring Cloud配置中心【服务端】

拼搏现实的明天。 2023-07-02 11:25 218阅读 0赞

spring boot版本:2.1.10.RELEASE
spring cloud版本:Greenwich.SR4

添加依赖

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-web</artifactId>
  8. </dependency>
  9. <!--配置中心依赖-->
  10. <dependency>
  11. <groupId>org.springframework.cloud</groupId>
  12. <artifactId>spring-cloud-config-server</artifactId>
  13. </dependency>

修改启动类

配置中心需添加 @EnableConfigServer、@EnableEurekaClient 两个注解。

  1. package com.ebook.config;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.config.server.EnableConfigServer;
  5. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  6. /** * @author:JZ * @date:2020/1/11 */
  7. @SpringBootApplication
  8. //配置中心需添加 @EnableConfigServer、@EnableEurekaClient
  9. @EnableConfigServer
  10. @EnableEurekaClient
  11. public class ConfigApplication {
  12. public static void main(String[] args) {
  13. SpringApplication.run(ConfigApplication.class, args);
  14. }
  15. }

添加配置

因为spring cloud 配置中心默认用Git存储相关文件,所以在application.properties 中添加Git相关配置。

  1. spring.application.name=ebook-config-server
  2. server.port=9100
  3. #将服务注册到注册中心
  4. eureka.client.service-url.defaultZone=http://192.168.xxx.xxx:18761/eureka/
  5. eureka.instance.prefer-ip-address=true
  6. eureka.instance.hostname=ebook-config-server
  7. #git仓库url
  8. spring.cloud.config.server.git.uri=http://192.168.xxx.xxx/git
  9. #git账号
  10. spring.cloud.config.server.git.username=username
  11. #git账号密码
  12. spring.cloud.config.server.git.password=password

配置文件命名规则

推荐使用 {application}-{profile}.properties 这种方式
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,218人围观)

还没有评论,来说两句吧...

相关阅读