springcloud配置中心服務化和高可用。包括動態獲取配置更新

由於,個人配置中心的服務,是在以前的服務基礎上面進行的,全部,如今須要的,就是修改對應的客戶端的配置。html

1.增長配置中心的pom配置java

<?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.4.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.example</groupId>
   <artifactId>democloudserver</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>democloudserver</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-starter-netflix-eureka-server</artifactId>
      </dependency>

      <!--Springclud 的配置中心-->
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-config-server</artifactId>
      </dependency>

      <dependency>
         <groupId>org.projectlombok</groupId>
         <artifactId>lombok</artifactId>
         <optional>true</optional>
      </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.git

###############註冊eureka服務##############
spring.application.name=spring-cloud-producer
server.port=9011
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/


##############集成SpringClud的配置中心##########
spring.cloud.config.server.git.uri=https://gitee.com/strongFan/individualProject/
#spring.cloud.config.server.git.uri=https://gitee.com/strongFan/individualProject.git   這裏獲取的不是git的地址,請參考進行對比



#配置文件的文件夾名稱
spring.cloud.config.server.git.searchPaths=config-repo
spring.cloud.config.server.git.username=xxxxxx
spring.cloud.config.server.git.password=xxxxxx
#開啓git的pull功能
spring.cloud.config.server.git.force-pull=true

3.啓動項的配置註解spring

package com.example.democloudserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableConfigServer  //開啓SpringClud的配置中心
@SpringBootApplication
@EnableDiscoveryClient//啓用服務註冊與發現
public class DemocloudserverApplication {

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

}

啓動該服務,在服務中心查看,已經顯示該服務apache

 

客戶端進行服務化,註冊進入配置中心bootstrap

1.pom增長配置中心客戶端依賴app

<?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.4.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.example</groupId>
   <artifactId>service-feign</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>service-feign</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-starter-netflix-eureka-server</artifactId>
      </dependency>

      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-openfeign</artifactId>
      </dependency>

      <dependency>
         <groupId>org.projectlombok</groupId>
         <artifactId>lombok</artifactId>
         <optional>true</optional>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>

      <!--熔斷器視圖監控依賴Hystrix Dashboard-->
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-hystrix</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-actuator</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>

   </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.和上次同樣,不一樣的是,bootstrap.properties 裏面,新增幾個配置maven

##########配置中心服務端獲取############
spring.cloud.config.name=neo-config-dev
spring.cloud.config.profile=dev
#spring.cloud.config.uri=http://localhost:9011/
spring.cloud.config.label=master

############將其修改成調用配置服務###########
#開啓Config服務發現支持
spring.cloud.config.discovery.enabled=true
#指定server端的name,也就是server端spring.application.name的值
spring.cloud.config.discovery.serviceId=spring-cloud-producer
#指向註冊中心的地址
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

#上面這些與spring-cloud相關的屬性必須配置在bootstrap.properties中,config部份內容才能被正確加載。
#由於config的相關配置會先於application.properties,而bootstrap.properties的加載也是先於application.properties。

 

3. 項目啓動項svn

package com.example.servicefeign;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient//啓用服務註冊與發現
@EnableFeignClients//啓用feign進行遠程調用
public class ServiceFeignApplication {

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

}

啓動,查看服務中心,已經成功了。spring-boot

再次請求客戶端的方法

原本,還準備配置配置中心的refresh  可是,配置了沒生效,搗鼓了半天,就沒管,先看後面的了。  後面看的時候,在關注下

相關文章
相關標籤/搜索