1-04隘道、Spring Cloud Alibaba 創(chuàng)建服務提供者

創(chuàng)建一個工程名為 hello-spring-cloud-alibaba-nacos-provider 的服務提供者項目畔塔,pom.xml 配置如下:

POM

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

    <parent>
        <groupId>com.wj</groupId>
        <artifactId>hello-spring-cloud-alibaba-dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../hello-spring-cloud-alibaba-dependencies/pom.xml</relativePath>
    </parent>

    <artifactId>hello-spring-cloud-alibaba-nacos-provider</artifactId>
    <packaging>jar</packaging>

    <name>hello-spring-cloud-alibaba-nacos-provider</name>

    <dependencies>
        <!-- Spring Boot Begin -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Spring Boot End -->

        <!-- Spring Cloud Begin -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <!-- Spring Cloud End -->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.wj.hello.spring.cloud.alibaba.nacos.provider.NacosProviderApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Application

通過 @EnableDiscoveryClient 注解表明是一個 Nacos 客戶端怜瞒,該注解是 Spring Cloud 提供的原生注解

package com.wj.hello.spring.cloud.alibaba.nacos.provider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

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

    @RestController
    public class EchoController {
        @GetMapping(value = "/echo/{message}")
        public String echo(@PathVariable String message) {
            return "Hello Nacos Discovery " + message;
        }
    }
}

application.yml

spring:
  application:
    name: nacos-provider
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

server:
  port: 8081

# 端點檢查,查看服務是否在線
management:
  endpoints:
    web:
      exposure:
        include: "*"

啟動工程

通過瀏覽器訪問 http://localhost:8848/nacos止潮,即 Nacos Server 網(wǎng)址

你會發(fā)現(xiàn)一個服務已經(jīng)注冊在服務中了窃判,服務名為 nacos-provider

這時打開 http://localhost:8081/echo/hi ,你會在瀏覽器上看到:Hello Nacos Discovery hi

服務的端點檢查

spring-cloud-starter-alibaba-nacos-discovery 在實現(xiàn)的時候提供了一個 EndPoint, EndPoint 的訪問地址為 http://ip:port/actuator/nacos-discovery喇闸。 EndPoint 的信息主要提供了兩類:

1袄琳、subscribe: 顯示了當前有哪些服務訂閱者
2、NacosDiscoveryProperties: 顯示了當前服務實例關(guān)于 Nacos 的基礎(chǔ)配置

通過瀏覽器訪問 http://localhost:8081/actuator/nacos-discovery 你會在瀏覽器上看到:

附:Nacos Starter 更多配置項信息

配置項 Key 默認值 說明
服務端地址 spring.cloud.nacos.discovery.server-addr Nacos Server 啟動監(jiān)聽的ip地址和端口
服務名 spring.cloud.nacos.discovery.service ${spring.application.name} 給當前的服務命名
權(quán)重 spring.cloud.nacos.discovery.weight 1 取值范圍 1 到 100燃乍,數(shù)值越大唆樊,權(quán)重越大
網(wǎng)卡名 spring.cloud.nacos.discovery.network-interface 當IP未配置時,注冊的IP為此網(wǎng)卡所對應的IP地址刻蟹,如果此項也未配置逗旁,則默認取第一塊網(wǎng)卡的地址
注冊的IP地址 spring.cloud.nacos.discovery.ip 優(yōu)先級最高
注冊的端口 spring.cloud.nacos.discovery.port -1 默認情況下不用配置,會自動探測
命名空間 spring.cloud.nacos.discovery.namespace 常用場景之一是不同環(huán)境的注冊的區(qū)分隔離舆瘪,例如開發(fā)測試環(huán)境和生產(chǎn)環(huán)境的資源(如配置片效、服務)隔離等。
AccessKey spring.cloud.nacos.discovery.access-key 當要上阿里云時英古,阿里云上面的一個云賬號名
SecretKey spring.cloud.nacos.discovery.secret-key 當要上阿里云時淀衣,阿里云上面的一個云賬號密碼
Metadata spring.cloud.nacos.discovery.metadata 使用 Map 格式配置,用戶可以根據(jù)自己的需要自定義一些和服務相關(guān)的元數(shù)據(jù)信息
日志文件名 spring.cloud.nacos.discovery.log-name
接入點 spring.cloud.nacos.discovery.enpoint UTF-8 地域的某個服務的入口域名召调,通過此域名可以動態(tài)地拿到服務端地址
是否集成 Ribbon ribbon.nacos.enabled true 一般都設(shè)置成 true 即可
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末膨桥,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子某残,更是在濱河造成了極大的恐慌国撵,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件玻墅,死亡現(xiàn)場離奇詭異介牙,居然都是意外死亡,警方通過查閱死者的電腦和手機澳厢,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進店門环础,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人剩拢,你說我怎么就攤上這事线得。” “怎么了徐伐?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵贯钩,是天一觀的道長。 經(jīng)常有香客問我,道長角雷,這世上最難降的妖魔是什么祸穷? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮勺三,結(jié)果婚禮上雷滚,老公的妹妹穿的比我還像新娘。我一直安慰自己吗坚,他們只是感情好祈远,可當我...
    茶點故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著商源,像睡著了一般车份。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上炊汹,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天躬充,我揣著相機與錄音逃顶,去河邊找鬼讨便。 笑死,一個胖子當著我的面吹牛以政,可吹牛的內(nèi)容都是我干的霸褒。 我是一名探鬼主播,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼盈蛮,長吁一口氣:“原來是場噩夢啊……” “哼废菱!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起抖誉,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤殊轴,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后袒炉,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體旁理,經(jīng)...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年我磁,在試婚紗的時候發(fā)現(xiàn)自己被綠了孽文。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,505評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡夺艰,死狀恐怖芋哭,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情郁副,我是刑警寧澤减牺,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響拔疚,放射性物質(zhì)發(fā)生泄漏斑鸦。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一草雕、第九天 我趴在偏房一處隱蔽的房頂上張望巷屿。 院中可真熱鬧,春花似錦墩虹、人聲如沸嘱巾。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽旬昭。三九已至,卻和暖如春菌湃,著一層夾襖步出監(jiān)牢的瞬間问拘,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工惧所, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留骤坐,地道東北人。 一個月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓下愈,卻偏偏與公主長得像纽绍,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子势似,可洞房花燭夜當晚...
    茶點故事閱讀 45,515評論 2 359

推薦閱讀更多精彩內(nèi)容