最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring?Cloud?Alibaba?Nacos服務(wù)治理平臺(tái)服務(wù)注冊(cè)、RestTemplate實(shí)現(xiàn)微服務(wù)之間訪問(wèn)負(fù)載均衡訪問(wèn)的問(wèn)題

 更新時(shí)間:2022年06月26日 08:56:33   作者:鱷魚兒  
這篇文章主要介紹了Spring?Cloud?Alibaba:Nacos服務(wù)治理平臺(tái),服務(wù)注冊(cè)、RestTemplate實(shí)現(xiàn)微服務(wù)之間訪問(wèn),負(fù)載均衡訪問(wèn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

??Nacos簡(jiǎn)介

Github:https://github.com/alibaba/nacos
官網(wǎng)文檔:https://nacos.io/zh-cn/docs/what-is-nacos.html
Nacos 提供了發(fā)現(xiàn)、配置和管理微服務(wù)能力,能快速實(shí)現(xiàn)動(dòng)態(tài)服務(wù)發(fā)現(xiàn)、服務(wù)配置、服務(wù)元數(shù)據(jù)及流量管理。使用 Nacos 可以更敏捷和容易地構(gòu)建、交付和管理微服務(wù)平臺(tái)。 Nacos 是構(gòu)建以“服務(wù)”為中心的現(xiàn)代應(yīng)用架構(gòu) (例如微服務(wù)范式、云原生范式) 的服務(wù)基礎(chǔ)設(shè)施。

1.服務(wù)發(fā)現(xiàn)和服務(wù)健康監(jiān)測(cè)
Nacos 支持基于 DNS 和基于 RPC 的服務(wù)發(fā)現(xiàn)。服務(wù)提供者使用 原生SDK、OpenAPI、或一個(gè)獨(dú)立的Agent TODO注冊(cè) Service 后,服務(wù)消費(fèi)者可以使用DNS TODO 或HTTP&API查找和發(fā)現(xiàn)服務(wù)。
Nacos 提供對(duì)服務(wù)的實(shí)時(shí)的健康檢查,阻止向不健康的主機(jī)或服務(wù)實(shí)例發(fā)送請(qǐng)求。Nacos 支持傳輸層 (PING 或 TCP)和應(yīng)用層 (如 HTTP、MySQL、用戶自定義)的健康檢查。 對(duì)于復(fù)雜的云環(huán)境和網(wǎng)絡(luò)拓?fù)洵h(huán)境中(如 VPC、邊緣網(wǎng)絡(luò)等)服務(wù)的健康檢查,Nacos 提供了 agent 上報(bào)模式和服務(wù)端主動(dòng)檢測(cè)2種健康檢查模式。Nacos 還提供了統(tǒng)一的健康檢查儀表盤,幫助您根據(jù)健康狀態(tài)管理服務(wù)的可用性及流量。

2.動(dòng)態(tài)配置服務(wù)
動(dòng)態(tài)配置服務(wù)可以讓您以中心化、外部化和動(dòng)態(tài)化的方式管理所有環(huán)境的應(yīng)用配置和服務(wù)配置。
動(dòng)態(tài)配置消除了配置變更時(shí)重新部署應(yīng)用和服務(wù)的需要,讓配置管理變得更加高效和敏捷。
配置中心化管理讓實(shí)現(xiàn)無(wú)狀態(tài)服務(wù)變得更簡(jiǎn)單,讓服務(wù)按需彈性擴(kuò)展變得更容易。
Nacos 提供了一個(gè)簡(jiǎn)潔易用的UI (控制臺(tái)樣例 Demo) 幫助您管理所有的服務(wù)和應(yīng)用的配置。Nacos 還提供包括配置版本跟蹤、金絲雀發(fā)布、一鍵回滾配置以及客戶端配置更新狀態(tài)跟蹤在內(nèi)的一系列開箱即用的配置管理特性,幫助您更安全地在生產(chǎn)環(huán)境中管理配置變更和降低配置變更帶來(lái)的風(fēng)險(xiǎn)。

3.動(dòng)態(tài) DNS 服務(wù)
動(dòng)態(tài) DNS 服務(wù)支持權(quán)重路由,讓您更容易地實(shí)現(xiàn)中間層負(fù)載均衡、更靈活的路由策略、流量控制以及數(shù)據(jù)中心內(nèi)網(wǎng)的簡(jiǎn)單DNS解析服務(wù)。動(dòng)態(tài)DNS服務(wù)還能讓您更容易地實(shí)現(xiàn)以 DNS 協(xié)議為基礎(chǔ)的服務(wù)發(fā)現(xiàn),以幫助您消除耦合到廠商私有服務(wù)發(fā)現(xiàn) API 上的風(fēng)險(xiǎn)。
Nacos 提供了一些簡(jiǎn)單的 DNS APIs TODO 幫助您管理服務(wù)的關(guān)聯(lián)域名和可用的 IP:PORT 列表.

