Spring Coud_31_SprigCloud配置センター/例
16781 ワード
SprigCloud配置センター/例 Configの役割:クラスタ構成の統一管理 Configの役割:クラスタ構成の統一管理 クライアント接続設定サーバの場合、デフォルト接続8888ポート 1、Configサーバを作成する Configサーバを作成する前に、SVNサーバを作成する必要があります.前の章はすでに を作成しました.今Cofigサーバ を作成します.
1.1、導入依存 SprigCloudはGit倉庫をデフォルトで使用しています.SVNとGIT倉庫が全部配置されていない場合、 にエラーが発生します.プロファイルを設定します. git:デフォルト値は、Git倉庫に行ってプロファイルを読み込むことを示す です. subversion:SVN倉庫にプロファイルを読み込むことを示す native:ローカルのファイルシステムでプロファイル を読み込みます. vault:Vaultにプロファイルを読み込みに行きます.Vaultは資源制御ツールです.資源に対して安全アクセスが可能です.
SVNディレクトリにファイルを作成します.作成されたファイル名規則に注意してください.本人はテスト時にfirst.ymlを使用してアクセスできません.first-test.ymlという名前の場合は にアクセスできます.ブラウザアクセスhttp://127.0.0.1:8888/first-test.yml 2、Configクライアントの作成
2.1、導入依存総括を読みだします.(サーバ構成ディレクトリ、クライアント構成はどのファイルを読みますか?) サーバ:spring.cloud.co.fig.server.svn.uri=https://USER-20170523PW/svn/my-aitemi/接続が必要なSVN を設定します.サーバ:spring.cloud.co.fig.server.default-label=aitemiは、SVNの指定ディレクトリに を読み込みます.クライアント:spring.appication.name=am-config-client、読み込むプロファイルの名前は、 です.クライアント:spring.cloud.co.profile=dev マルチプロファイルを読みだします.spring.cloudc.fig.profile=hystrix、zul クライアント構成ディレクトリ:spring.cloud.com nfig.labelは、クライアントが設定ファイルを読み込むディレクトリを設定することができ、サーバが構成するdefault-label をカバーします.アクセス/refreshエンドポイント アナログPOST要求 実際のアプリケーションでは、構成を更新する場合、対応するbean を更新する必要がある.
1.1、導入依存
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Dalston.SR3version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-config-serverartifactId>
dependency>
<dependency>
<groupId>org.tmatesoft.svnkitgroupId>
<artifactId>svnkitartifactId>
<version>1.9.0version>
dependency>
1.2、SVN倉庫の配置server:
port: 8888
## ,
management:
security:
enabled: false
## SVN
spring:
profiles:
active: subversion
cloud:
config:
server:
svn:
uri: https://USER-20170523PW/svn/my-aitemi/
username: aitemi
password: aitemi
## SVN trunk
default-label: aitemi
1.3、起動クラスpackage com.atm.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer//
public class ConfigServerApp {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApp.class, args);
}
}
2.1、導入依存
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Dalston.SR3version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-configartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>1.5.4.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
<version>1.5.4.RELEASEversion>
dependency>
2.2、配置ファイルブックstrap.ymlspring:
## spring.application.name, cloud.config.profile, atm-config-client-dev.yml
application:
name: atm-config-client
cloud:
config:
url: http://localhost:8888
## atm-config-client-dev.yml
profile: dev
spring:
cloud:
config:
url: http://localhost:8888
## spring.applicaiton.name, spring.cloud.config.name, , atm-config-client-dev.yml
profile: dev
name: atm-config-client
spring:
cloud:
config:
url: http://localhost:8888
## , spring.cloud.profile, application-dev.yml
profile: dev
2.3、ReadConfigControllerpackage com.atm.cloud;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
*
*/
@RestController
public class ReadConfigController {
@Autowired
private Environment environment;
@GetMapping("/read")
public String readConfig() {
return environment.getProperty("test.user.name");
}
}
3、カタログ構成読取りまとめspring:
cloud:
config:
label: aitemi2
4、設定を更新する## ,
management:
security:
enabled: false
package com.atm.cloud;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class RefreshClientMain {
public static void main(String[] args) throws Exception {
CloseableHttpClient client = HttpClients.createDefault();
// post
HttpPost post = new HttpPost("http://localhost:8080/refresh");
HttpResponse response = client.execute(post);
System.out.println(EntityUtils.toString(response.getEntity()));
}
}
5、ビーンをリフレッシュするpackage com.atm.cloud;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
@Configuration
public class MyConfig {
@Bean
@RefreshScope
public Person person(Environment env) {
// Person
String name = env.getProperty("test.user.name");
// Person
System.out.println(" Person bean:" + name);
// Bean
Person p = new Person();
p.setName(name);
return p;
}
}
@Autowired
private Person person;
@GetMapping("/personName")
public String readPersonName() {
return person.getName();
}