pom.xml
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.RC1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-bus</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-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> <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.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies>
</dependencyManagement>
启动类
在启动类中加入@RefreshScope该注解的作用在于使用消息总线刷新配置
bootstrap.yml配置
在这个实例中我把application.yml文件放到了git上通过spring cloud config server获取配置信息
所以把一些连接spring cloud config server挪到了bootstrap.yml中 bootstrap.yml在项目启动的加载顺序中大于application.yml
连接到rabbitMQ中 等待消息的刷新
spring: rabbitmq: host: 127.0.0.1 port: 5672 username: guest password: guest cloud: bus: trace: enabled: true #开启消息总线 enabled: true config: discovery: enabled: true #开启通过服务来访问Config Server的功能 service-id: config-server #在eureka中注册的服务名 name: client-b #在git上的配置文件名 profile: test #测试文件 label: master #在git主分支中
management: endpoints: web: exposure: include: bus-refresh #消息总线刷新的位置