server端配置
POM文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
配置文件
application.yml
server: port: 8251 spring: application: name: config-server-bus cloud: config: server: git: username: git userName password: gitPassword uri:git Url search-paths: /spring-cloud-config default-label: spring-cloud-config bus: trace: enabled: true # 啟用日志跟蹤 rabbitmq: # rabbitmq的配置信息 host: localhost port: 5672 username: guest password: guest management: # 管理端信息 endpoints: web: exposure: include: bus-env,bus-refresh # 暴露端點 endpoint: health: show-details: always # 日志顯示時機
client端配置
POM文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
配置文件
bootstrap.yml
server: port: 8081 spring: rabbitmq: # Rabbit Mq 配置信息 host: localhost port: 5672 username: guest password: guest profiles: active: dev # 啟動項目加載的配置文件類型 application: name: config cloud: config: uri: http://localhost:8251 # 配置服務器地址 name: ${spring.application.name} # 服務名稱 profile: ${spring.profiles.active} # 那個類型的配置為文件 文件類型分為 dev test prod bus: trace: enabled: true management: endpoints: web: exposure: include: bus-env,bus-refresh endpoint: health: show-details: always refresh: enabled: true
原來代碼需要修改的
需要在原來的組件類上加該注解@RefreshScope表示這個類是刷新作用域,其他注解保持不變。
組件類定義:使用了類級注解@RestController、@Controller、@Service、@Repository、@Component、@Configuration的類
注意:服務端和客戶端都需要依賴amqp的依賴。
