美文网首页
dubbo spring cloud 之 provider

dubbo spring cloud 之 provider

作者: 某某程序员_ | 来源:发表于2019-04-28 14:38 被阅读0次

    描述

    dubbo服务提供方

    maven依赖

    <dependencies>
            <!-- dubbo API -->
            <dependency>
                <groupId>com.zm.demo.dubbo</groupId>
                <artifactId>dubbo-sc-api</artifactId>
                <version>${project.version}</version>
            </dependency>
    
            <!-- Spring Boot dependencies -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-actuator</artifactId>
            </dependency>
    
            <!-- Dubbo Spring Cloud Starter -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-dubbo</artifactId>
            </dependency>
    
            <!-- Spring Cloud Nacos Service Discovery -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            </dependency>
        </dependencies>
    

    配置:

    dubbo:
      scan:
        # dubbo 服务扫描基准包
        base-packages: com.zm.demo.dubbo.sc.provider
      protocol:
        # dubbo 协议
        name: dubbo
        # dubbo 协议端口( -1 表示自增端口,从 20880 开始)
        port: -1
      registry:
        # 挂载到 Spring Cloud 注册中心
        address: spring-cloud://localhost
    
    spring:
      application:
        # Dubbo 应用名称
        name: demo-dubbo-sc-provider
      main:
        # Spring Boot 2.1 需要设定
        allow-bean-definition-overriding: true
      cloud:
        nacos:
          # Nacos 服务发现与注册配置
          discovery:
            server-addr: 127.0.0.1:8848
    

    服务实现

    package com.zm.demo.dubbo.sc.provider;
    
    import com.zm.demo.dubbo.sc.api.UserService;
    import org.apache.dubbo.config.annotation.Service;
    
    /**
     * @org.apache.dubbo.config.annotation.Service 是 Dubbo 服务注解,仅声明该 Java 服务(本地)实现为 Dubbo 服务
     * @author zoum
     * @create 2019/4/26 11:25
     */
    @Service
    public class UserServiceImpl implements UserService {
    
        public String hello(String userName) {
            return "hello " + userName;
        }
    }
    
    

    服务启动

    package com.zm.demo.dubbo.sc.provider;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    /**
     * @author zoum
     * @create 2019/4/26 11:38
     */
    
    @EnableDiscoveryClient
    @EnableAutoConfiguration
    public class DubboSpringCloudServerBootstrap {
    
        public static void main(String[] args){
            SpringApplication.run(DubboSpringCloudServerBootstrap.class);
        }
    }
    
    

    源码

    https://gitee.com/love2014/demo/tree/master/demo-dubbo/dubbo-sc-provider

    相关文章

      网友评论

          本文标题:dubbo spring cloud 之 provider

          本文链接:https://www.haomeiwen.com/subject/cbldnqtx.html