マイクロサービストピックのSpringBoot2.Xは、構成センターとしてNacosを統合します

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion> 4.0.0 </ modelVersion>


    <groupId> com.sino.microservice </ groupId>

    <artifactId> appw </ artifactId>

    <バージョン> 0.0.1-スナップショット</バージョン>

    <name> appw </ name>

    <description> SpringBootのデモプロジェクト</ description>


    <プロパティ>

        <java.version> 1.8 </ java.version>

        <project.build.sourceEncoding> UTF-8 </ project.build.sourceEncoding>

        <maven.compiler.source> 8 </ maven.compiler.source>

        <maven.compiler.target> 8 </ maven.compiler.target>

        <spring.boot.version> 2.3.2.RELEASE </ spring.boot.version>

        <spring.cloud.alibaba.version> 2.2.3.RELEASE </ spring.cloud.alibaba.version>

    </ properties>


    <!-SrpingBootバージョンとalibabaクラウドバージョンを定義します->

    <dependencyManagement>

        <依存関係>

            <依存関係>

                <groupId> org.springframework.boot </ groupId>

                <artifactId> spring-boot-dependencies </ artifactId>

                <version> $ {spring.boot.version} </ version>

                <type> pom </ type>

                <scope> import </ scope>

            </依存性>

            <依存関係>

                <groupId> com.alibaba.cloud </ groupId>

                <artifactId> spring-cloud-alibaba-dependencies </ artifactId>

                <version> $ {spring.cloud.alibaba.version} </ version>

                <type> pom </ type>

                <scope> import </ scope>

            </依存性>

        </依存関係>

    </ dependencyManagement>

    <依存関係>

        <依存関係>

            <groupId> org.springframework.boot </ groupId>

            <artifactId> spring-boot-starter-web </ artifactId>

        </依存性>

        <依存関係>

            <groupId> org.springframework.boot </ groupId>

            <artifactId> spring-boot-starter-test </ artifactId>

        </依存性>

        <!-レジストリが導入する必要のある依存関係->

        <依存関係>

            <groupId> com.alibaba.cloud </ groupId>

            <artifactId> spring-cloud-starter-alibaba-nacos-discovery </ artifactId>

        </依存性>

        <!-構成センターが導入する必要のある依存関係->

        <依存関係>

            <groupId> com.alibaba.cloud </ groupId>

            <artifactId> spring-cloud-starter-alibaba-nacos-config </ artifactId>

        </依存性>

    </依存関係>

    <ビルド>

        <finalName> appw </ finalName>

        <pluginManagement> <!-プラグインのバージョンをロックダウンして、Mavenのデフォルトを使用しないようにします(親pomに移動される場合があります)->

            <プラグイン>

                <プラグイン>

                    <artifactId> maven-clean-plugin </ artifactId>

                    <バージョン> 3.1.0 </バージョン>

                </プラグイン>


                <!-http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packagingを参照->

                <プラグイン>

                    <artifactId> maven-resources-plugin </ artifactId>

                    <バージョン> 3.0.2 </バージョン>

                </プラグイン>


                <プラグイン>

                    <artifactId> maven-compiler-plugin </ artifactId>

                    <バージョン> 3.8.0 </バージョン>

                </プラグイン>


                <プラグイン>

                    <artifactId> maven-surefire-plugin </ artifactId>

                    <バージョン> 2.22.1 </バージョン>

                </プラグイン>


                <プラグイン>

                    <artifactId> maven-war-plugin </ artifactId>

                    <バージョン> 3.2.2 </バージョン>

                </プラグイン>


                <プラグイン>

                    <artifactId> maven-install-plugin </ artifactId>

                    <バージョン> 2.5.2 </バージョン>

                </プラグイン>


                <プラグイン>

                    <artifactId> maven-deploy-plugin </ artifactId>

                    <バージョン> 2.8.2 </バージョン>

                </プラグイン>

            </プラグイン>

        </ pluginManagement>

    </ build>

</プロジェクト>



bootstrap.properties

#ブートストラップは、親のSpringApplicationContextによってロードされます。親ApplicationContextは、アプリケーションを使用する前にロードされます。

#Spring Cloudを使用する場合、構成情報は通常、構成サーバーからロードされます。構成情報(パスワードなど)を取得するには、初期構成またはブート構成が必要です。

#したがって、構成サーバー情報をブートストラップに入れて、本当に必要な構成情報をロードします

server.port = 8070

spring.application.name = Consumer-config


#登録センターのアドレス構成

spring.cloud.nacos.discovery.server-addr = 127.0.0.1:8848

#構成センターアドレス構成

spring.cloud.nacos.config.server-addr = 127.0.0.1:8848


#環境を開発者として定義する

spring.profiles.active = dev

#構成ファイルのサフィックスを.properties、dataId = $ {spring.application.name}-$ {spring.profiles.active} .propertiesとして定義します

spring.cloud.nacos.config.file-extension = properties

#構成ファイルはデフォルトでDEFAULT_GROUPとしてグループ化されます

spring.cloud.nacos.config.group = Dev


TestNacosControl.java

パッケージcom.sino.microservice.appw.ctrl;


import org.springframework.beans.factory.annotation.Value;

import org.springframework.cloud.context.config.annotation.RefreshScope;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;


インポートjavax.annotation.PostConstruct;


@RestController

@RefreshScope

パブリッククラスTestNacosControl {

    @Value( "$ {test.nacos}")

    プライベート文字列testNacos;


    @PostConstruct

    public void printNacosConfig(){

        System.out.println( "Nacosから取得した構成は次のとおりです:" + testNacos);

    }


    @RequestMapping( "/ test-nacos")

    public String getTestNacos(){

        testNacosを返します。

    }

}


AppwStarter.java

パッケージcom.sino.microservice.appw;


import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication

パブリッククラスAppwStarter {

    public static void main(String [] args){

        System.out.println( "hello Appw");


        System.setProperty( "ignoreUnresolvablePlaceholders"、 "true");

        SpringApplication.run(AppwStarter.class、args);

    }

}


おすすめ

転載: blog.51cto.com/14960630/2572003