40、微服务 配置中心 SpringCloud Config 和消息总线 Bus
1、创建分布式配置中心服务端 cloud-config-center3344
编写pom.xml依赖
<dependencies>
<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-client</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
首先在gitee或者 gitHub上创建一个专门放配置的仓库://gitee.com/MyShopPlus/spring-cloud-config.git 创建一个目录respo,里面config-dev.yml
config:
info: cloud-config-client version=1
编写application.yml配置文件
spring: application: name: cloud-config-center cloud: config: label: master server: git: uri: https://gitee.com/MyShopPlus/spring-cloud-config.git search-paths: respo username: 13652332424 password: shun2302768 server: port: 3344 eureka: client: serviceUrl: defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
编写主启动类
@SpringBootApplication @EnableDiscoveryClient @EnableConfigServer public class ConfigCenterApplication3344 { public static void main(String[] args) { SpringApplication.run(ConfigCenterApplication3344.class,args); } }
测试访问:http://localhost:3344/config-dev.yml
2、创建配置中心的客户端 cloud-config-client3355
编写pom.xml 添加依赖
<dependencies>
<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-client</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
编写 bootstrap.yml 配置文件
server: port: 3355 spring: application: name: cloud-config-client3355 cloud: config: uri: http://localhost:3344 #服务端的访问地址 name: config #gie上的配置文件的名字:config-dev.yml label: master #哪个分支下的 profile: dev #哪个环境:比如:dev,sit,prod eureka: client: serviceUrl: defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
编写主启动类
@SpringBootApplication @EnableDiscoveryClient public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } }
编写controller 读取配置中心上的配置文件(config-dev.yml)里面的内容
@RestController public class ConfigClientController { @Value("${config.info}") private String configInfo; @GetMapping("/config/info") public String configInfo() { return configInfo; } }
测试地址:http://localhost:3355/config/info

浙公网安备 33010602011771号