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

Spring Cloud Alibaba和Dubbo融合實(shí)現(xiàn)

 更新時(shí)間:2020年04月14日 10:40:03   作者:西街青年  
這篇文章主要介紹了Spring Cloud Alibaba和Dubbo融合實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

服務(wù)提供者

創(chuàng)建一個(gè)名為 hello-dubbo-nacos-provider 的服務(wù)提供者項(xiàng)目

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>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <groupId>com.antoniopeng</groupId>
  <artifactId>hello-dubbo-nacos-provider</artifactId>
  <packaging>pom</packaging>
  
  <modules>
    <module>hello-dubbo-nacos-provider-api</module>
    <module>hello-dubbo-nacos-provider-service</module>
  </modules>
</project>

該項(xiàng)目下有兩個(gè)子模塊,分別是 hello-dubbo-nacos-provider-api 和 hello-dubbo-nacos-provider-service,前者用于定義接口,后者用于實(shí)現(xiàn)接口。

服務(wù)提供者接口模塊

在服務(wù)提供者項(xiàng)目下創(chuàng)建一個(gè)名為 hello-dubbo-nacos-provider-api 的模塊, 該項(xiàng)目模塊只負(fù)責(zé) 定義接口

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.antoniopeng</groupId>
    <artifactId>hello-dubbo-nacos-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>

  <artifactId>hello-dubbo-nacos-provider-api</artifactId>
  <packaging>jar</packaging>
</project>

定義一個(gè)接口

public interface EchoService {
  String echo(String string);
}

服務(wù)提供者接口實(shí)現(xiàn)模塊

創(chuàng)建名為 hello-dubbo-nacos-provider-service 服務(wù)提供者接口的實(shí)現(xiàn)模塊,用于實(shí)現(xiàn)在接口模塊中定義的接口。

引入依賴

在 pom.xml 中主要添加以下依賴

<!-- Nacos And Dubbo-->
<dependency>
	<groupId>org.apache.dubbo</groupId>
	<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
	<groupId>org.apache.dubbo</groupId>
	<artifactId>dubbo</artifactId>
</dependency>
<dependency>
	<groupId>org.apache.dubbo</groupId>
	<artifactId>dubbo-serialization-kryo</artifactId>
</dependency>
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>dubbo-registry-nacos</artifactId>
</dependency>
<dependency>
	<groupId>com.alibaba.nacos</groupId>
	<artifactId>nacos-client</artifactId>
</dependency>
<dependency>
	<groupId>com.alibaba.spring</groupId>
	<artifactId>spring-context-support</artifactId>
</dependency>

<!-- 依賴接口模塊,用于實(shí)現(xiàn)接口 -->
<dependency>
	<groupId>com.antoniopeng</groupId>
	<artifactId>hello-dubbo-nacos-provider-api</artifactId>
	<version>${project.parent.version}</version>
</dependency>

相關(guān)配置

在 application.yml 中加入相關(guān)配置

spring:
 application:
  name: dubbo-nacos-provider
 main:
  allow-bean-definition-overriding: true
dubbo:
 scan:
  # 接口掃描路徑
  base-packages: com.antoniopeng.hello.dubbo.nacos.provider.service
 protocol:
  name: dubbo
  # -1 代表自動(dòng)分配端口
  port: -1
  # 配置高速序列化規(guī)則
  serialization: kryo
 registry:
  # 服務(wù)注冊(cè)地址,也就是 Nacos 的服務(wù)器地址
  address: nacos://192.168.127.132:8848
 provider:
  # 配置負(fù)載均衡策略(輪詢)
  loadbalance: roundrobin

附:Duubo 負(fù)載均衡策略

  • random:隨機(jī)
  • roundrobin:輪詢
  • leastactive:最少活躍數(shù)
  • consistenthash:一致性 Hash

實(shí)現(xiàn)接口

通過(guò) org.apache.dubbo.config.annotation 包下的 @Service 注解將接口暴露出去

import com.antoniopeng.hello.dubbo.nacos.provider.api.EchoService;
import org.apache.dubbo.config.annotation.Service;

@Service(version = "1.0.0")
public class EchoServiceImpl implements EchoService {

