10、服務提供者provider如何使用配置中心config-創(chuàng)新互聯(lián)

前面的《配置中心》和《服務注冊&服務提供者》這兩篇分別講解了配置中心和服務提供者,但是服務提供者使用的配置文件還是本地的,沒有使用配置中心的配置文件。今天看看如何實現(xiàn)服務提供者使用配置中心的配置文件。

專業(yè)從事做網(wǎng)站、成都網(wǎng)站建設,高端網(wǎng)站制作設計,成都小程序開發(fā),網(wǎng)站推廣的成都做網(wǎng)站的公司。優(yōu)秀技術團隊竭力真誠服務,采用H5高端網(wǎng)站建設+CSS3前端渲染技術,成都響應式網(wǎng)站建設,讓網(wǎng)站在手機、平板、PC、微信下都能呈現(xiàn)。建站過程建立專項小組,與您實時在線互動,隨時提供解決方案,暢聊想法和感受。

10、服務提供者provider如何使用配置中心config
1、 新建項目sc-eureka-client-provider-config,項目對應的pom.xml文件如下

<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>

    <groupId>spring-cloud</groupId>
    <artifactId>sc-eureka-client-provider-config</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>sc-eureka-client-provider-config</name>
    <url>http://maven.apache.org</url>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
<!-- 說明是一個 eureka client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!-- spring boot實現(xiàn)Java Web服務 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- 把tomcat-jdbc連接池排除掉,這樣spring-boot就會尋找是否有HikariCP可用 -->
            <exclusions>
                <exclusion>
                    <groupId>org.apache.tomcat</groupId>
                    <artifactId>tomcat-jdbc</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
    </dependencies>
</project>

備注:可以看到pom.xml中引入了spring-cloud-starter-config,這個引入在《如何獲取配置中心的配置》博文中說到。引入這個配置項說明只要在配置文件中做相應的配置就可以獲取到配置中心的配置項。

2、 新建springboot啟動類型ProviderConfigApplication.java

package sc.provider.config;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient
@SpringBootApplication
@MapperScan(basePackages="sc.provider.config.dao")
public class ProviderConfigApplication {

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

}

3、 新建配置文件bootstrap.yml

server:
    port: 8500

eureka:
    client:
        serviceUrl:
            defaultZone: http://localhost:5001/eureka/

spring:
    application:
        name: sc-eureka-client-provider-config
    cloud:
        config:
            name: sc-eureka-client-provider-config
            label: master # 配置文件所在分支
            #uri: http://127.0.0.1:8100/  #配置服務中心
            profile: dev  # dev根據(jù)具體情況來修改
            #profile: prd  # dev根據(jù)具體情況來修改
            discovery:
                serviceId: sc-config-server #配置服務實例名稱
                enabled: true  #開啟配置服務發(fā)現(xiàn)

備注:配置文件用有如下配置項
10、服務提供者provider如何使用配置中心config
該配置項將會作為配置中心config server的配置文件bootstrap.yml中的search-paths的一個占位符{application}的值

10、服務提供者provider如何使用配置中心config
4、 其他項目文件如下圖

10、服務提供者provider如何使用配置中心config

5、 修改配置中心sc-config-server的配置文件bootstrap.yml

#服務端口
server:
    port: 8100

#服務注冊中心
eureka:
    client:
        registerWithEureka: true #是否將自己注冊到Eureka服務中,默認為true
        fetchRegistry: true #是否從Eureka中獲取注冊信息,默認為true
        serviceUrl:
            defaultZone: http://localhost:5001/eureka/
    instance: 
        prefer-ip-address: true #將自己的ip地址注冊到Eureka服務中
        ipAddress: 127.0.0.1

spring:
    application:
        name: sc-config-server #服務名稱
    cloud:
        config:
            label: master #配置文件所在的分支
            server:
                git:
                    #uri: https://gitee.com/hjj520/spring-cloud-2.x.git #服務的git倉庫地址
                    uri: https://gitee.com/hjj520/spring-cloud-2.x #服務的git倉庫地址
                    #git倉庫的用戶名
                    #username: huangjinjin
                    #git倉庫的密碼
                    #password: ********
                    #search-paths: /config-repos/sc-consumer-config  #配置文件所在的目錄
                    #search-paths: /config-repos/sc-config-client
                    #search-paths: /config-repos/sc-eureka-client-provider-config
                    search-paths:  /config-repos/{application}

6、 在git倉庫新建如下內(nèi)容,并提交到git倉庫中
10、服務提供者provider如何使用配置中心config
applicaton-dev.yml和application-prd.yml的內(nèi)容是一樣的(dev代表開發(fā)環(huán)境,prd代表生產(chǎn)環(huán)境,實際項目中這兩個文件一定是不一樣的)

spring:
    datasource:
        driverClassName: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/sc?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
        username: root
        password: root
        type: com.zaxxer.hikari.HikariDataSource
        hikari:
            minimum-idle: 5
            maximum-pool-size: 15
            auto-commit: true
            idle-timeout: 30000
            pool-name: DatebookHikariCP
            max-lifetime: 1800000
            connection-timeout: 30000
            connection-test-query: SELECT 1

7、 先分別啟動注冊中心sc-eureka-server和配置中心sc-config-server

8、 啟動sc-eureka-client-provider-config項目在控制臺可以看到如下輸出
10、服務提供者provider如何使用配置中心config
說明項目已經(jīng)通過配置中心獲取git倉庫的配置文件,如果看到如下輸出說明啟動成功

10、服務提供者provider如何使用配置中心config
9、 通過postman訪問相關restful接口驗證是否能正常訪問
查詢:
http://127.0.0.1:8500/user/getUser/4

10、服務提供者provider如何使用配置中心config
列表:
http://127.0.0.1:8500/user/listUser/
10、服務提供者provider如何使用配置中心config

添加:
http://127.0.0.1:8500/user/addUser

10、服務提供者provider如何使用配置中心config
更新:
http://127.0.0.1:8500/user/updateUser
10、服務提供者provider如何使用配置中心config
刪除:
http://127.0.0.1:8500/user/deleteUser/7
10、服務提供者provider如何使用配置中心config

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

分享標題:10、服務提供者provider如何使用配置中心config-創(chuàng)新互聯(lián)
網(wǎng)頁鏈接:http://muchs.cn/article48/djjiep.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作網(wǎng)頁設計公司、微信公眾號、網(wǎng)站維護云服務器、品牌網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設