1. 程式人生 > >springcloud4 配置中心(碼雲或者github)

springcloud4 配置中心(碼雲或者github)

1.新建一個配置中心模組

備註:遠端倉庫的配置程式碼,和客戶端或者服務端的基本一樣。

1.1 主方法

@SpringBootApplication
@EnableEurekaClient //加入註冊中心
@EnableConfigServer //啟用配置服務端
public class UserProviderApplication_6666 {
    public static void main(String[] args) {
        SpringApplication.run(UserProviderApplication_6666.class, args);
    }
}

1.2 application配置

server:
    #配置中心的埠號
  port: 6666 
eureka:
  client:
    service-url:
      defaultZone:  http://peer1000:1000/eureka/,http://peer1001:1001/eureka/
  #可以使用ip註冊
  instance:
    prefer-ip-address: true
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          #是在倉庫的配置中心地址
          uri: 
https://gitee.com/ccjdk/springcloud-demo.git username: ...... password: ......

1.3 pom

<!--springboot支援-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<
dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!--eureka客戶端--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--配置中心支援--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>

 

 

2.客戶端(配置檔名字是bootstrap.yml,優先順序高於application)

2.1 基於springcloud3中的 客戶端,

在pom中追加上

<!--開啟載入配置中心-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

2.2 bootstrap.yml配置

spring:
  cloud:
    config:
      #name+profile的總名字是在碼雲或者github上的名字application-getting-test
      name: application-getting #github上面名稱
      profile: test #環境
      #master是在倉庫上的分支名字,是預設的一個
      label: master #分支
      uri: http://localhost:6666 #配置伺服器
eureka:
  client:
    service-url:
      defaultZone:  http://peer1000:1000/eureka/,http://peer1001:1001/eureka/
  instance:
    prefer-ip-address: true #顯示客戶端真實ip

2.3 執行主方法,埠號就變成了遠端倉庫