  @Override
  public String echo(String string) {
    return "Echo Hello Dubbo " + string;
  }
}

注意:@Service 注解要注明 version 屬性

驗(yàn)證是否成功

啟動(dòng)項(xiàng)目,通過(guò)瀏覽器訪問(wèn)Nacos Server 網(wǎng)址 http://192.168.127.132:8848/nacos ,會(huì)發(fā)現(xiàn)有一個(gè)服務(wù)已經(jīng)注冊(cè)在服務(wù)列表中。

服務(wù)消費(fèi)者

創(chuàng)建一個(gè)名為 hello-dubbo-nacos-consumer 的服務(wù)消費(fèi)者項(xiàng)目

引入依賴

同樣在 pom.xml中添加以下主要依賴

<!-- Nacos And Dubbo -->
<dependency>
	<groupId>org.apache.dubbo</groupId>
	<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
	<groupId>org.apache.dubbo</groupId>
	<artifactId>dubbo-serialization-kryo</artifactId>
</dependency>
<dependency>
	<groupId>org.apache.dubbo</groupId>
	<artifactId>dubbo-spring-boot-actuator</artifactId>
</dependency>
<dependency>
	<groupId>org.apache.dubbo</groupId>
	<artifactId>dubbo</artifactId>
</dependency>
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>dubbo-registry-nacos</artifactId>
</dependency>
<dependency>
	<groupId>com.alibaba.nacos</groupId>
	<artifactId>nacos-client</artifactId>
</dependency>
<dependency>
	<groupId>com.alibaba.spring</groupId>
	<artifactId>spring-context-support</artifactId>
</dependency>

<!-- 依賴服務(wù)提供者接口模塊,用于調(diào)用接口 -->
<dependency>
	<groupId>com.antoniopeng</groupId>
	<artifactId>hello-dubbo-nacos-provider-api</artifactId>
	<version>${project.parent.version}</version>
</dependency>

相關(guān)配置

在 application.yml 中添加以下配置

spring:
 application:
  name: dubbo-nacos-consumer
 main:
  allow-bean-definition-overriding: true

dubbo:
 scan:
  # 配置 Controller 掃描路徑
  base-packages: com.antoniopeng.dubbo.nacos.consumer.controller
 protocol:
  name: dubbo
  port: -1
 registry:
  address: nacos://192.168.127.132:8848

server:
 port: 8080

# 服務(wù)監(jiān)控檢查
endpoints:
 dubbo:
  enabled: true
management:
 health:
  dubbo:
   status:
    defaults: memory
    extras: threadpool
 endpoints:
  web:
   exposure:
    include: "*"

Controller

通過(guò) org.apache.dubbo.config.annotation 包下的 @Reference 注解以 RPC 通信的方式調(diào)用服務(wù),而對(duì)外提供以 HTTP 通信的方式的 Restful API

import com.antoniopeng.dubbo.nacos.provider.api.EchoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class EchoController {

  @Reference(version = "1.0.0")
  private EchoService echoService;

  @GetMapping(value = "/echo/{string}")
  public String echo(@PathVariable String string) {
    return echoService.echo(string);
  }
}

驗(yàn)證是否成功

通過(guò)瀏覽器訪問(wèn) Nacos Server 網(wǎng)址 http:192.168.127.132:8848/nacos ,會(huì)發(fā)現(xiàn)又多了一個(gè)服務(wù)在服務(wù)列表中。

然后再訪問(wèn)服務(wù)消費(fèi)者對(duì)外提供的 RESTful API http://localhost:8080/echo/hi,瀏覽器會(huì)響應(yīng)以下內(nèi)容:

Echo Hello Dubbo hi

