父工程代码如下:
pom
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</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.RELEASE</spring-cloud.version>
</properties>
<!-- 公共依赖 -->
<dependencies>
<!-- 核心包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 配置文件处理器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- 监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- 测试依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- 依赖管理 -->
<dependencyManagement>
<dependencies>
<!-- spring cloud依赖 -->
<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>
注册中心代码如下:
pom
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
bootstrap.yml
server:
port: 8761
spring:
application:
name: 127.0.0.1
eureka:
instance:
hostname: cloud-eureka-server
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
CloudEurekaServerBootstrap.java
@EnableEurekaServer
@SpringBootApplication
public class CloudEurekaServerBootstrap {
public static void main(String[] args) {
SpringApplication.run(CloudEurekaServerBootstrap.class, args);
}
}
配置中心服务端代码如下:
pom
<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-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
</dependencies>
bootstrap.yml
server:
port: 8881
eureka:
instance:
prefer-ip-address: true
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: cloud-config-amqp-server
cloud:
config:
server:
native:
search-locations:
- classpath:/shared/
profiles:
active:
- native
rabbitmq:
host: 192.168.46.120
username: username
password: password
management:
endpoints:
web:
exposure:
include: "*"
cors:
allowed-origins: "*"
allowed-methods: "*"
shared/cloud-config-amqp-client-dev.yml
server:
version: V1.0
CloudConfigAmqpServerBootstrap.java
@EnableDiscoveryClient
@EnableEurekaClient
@EnableConfigServer
@SpringBootApplication
public class CloudConfigAmqpServerBootstrap {
public static void main(String[] args) {
SpringApplication.run(CloudConfigAmqpServerBootstrap.class, args);
}
}
配置中心客户端代码如下
pom
<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.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
bootstrap.yml
server:
port: 8801
eureka:
instance:
prefer-ip-address: true
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: cloud-config-amqp-client
cloud:
config:
discovery:
enabled: true
service-id: cloud-config-amqp-server
profile: dev
bus:
enabled: true
trace:
enabled: true
rabbitmq:
host: 192.168.46.120
username: username
password: password
management:
endpoints:
web:
exposure:
include: "*"
cors:
allowed-origins: "*"
allowed-methods: "*"
VersionController.java
@RefreshScope
@RestController
public class VersionController {
@Value("${server.version}")
String version;
@RequestMapping(value = "/version")
public String getVersion() {
return version;
}
}
CloudConfigAmqpClientBootstrap.java
@EnableEurekaClient
@SpringBootApplication
public class CloudConfigAmqpClientBootstrap {
public static void main(String[] args) {
SpringApplication.run(CloudConfigAmqpClientBootstrap.class, args);
}
}