SpringCloud入门(二)nacos + dubbo

梦想不抛弃苦心追求的人,只要不停止追求,你们会沐浴在梦想的光辉之中。再美好的梦想与目标,再完美的计划和方案,如果不能尽快在行动中落实,最终只能是纸上谈兵,空想一番。只要瞄准了大方向,坚持不懈地做下去,才能够扫除挡在梦想前面的障碍,实现美好的人生蓝图。SpringCloud入门(二)nacos + dubbo,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

前言

好久没正经更新这个系列了,还是要续一下的…

设计

springboot + nacos + dubbo。本集只更新最基础最简单的用法。项目结构如下:
项目结构

父工程

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>org.example</groupId>
    <artifactId>learnCloud07</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>nacos-test01</module>
        <module>nacos-dubbo-consume</module>
        <module>nacos-middle-interface</module>
    </modules>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <spring-cloud.version>HOXTON.SR8</spring-cloud.version>
        <spring-boot.version>2.3.2.RELEASE</spring-boot.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.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>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.16</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.75</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

服务接口约定

在父项目中新建一个模块,什么依赖都不需要,直接

package com.xu.rpcInterface;

public interface HelloService {

    /**
     * hello!
     * @return
     */
    String sayHello();
}

服务提供方

pom.xml

<dependencies>
        <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.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.5.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.8</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.11</version>
        </dependency>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>nacos-middle-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>

    </dependencies>

配置文件

server:
  port: 8001
spring:
  application:
    name: nacos-provider
  cloud:
    nacos:
      discovery:
        server-addr: {your nacos ip}
        username: nacos
        password: nacos
dubbo:
  application:
    name: ${spring.application.name}
  registry:
    address: nacos://${spring.cloud.nacos.discovery.server-addr}:8848
  protocol:
    name: dubbo # 指定通信协议
    port: 20880 # 通信端口  这里指的是与消费者间的通信协议与端口
  provider:
    timeout: 10000 # 配置全局调用服务超时时间
    retries: 3 # 重试3次
    delay: -1

启动类

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubbo
public class Provider01 {

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

服务提供

import com.xu.rpcInterface.HelloService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

@Component
@DubboService
public class HelloServiceImpl implements HelloService {

    @Override
    public String sayHello() {
        return "hello dubbo";
    }
}

启动项目,如果正确的话应该能在nacos的管理页面看到。

服务消费方

pom.xml

和生产者一样的

配置文件

server:
  port: 8002
spring:
  application:
    name: nacos-consumer
  cloud:
    nacos:
      discovery:
        server-addr: {your nacos ip}
        username: nacos
        password: nacos

dubbo:
  # 配置服务信息
  application:
    name: dubbo-consumer
    # 禁用QOS同一台机器可能会有端口冲突现象
    qos-enable: false
    qos-accept-foreign-ip: false
  # 配置注册中心
  registry:
    address: nacos://${spring.cloud.nacos.discovery.server-addr}:8848
  # 设置超时时间
  consumer:
    timeout: 10000

启动类

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubbo
public class NacosDubboConsume {

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

服务调用

import com.xu.rpcInterface.HelloService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

@Service
public class RpcCallServiceImpl {

    @DubboReference
    private HelloService helloService;

    public String sayHello() {
        return helloService.sayHello();
    }
}

控制器

import com.xu.service.RpcCallServiceImpl;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class HelloController {

    @Resource
    private RpcCallServiceImpl rpcCallService;

    @GetMapping("/hello")
    public String sayHelloFunc() {
        return rpcCallService.sayHello();
    }
}

然后

curl http://localhost:8002/hello

就能得到结果,超简单的。更多的,关于负载均衡,熔断之类的,自己探索啦~
首发于csdn,作者 亭台烟雨中

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/153518.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!