04 nacos基于dubbo微服务

本文档介绍了如何基于Nacos搭建Dubbo微服务体系,包括启动Nacos服务器,创建服务接口定义工程service-api,实现服务注册,以及在nacos-restful-consumer中实现远程调用。在服务注册过程中,接口被抽离到独立工程,并在启动时可能出现需要添加spring-boot-starter-web坐标的问题。服务发现环节讲解了调用方的配置和调用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

此架构同时提供RESTful和Dubbo接口服务,应用层对前端提供RESTful接口,RESTful是互联网通用的轻量级交互
协议,方便前端接入系统;微服务层向应用层提供Dubbo接口,Dubbo接口基于RPC通信协议速度更快。

项目结构
service对外暴露dubbo协议的接口,考虑远程接口可能 会被其它多个服务调用,这里将service的接口单独抽取
出api工程
service-api:存放接口,独立成一个工程方便被其它服务工程依赖。
service-server:存放接口实现,即dubbo服务的实现部分。(调用service-api)
nacos-restful-consumer:实现远程调用(调用service-service)

启动nacos

访问正常 http://localhost:8848/nacos

父工程

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.itheima.nacos</groupId>
    <artifactId>nacos-discovery</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.1.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>
接口服务(service-api)
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>nacos-dubbo-service</artifactId>
        <groupId>li.chen.com</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>serivce-api</artifactId>


</project>
ServiceApi文件
package li.chen.com.microservice.service.api;

public interface ServiceApi {
    public String dubboService();
}

服务注册

调用service-api

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>nacos-dubbo-service</artifactId>
        <groupId>li.chen.com</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>serivce-service</artifactId>
    <dependencies>
        <dependency>
            <groupId>li.chen.com</groupId>
            <artifactId>serivce-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>


</project>
bootstrap.yml
server:
  port: 56040 #启动端口 命令行注入

spring:
  application:
    name: dubbo-service
  main:
    allow-bean-definition-overriding: true # Spring Boot 2.1 需要设定
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
dubbo:
  scan:
    # dubbo服务扫描基准包
    base-packages: li.chen.com.microservice.serivce.service
  protocol:
    # dubbo 协议
    name: dubbo
    # dubbo 协议端口
    port: 20891
  registry:
    address: nacos://127.0.0.1:8848
  application:
    qos-enable: false #dubbo运维服务是否开启
  consumer:
    check: false  #启动时就否检查依赖的服务
ServiceApiImpl 文件
package li.chen.com.microservice.serivce.service;

import li.chen.com.microservice.service.api.ServiceApi;
import org.apache.dubbo.config.annotation.Service;

/**
 * 实现service-api模块的ServiceApi接口
 */
@Service   //dubbo的service注解 org.apache.dubbo.config.annotation.Service
public class ServiceApiImpl implements ServiceApi {
    @Override
    public String dubboService() {
        return "dubboService";
    }
}

启动类
package li.chen.com.microservice.serivce;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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


访问nacos页面

在这里插入图片描述

注:启动时如下错误:在pom.xml文件增加spring-boot-starter-web坐标。
在这里插入图片描述

服务发现(远程调用)

调用service-service

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>nacos-dubbo-service</artifactId>
        <groupId>li.chen.com</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>serivce-service</artifactId>
    <dependencies>
        <dependency>
            <groupId>li.chen.com</groupId>
            <artifactId>serivce-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>


</project>
application.yml
server:
  port: 56020

# 服务生产方地址
#provider:
#  address: 192.168.58.1:56010

spring:
  application:
    name: nacos-restful-consumer #服务名
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848  #服务发现中心地址

nacos-restful-provider:
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
ServiceApiImpl 文件
package li.chen.com.nacos.controller;

import li.chen.com.microservice.service.api.ServiceApi;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;

@RestController
@RequestMapping("/RestConsumerController")
public class RestConsumerController {

    @Reference  //import org.apache.dubbo.config.annotation.Reference;
    ServiceApi serviceApi;

    @GetMapping("/service-api/service")
    public String serviceApi(){
        //远程调用
        String providerResult = serviceApi.dubboService();
        return "consumer dubbo invode|"+providerResult;
    }

}

启动类
package li.chen.com.microservice.serivce;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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


远程调用步骤

  • 引入调用方坐标
<dependency>
<groupId>com.itheima.nacos</groupId>
<artifactId>service‐api</artifactId>
<version>1.0‐SNAPSHOT</version>
</dependency>
  • 注入调用方,直接调用方法
 	@Reference  //import org.apache.dubbo.config.annotation.Reference;
    ServiceApi serviceApi;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

岿然如故

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值