到此,實(shí)現(xiàn)了 Nacos 與 Dubbo 的融合。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java7和Java8中的ConcurrentHashMap原理解析

    Java7和Java8中的ConcurrentHashMap原理解析

    這篇文章主要介紹了Java7和Java8中的ConcurrentHashMap原理解析,對(duì)ConcurrentHashMap感興趣的讀者,一定要好好看一下
    2021-04-04
  • 圖解分析Javaweb進(jìn)程與線程

    圖解分析Javaweb進(jìn)程與線程

    這篇文章主要介紹了Javaweb進(jìn)程與線程的知識(shí),本篇文章通過(guò)簡(jiǎn)要的案例,講解了它的基礎(chǔ)原理與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2022-03-03
  • Spring Boot 3 集成 RabbitMQ 實(shí)踐指南(原理解析)

    Spring Boot 3 集成 RabbitMQ 實(shí)踐指南(原理解析

    本文介紹了SpringBoot 3集成RabbitMQ的實(shí)踐指南,涵蓋了RabbitMQ的核心原理、核心概念、高級(jí)特性、應(yīng)用場(chǎng)景、環(huán)境搭建、核心配置類、消息生產(chǎn)者、消息消費(fèi)者、接口控制器、監(jiān)控與運(yùn)維、最佳實(shí)踐以及常見(jiàn)問(wèn)題與解決方案等內(nèi)容,感興趣的朋友一起看看吧
    2025-02-02
  • java中的過(guò)濾器 Filter應(yīng)用小結(jié)

    java中的過(guò)濾器 Filter應(yīng)用小結(jié)

    文章主要介紹了Java Web中的過(guò)濾器(Filter)的基本概念、生命周期、配置和應(yīng)用,過(guò)濾器可以攔截請(qǐng)求和響應(yīng),用于執(zhí)行一些預(yù)處理或后處理操作,如設(shè)置編碼、校驗(yàn)用戶身份等,感興趣的朋友一起看看吧
    2025-03-03
  • springboot配置Jackson返回統(tǒng)一默認(rèn)值的實(shí)現(xiàn)示例

    springboot配置Jackson返回統(tǒng)一默認(rèn)值的實(shí)現(xiàn)示例

    在項(xiàng)目開(kāi)發(fā)中,我們返回的數(shù)據(jù)或者對(duì)象沒(méi)有的時(shí)候一般直接返回的null,那么如何返回統(tǒng)一默認(rèn)值,感興趣的可以了解一下
    2021-07-07
  • SpringMvc獲取請(qǐng)求頭請(qǐng)求體消息過(guò)程解析

    SpringMvc獲取請(qǐng)求頭請(qǐng)求體消息過(guò)程解析

    這篇文章主要介紹了SpringMvc獲取請(qǐng)求頭請(qǐng)求體消息過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Flowable流程引擎API與服務(wù)

    Flowable流程引擎API與服務(wù)

    這篇文章主要介紹了Flowable流程引擎API與服務(wù),引擎API是與Flowable交互的最常用手段,總?cè)肟邳c(diǎn)是ProcessEngine,使用ProcessEngine,可以獲得各種提供工作流或BPM方法的服務(wù),下面我們來(lái)詳細(xì)了解
    2023-10-10
  • java哈夫曼樹(shù)實(shí)例代碼

    java哈夫曼樹(shù)實(shí)例代碼

    這篇文章主要為大家介紹了java哈夫曼樹(shù)實(shí)例代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • java中@DateTimeFormat和@JsonFormat注解的使用

    java中@DateTimeFormat和@JsonFormat注解的使用

    本文主要介紹了java中@DateTimeFormat和@JsonFormat注解的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • Java用BigDecimal解決double類型相減時(shí)可能存在的誤差

    Java用BigDecimal解決double類型相減時(shí)可能存在的誤差

    這篇文章主要介紹了Java用BigDecimal解決double類型相減時(shí)可能存在的誤差,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05

最新評(píng)論

中阳县| 渝中区| 池州市| 永定县| 石泉县| 镇赉县| 垦利县| 洪江市| 修文县| 铜川市| 交城县| 南开区| 镇坪县| 普定县| 龙州县| 岑溪市| 临湘市| 綦江县| 阿拉善盟| 安吉县| 宁阳县| 新民市| 鲜城| 大方县| 马鞍山市| 山阴县| 六枝特区| 阿合奇县| 黄龙县| 抚顺县| 柳江县| 兴海县| 梓潼县| 明光市| 板桥市| 京山县| 石景山区| 鹰潭市| 金阳县| 五河县| 资兴市|