1. 程式人生 > >十二、SpringCloud之Spring Cloud Bus配置中心

十二、SpringCloud之Spring Cloud Bus配置中心

一、簡介

ConfigServer使用了Spring Cloud Bus之後(引入Spring Cloud Bus用來操作訊息佇列),會對外提供一個介面,叫做bus-refresh,遠端git訪問這個介面ConfigServer就會把配置更新的資訊傳送到訊息佇列(RabbitMQ)裡面,ConfigServer 和order服務通過訊息佇列(RabbitMQ)來傳遞訊息

二、Spring Cloud Bus實操

1、config-server

  • pom.xml
<?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>
    <groupId>com.imooc</groupId>
    <artifactId>config</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>config</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.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.RELEASE</spring-cloud.version>
    </properties>
    <!-- 引入依賴 -->
    <dependencies>
        <!-- 引入config-server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!-- 引入eureka-client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- 引入eureka-server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!-- 引入bus-amqp 訊息佇列-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
        <!-- 引入config-monitor 自動重新整理配置-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-monitor</artifactId>
        </dependency>
        <!-- 引入actuator 自動重新整理配置-->
        <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>
    <!-- 打包 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <!-- 倉庫地址-->
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>
  • bootstrap.yml
spring:
  application:
    name: config #配置服務名稱
  #配置config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/AmyZurich/zwj #地址
          username: XXX # 使用者名稱
          password: XXX # 密碼
          #配置檔案拉取地址
          #basedir: /Users/Amy/Documents/ideaproject/config/basedir
  rabbitmq:
    host: 192.168.3.233
    username: guest
    password: guest
    port: 5672
#配置Eureka
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
#配置端扣號
server:
  port: 8808
management:
  endpoints:
    web:
      exposure:
        include: "*"
  • ConfigApplication
@SpringBootApplication
@EnableDiscoveryClient //開啟發現服務功能
@EnableConfigServer //開啟服務端功能
public class ConfigApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class, args);
    }
}
  • 測試

測試地址:http://localhost:8808/order-dev.yml

2、config-client

  • pom.xml
<?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>
        <artifactId>order</artifactId>
        <groupId>com.imooc</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>order-server</artifactId>
    <!-- 引入依賴 -->
    <dependencies>
        <!-- 引入web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 引入eureka-client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- 引入data-jpa -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!-- 引入mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- 引入lombok 可以省去get set方法 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!-- 引入gson 轉換資料-->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </dependency>
        <!-- 引入feign 應用通訊-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
            <version>1.4.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- 引入order-common-->
        <dependency>
            <groupId>com.imooc</groupId>
            <artifactId>order-common</artifactId>
        </dependency>
        <!-- 引入product-client -->
        <dependency>
            <groupId>com.imooc</groupId>
            <artifactId>product-client</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
        <!-- 引入cloud-config-client 統一配置中心 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
        </dependency>
        <!-- 引入bus-amqp 訊息佇列-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </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>
    <!-- 倉庫地址 -->
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>
  • bootstrap.yml
spring:
  application:
    name: order #配置服務名稱
  #統一配置中心
  cloud:
      config:
        discovery:
          enabled: true
          service-id: CONFIG   #configServer 服務的名字
        profile: dev
      bus:
        trace:
          enabled: true
  #訊息佇列
  rabbitmq:
    host: 192.168.3.233
    username: guest
    password: guest
    port: 5672
#配置Eureka
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  • OrderApplication
@SpringBootApplication //開啟服務發現功能
@EnableDiscoveryClient
//掃描商品的包路徑
@EnableFeignClients(basePackages = "com.imooc.product.client")//開啟Feign功能
@RefreshScope
public class OrderApplication {
	public static void main(String[] args) {
		SpringApplication.run(OrderApplication.class, args);
	}
}
  • Controller
@Data
@Component
@ConfigurationProperties("girl")
@RefreshScope //重新整理配置
public class GirlConfig {
    private String name;
    private Integer age;
}
@RestController
@RefreshScope //重新整理配置
public class GirlController {
    @Autowired
    private GirlConfig girlConfig;
    @GetMapping("/girl/print")
    public String print() {
        return "name:" + girlConfig.getName() + " age:" + girlConfig.getAge();
    }
}
  • 測試

測試地址:http://localhost:8802/girl/print

3、自動重新整理配置

  • 獲取natapp提供的外網網址,即可實現本地實時開發除錯了,natapp教程
  • 開啟git上配置倉庫的地址,新增webhooks

上面的Payload URL就填寫我們的配置中心觸發重新整理的地址,當然這裡不能寫localhost啦,要外網訪問地址才行。