天天看點

SpringCloud 學習筆記-config配置中心(三)

1、pom 依賴

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.gray</groupId>
    <artifactId>server-config</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>server-config</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <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.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-monitor</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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
           

2、 yml配置

server:
  port: 8610
spring:
  application:  #服務名稱
    name: config
  profiles:
    active: dev
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/graycat/SpringCloudStudy.git
          searchPaths: SpringCloud-dev/baseConfig #配置倉庫路徑
          username: 59147***@qq.com
          password: lizh***14ma
      label: dev #配置倉庫的分支
  #          設定本地GIT存儲目錄
  #          basedir: /Users/admin/code/myProjects/java/imooc/SpringCloud_Sell/config/basedir 本地GIT存儲位址設定
  rabbitmq:
    host: 192.168.1.16
    port: 5672
    username: admin
    password: admin
    virtualHost: /
    publisherConfirms: true
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
management:
  endpoints:
    web:
      exposure:  # 設定開放通路
        include: refresh,health,info,bus-refresh
           

 如果配置檔案存在本地(在項目根目錄/baseConfig檔案夾)

server:
  port: 8610

spring:
  application:
    name: config
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          search-locations: file:./baseConfig
  rabbitmq:
    host: 192.168.34.168
    port: 5672
    username: admin
    password: admin
    virtualHost: /
    publisherConfirms: true
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/
management:
  endpoints:
    web:
      exposure:
        include: refresh,health,info,bus-refresh
           

3、啟動類注解

@EnableConfigServer
@SpringCloudApplication
public class ServerConfigApplication {

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

}
           

4、自定義了非Post重新整理方式

@RestController
public class RefreshController {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${server.port}")
    private int port;

    @RequestMapping("/refresh")
    public String refresh(){
        String url = String.format("http://%s:%s", "localhost", port) + "/actuator/bus-refresh";
        URI uri = URI.create(url);
        //消息頭
        HttpHeaders httpHeaders = new HttpHeaders();
        // 設定contentType
        httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
        String httpBody = "{\"motto\":\"配置重新整理\"}";
        HttpEntity<String> httpEntity = new HttpEntity<String>(httpBody, httpHeaders);
        ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.POST, httpEntity, String.class);
        return response.getBody();
    }
}
           
@Component
public class RestTemplateConfig {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
           

一般情況都是 發送post請求 到配置中心,http://localhost:8101/actuator/bus-refresh

在CMD 中可以模拟post重新整理:curl -X POST  http://localhost:8101/actuator/bus-refresh

5、client端 使用配置中心的配置  例:user伺服器(正常用戶端pom和啟動類)

配置yml 由application.yml 改成 bootstrap.yml

spring:
  cloud:
    config:
      discovery:
        enabled: true  # 打開使用配置中心開關
        service-id: CONFIG  #配置中心 服務名稱
      profile: dev  # 使用配置類型
      name: clientUser,datasource,rabbitmq   # 需要的配置,這裡配置了多個:自己配置,資料庫,mq
      label: master
eureka:
  client:
    service-url:  # 這個不能放在配置中心,不然連配置中心都連不上
      defaultZone: http://localhost:8761/eureka/
           

配置中心,檔案存放 

SpringCloud 學習筆記-config配置中心(三)
localhost:8610/getway-dev.yml 
           
浏覽器上能直接檢視配置
           

繼續閱讀