ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Dubbo SpringCloud(一)服务提供者和服务消费者

2022-07-10 22:04:27  阅读:175  来源: 互联网

标签:dubbo Dubbo spring SpringCloud nacos moe 服务提供者 org cloud


Dubbo SpringCloud

先定义父工程,springboot版本为idea自己生成的2.3.7.RELEASE

springcloud版本为Hoxton.SR12

springcloud alibaba版本为2.2.2.RELEASE

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>
    <modules>
        <module>dubbo-api</module>
        <module>common</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.7.RELEASE</version>
    </parent>

    <groupId>moe.macrohard</groupId>
    <artifactId>springcloud-dubbo-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-cloud.version>Hoxton.SR12</spring-cloud.version>
        <spring-cloud-alibaba.version>2.2.2.RELEASE</spring-cloud-alibaba.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <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>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <addResources>true</addResources>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

1. 公共接口和公共POJO

dubbo服务之间调用通过接口类名实现,因此公共的服务接口全类名要相同

声明一个接口,其全类名为moe.macrohard.common.service.ProviderService

package moe.macrohard.common.service;

public interface ProviderService {
	String getProviderData();
}

2. 服务提供者

  1. pom依赖

    java依赖如下,其中common为之前的公共接口和pojo包

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- nacos依赖 -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <!-- dubbo依赖 -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-dubbo</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>moe.macrohard</groupId>
        <artifactId>common</artifactId>
        <version>1.0-SNAPSHOT</version>
        <scope>compile</scope>
    </dependency>
    
  2. yaml配置

    # 应用名称
    spring:
      application:
        name: dubbo-api
      main:
        # Spring Boot 2.1
        allow-bean-definition-overriding: true
    # Nacos帮助文档: https://nacos.io/zh-cn/docs/concepts.html
    # Nacos认证信息
      cloud:
        nacos:
          discovery:
            username: nacos
            password: nacos
    # Nacos 服务发现与注册配置,其中子属性 server-addr 指定 Nacos 服务器主机和端口
            server-addr: localhost:8848
    # 注册到 nacos 的指定 namespace,默认为 public
    #spring.cloud.nacos.discovery.namespace=public
    # dubbo 协议
    dubbo:
      protocol:
        name: dubbo
    # dubbo 协议端口( -1 表示自增端口,从 20880 开始)
        port: -1
    # dubbo 服务扫描基准包
      scan:
        base-packages: moe.macrohard.dubboapi
      registry:
        # 配置注册中心
        address: nacos://localhost:8848
    # 应用服务 WEB 访问端口
    server:
      port: 8989
    
  3. 实现服务接口

    创建实现类,实现类要使用@DubboService注解

    package moe.macrohard.dubboapi.service.impl;
    
    import moe.macrohard.common.service.ProviderService;
    import org.apache.dubbo.config.annotation.DubboService;
    
    @DubboService
    public class ProviderServiceImpl implements ProviderService {
    	@Override
    	public String getProviderData() {
    		return "Hello";
    	}
    }
    
    
  4. nacos配置类

    表示启用nacos服务

    /*
     * Licensed to the Apache Software Foundation (ASF) under one or more
     * contributor license agreements.  See the NOTICE file distributed with
     * this work for additional information regarding copyright ownership.
     * The ASF licenses this file to You under the Apache License, Version 2.0
     * (the "License"); you may not use this file except in compliance with
     * the License.  You may obtain a copy of the License at
     *
     *     http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    package moe.macrohard.dubboapi.nacosdiscovery;
    
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
     */
    @EnableDiscoveryClient
    @Configuration
    public class NacosDiscoveryConfiguration {
    }
    
    

3.服务消费者

  1. pom配置

    服务消费者同样需要公共接口包

    <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>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-dubbo</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>moe.macrohard</groupId>
        <artifactId>common</artifactId>
        <version>1.0-SNAPSHOT</version>
        <scope>compile</scope>
    </dependency>
    
  2. yaml配置

    # 应用名称
    spring:
      application:
        name: consumer
      # Nacos帮助文档: https://nacos.io/zh-cn/docs/concepts.html
      # Nacos认证信息
      cloud:
        nacos:
          discovery:
            username: nacos
            password: nacos
            # Nacos 服务发现与注册配置,其中子属性 server-addr 指定 Nacos 服务器主机和端口
            server-addr: localhost:8848
    # 注册到 nacos 的指定 namespace,默认为 public
    #spring.cloud.nacos.discovery.namespace=public
    # dubbo 协议
    dubbo:
      protocol:
        id: dubbo
        name: dubbo
        # dubbo 协议端口( -1 表示自增端口,从 20880 开始)
        port: -1
      # Dubbo 消费端订阅服务端的应用名,多个服务提供者用逗号分隔
      # 表示要订阅服务的服务名,能够配置'*',
      # 表明订阅全部服务,不推荐使用。若需订阅多应用,使用 "," 分割
      cloud:
        subscribed-services: dubbo-api
      # dubbo 服务扫描基准包
      scan:
        base-packages: moe.macrohard.consumer
      registry:
        # 配置注册中心
        address: nacos://localhost:8848
    # 应用服务 WEB 访问端口
    server:
      port: 8990
    
    
  3. 定义消费者的接口以及实现类

    消费者接口

    package moe.macrohard.consumer.service;
    
    public interface ConsumerService {
    	String getProviderData();
    }
    
    

    接口实现类

    注入服务提供者接口使用的注解是@DubboReference

    在消费者服务中调用注入的提供者服务接口的方法即可

    package moe.macrohard.consumer.service.impl;
    
    import moe.macrohard.common.service.ProviderService;
    import moe.macrohard.consumer.service.ConsumerService;
    import org.apache.dubbo.config.annotation.DubboReference;
    import org.springframework.stereotype.Service;
    
    @Service
    public class ConsumerServiceImpl implements ConsumerService {
    	@DubboReference
    	ProviderService providerService;
    
    	@Override
    	public String getProviderData() {
    		return providerService.getProviderData();
    	}
    }
    
  4. nacos配置类

    和服务提供者的nacos配置类一样

  5. 消费者的controller

    调用消费者服务即可

    package moe.macrohard.consumer.controller;
    
    import moe.macrohard.consumer.service.ConsumerService;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.annotation.Resource;
    
    @RestController
    public class ConsumerController {
    	@Resource
    	private ConsumerService consumerService;
    
    	@GetMapping("/")
    	public String hello() {
    		return consumerService.getProviderData();
    	}
    }
    

标签:dubbo,Dubbo,spring,SpringCloud,nacos,moe,服务提供者,org,cloud
来源: https://www.cnblogs.com/poifa/p/16464185.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有