4.服務(wù)及其元數(shù)據(jù)管理
Nacos 能讓您從微服務(wù)平臺(tái)建設(shè)的視角管理數(shù)據(jù)中心的所有服務(wù)及元數(shù)據(jù),包括管理服務(wù)的描述、生命周期、服務(wù)的靜態(tài)依賴分析、服務(wù)的健康狀態(tài)、服務(wù)的流量管理、路由及安全策略、服務(wù)的 SLA 以及最首要的 metrics 統(tǒng)計(jì)數(shù)據(jù)。

?Spring Cloud 組件依賴版本

官網(wǎng)文檔:https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E

本文參考使用組件依賴如下

?Nacos部署

下載:https://github.com/alibaba/nacos/releases

運(yùn)行bin下的startup.cmd/startup.sh

Nacos2.* 默認(rèn)改為集群運(yùn)行了,可以只用-m指令指定啟動(dòng)方式。

Windows

rem 單機(jī)啟動(dòng)
startup.cmd -m standalone
rem 集群?jiǎn)?dòng)
startup.cmd -m cluster

?訪問(wèn)Nacos平臺(tái)

默認(rèn)賬密:nacos/nacos

??Nacos服務(wù)注冊(cè)、微服務(wù)訪問(wèn)、負(fù)載均衡實(shí)現(xiàn)

創(chuàng)建微服務(wù)項(xiàng)目,項(xiàng)目結(jié)構(gòu)如下

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

    <groupId>com.ber</groupId>
    <artifactId>SpringCloud</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/>
    </parent>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <java.version>1.8</java.version>
        <spring-boot.version>2.4.2</spring-boot.version>
        <spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
        <spring-cloud.version>2020.0.1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <modules>
        <module>ber-nacos</module>
    </modules>

</project>

nacos-consumer微服務(wù)創(chuàng)建

nacos-comsumer 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>ber-nacos</artifactId>
        <groupId>com.ber</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>nacos-consumer</artifactId>
    <description>服務(wù)消費(fèi)</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!-- 負(fù)載均衡 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-loadbalancer</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>


</project>

application.yml

spring.application.name=nacos-consumer

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

server.port=8891

啟動(dòng)類
記得在類上加上注解@EnableDiscoveryClient,RestTemplate屬性注入增加 @LoadBalanced 注解(負(fù)載均衡)

package com.ber.nacos.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * @author: ber
 * @date: 2022/6/25 0025 9:30
 * -------------------------------
 * Github:https://github.com/berbai
 * Blog:https://blog.csdn.net/Ber_Bai
 */
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerApplication {

    @Bean
    @LoadBalanced
    RestTemplate initRestTemplate() {
        return new RestTemplate();
    }


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

創(chuàng)建controller訪問(wèn)方法

package com.ber.nacos.consumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * @author: ber
 * @date: 2022/6/25 0025 10:58
 * -------------------------------
 * Github:https://github.com/berbai
 * Blog:https://blog.csdn.net/Ber_Bai
 */
@RestController
public class MsgController {

    private final RestTemplate restTemplate;

    @Autowired
    public MsgController(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    /**
     * 獲取消息
     *
     * @param msgStr 消息
     * @return
     */
    @GetMapping("/queryMsg/{msgStr}")
    public String getMsg(@PathVariable(value = "msgStr") String msgStr) {
        return restTemplate.getForObject("http://nacos-provider/getMsg/" + msgStr, String.class);
    }
}

?nacos-provider微服務(wù)創(chuàng)建

nacos-provider 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>ber-nacos</artifactId>
        <groupId>com.ber</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>
    <artifactId>nacos-provider</artifactId>
    <description>服務(wù)提供</description>

    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>
</project>

application.properties

server.port=8892

spring.application.name=nacos-provider

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

啟動(dòng)類

package com.ber.nacos.provider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @author: ber
 * @date: 2022/6/25 0025 14:03
 * -------------------------------
 * Github:https://github.com/berbai
 * Blog:https://blog.csdn.net/Ber_Bai
 */
@EnableDiscoveryClient
@SpringBootApplication
public class NacosProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(NacosProviderApplication.class, args);
    }
}

注解@EnableDiscoveryClient不要忘記加上。

創(chuàng)建controller方法,被consumer訪問(wèn)的接口

package com.ber.nacos.provider.controller;

import com.ber.nacos.provider.service.MsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: ber
 * @date: 2022/6/25 0025 14:06
 * -------------------------------
 * Github:https://github.com/berbai
 * Blog:https://blog.csdn.net/Ber_Bai
 */
@RestController
public class MsgController {
    @Autowired
    MsgService msgService;

