springcloud分佈式配置中心

一共三個部分  

1、註冊中心

配置文件:application.yml

###服務啓動端口號
server:
  port: 8100
###服務名稱(服務註冊到eureka名稱)
spring:
  application:
    name: server
###服務註冊到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8100/eureka


    ###由於該應用爲註冊中心,不會註冊本身
    register-with-eureka: true
    ###是否須要從eureka上獲取註冊信息
    fetch-registry: true

啓動類

@EnableEurekaServer
@SpringBootApplication
public class springcloudEureka {
    public static void main(String[] args) {
        SpringApplication.run(springcloudEureka.class,args);
    }
}

pom文件

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.1.RELEASE</version>
	</parent>
	<!-- 管理依賴 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.M7</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
		<!--SpringCloud eureka-server -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>
	</dependencies>
	<!-- 注意: 這裏必需要添加, 否者各類依賴有問題 -->
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/libs-milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>

2、configServer

1、配置文件 :bootstrap.ymljava

###服務註冊到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8100/eureka
spring:
  application:
    ####註冊中心應用名稱
    name: config
  cloud:
    config:
      server:
        git:
          .
          ###git環境地址
          uri: https://gitee.com/XXXX/testspringcloud.git
          ####svn項目下屬搜索目錄
          search-paths:
          - testspringcloud
          ###帳號密碼
          username: XXXXX
          password: XXXXX
      ####讀取分支
      label: master
####端口號
server:
  port: 9999

2、啓動類git

@EnableConfigServer//開啓配置中心
@SpringBootApplication
@EnableEurekaClient
public class Star {

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

}

pom文件

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>  
<!-- 管理依賴 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.M7</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <!--spring-cloud 整合 config-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>

        <!-- SpringBoot整合eureka客戶端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

    </dependencies>
    <!-- 注意: 這裏必需要添加, 否者各類依賴有問題 -->
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

測試有沒有配置成功瀏覽器輸出:

3、configclient

1、配置文件 bootstrap.ymlweb

spring:
  application:
    ####與git上配置文件前綴同樣
    name:  config
  cloud:
    config:
      ####讀取後綴
      profile: sit
      ####讀取config-server註冊地址
      discovery:
        ##與configServer在註冊中心上別名同樣
        serviceId: config
        enabled: true
      label: master
     # uri: http://localhost:9999/
#####    eureka服務註冊地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8100/eureka
server:
  port: 8882

2、啓動類spring

@SpringBootApplication
@EnableEurekaClient
public class Star {

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

}

pom文件bootstrap

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>

    <!-- 管理依賴 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.M7</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

        </dependencies>
    </dependencyManagement>
    <dependencies>
        <!-- SpringBoot整合Web組件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
        </dependency>
        <!-- SpringBoot整合eureka客戶端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
    <!-- 注意: 這裏必需要添加, 否者各類依賴有問題 -->
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
相關文章
相關標籤/搜索