欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring Cloud Config Bus

程序员文章站 2022-07-14 22:41:56
...

Spring Cloud Config Bus

Spring Cloud Config Bus
pom.xml

    <dependencies>
        <!--		spring-cloud-config-bus-server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

    </dependencies>

application.yml


---
spring:
  cloud:
    bus:
      trace:
        enabled: true
      enabled: true
    config:
      server:
        git:
#          password:
#          search-paths: /springcloud-config/config-repo
          uri: https://gitee.com/mingshine/SpringCloud-Config/
#          username:
  application:
    name: spring-cloud-config-bus-server
  rabbitmq:
    password: guest
    port: 5672
    host: 127.0.0.1
    username: guest
server:
  port: 6811
management:
  endpoints:
    web:
      exposure:
        include: bus-refresh
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:6888/eureka/

SpringCloudConfigBusServerApplication.java

package com.mingshine.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class SpringCloudConfigBusServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringCloudConfigBusServerApplication.class, args);
	}

}

Spring Cloud Config Bus
pom.xml

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

application.yml


server:
  port: 6812

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:6888/eureka/

spring:
  application:
    name: spring-cloud-config-bus-client

bootstrap.yml

spring:
  cloud:
    config:
      label: master
      uri: http://localhost:6811/  #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址


---
management:
  endpoints:
    web:
      exposure:
        include: refresh

SpringCloudConfigBusClientApplication.java

package com.mingshine.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringCloudConfigBusClientApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringCloudConfigBusClientApplication.class, args);
	}

}

ClientController.java

package com.mingshine.cloud;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope
public class ClientController {

    @Value("${test}")
    String test;

    @RequestMapping(value = "/test")
    public String test() {
        return test;
    }
}


相关标签: Spring Cloud