    @GetMapping("getMsg/{msgStr}")
    public String getMsg(@PathVariable(value = "msgStr") String msgStr) {
        String res = "C服務(wù)";
        System.out.println(res + " 調(diào)用"+msgService.queryCount()+"次");
        msgService.addCount();
        return res + " 調(diào)用" + msgService.queryCount() + "次 Get " + msgStr;
    }
}

另外創(chuàng)建一個(gè)實(shí)體類記錄consumer微服務(wù)訪問(wèn)provider微服務(wù)的次數(shù)

package com.ber.nacos.provider.entity;

import org.springframework.stereotype.Component;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author: ber
 * @date: 2022/6/25 0025 14:09
 * -------------------------------
 * Github:https://github.com/berbai
 * Blog:https://blog.csdn.net/Ber_Bai
 */
@Component
public class CountLog {
    private AtomicInteger atomicInteger;

    public CountLog(){
        this.atomicInteger = new AtomicInteger();
    }
    public AtomicInteger getAtomicInteger() {
        return atomicInteger;
    }

    public void setAtomicInteger(AtomicInteger atomicInteger) {
        this.atomicInteger = atomicInteger;
    }
}

創(chuàng)建操作訪問(wèn)次數(shù)的service方法

package com.ber.nacos.provider.service;

import com.ber.nacos.provider.entity.CountLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: ber
 * @date: 2022/6/25 0025 14:13
 * -------------------------------
 * Github:https://github.com/berbai
 * Blog:https://blog.csdn.net/Ber_Bai
 */
@Service
public class MsgService {

    @Autowired
    CountLog countLog;

    public Integer queryCount() {
        return countLog.getAtomicInteger().get();
    }

    public void addCount() {
        countLog.getAtomicInteger().addAndGet(1);
    }
}

??測(cè)試

啟動(dòng)一個(gè)consumer和一個(gè)provider微服務(wù),訪問(wèn)http://localhost:8848/nacos,使用 nacos/nacos 登陸后,可以發(fā)現(xiàn)服務(wù)列表中,兩個(gè)微服務(wù)已經(jīng)注冊(cè),如下圖所示。

訪問(wèn)http://localhost:8891/queryMsg/hello,即consumer的接口,可以得到如下圖所示的界面

再額外啟動(dòng)兩個(gè)provider微服務(wù),測(cè)試nacos的負(fù)載均衡能力,這里我修改了provider端口和接口返回的數(shù)據(jù)以標(biāo)明不同的provider微服務(wù)。通過(guò)nacos控制平臺(tái)可以看到三個(gè)provider微服務(wù)已經(jīng)注冊(cè)上去,如下圖所示。

 

再次請(qǐng)求http://localhost:8891/queryMsg/hello,可以發(fā)現(xiàn)每次請(qǐng)求接口,三個(gè)provider接口的內(nèi)容會(huì)順序出現(xiàn),如下圖所示。說(shuō)明nacos默認(rèn)的負(fù)載均衡方式是輪詢

通過(guò)nacos修改其中一個(gè)provider的權(quán)重,修改參考下圖

經(jīng)過(guò)測(cè)試發(fā)現(xiàn),負(fù)載策略還是輪詢方式,本次修改無(wú)效。

通過(guò)翻閱spring cloud alibaba源碼,發(fā)現(xiàn)nacos實(shí)現(xiàn)了ribbon

修改consumer的啟動(dòng)類,如下所示

package com.ber.nacos.provider.service;

import com.ber.nacos.provider.entity.CountLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: ber
 * @date: 2022/6/25 0025 14:13
 * -------------------------------
 * Github:https://github.com/berbai
 * Blog:https://blog.csdn.net/Ber_Bai
 */
@Service
public class MsgService {

    @Autowired
    CountLog countLog;

    public Integer queryCount() {
        return countLog.getAtomicInteger().get();
    }

    public void addCount() {
        countLog.getAtomicInteger().addAndGet(1);
    }
}

重啟consumer后,設(shè)置的權(quán)重已經(jīng)生效。

到此這篇關(guān)于Spring Cloud Alibaba:Nacos服務(wù)治理平臺(tái),服務(wù)注冊(cè)、RestTemplate實(shí)現(xiàn)微服務(wù)之間訪問(wèn),負(fù)載均衡訪問(wèn)的文章就介紹到這了,更多相關(guān)Spring Cloud Alibaba Nacos服務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

夹江县| 濮阳市| 沁水县| 沈丘县| 临城县| 汝南县| 永仁县| 天津市| 西藏| 青州市| 新昌县| 理塘县| 白银市| 丹东市| 交口县| 吉安市| 酒泉市| 土默特右旗| 屯留县| 张北县| 昌平区| 遂川县| 上林县| 余庆县| 东城区| 新安县| 商南县| 马边| 罗平县| 博兴县| 揭西县| 明溪县| 扬州市| 金阳县| 琼海市| 海晏县| 牡丹江市| 红安县| 扶余县| 宣威市| 三河市|