美文网首页
dubbo spring cloud 之 consumer

dubbo spring cloud 之 consumer

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

    描述

    dubbo服务调用方

    maven依赖

    <!-- 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-starter-web</artifactId>
            </dependency>
    
            <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>
    

    配置

    dubbo:
      registry:
        # 挂载到 Spring Cloud 注册中心
        address: spring-cloud://localhost
      cloud:
        subscribed-services: demo-dubbo-sc-provider #订阅provider服务
    
    spring:
      application:
        # Dubbo 应用名称
        name: demo-dubbo-sc-consumer
      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.consumer;
    
    import com.zm.demo.dubbo.sc.api.UserService;
    import org.apache.dubbo.config.annotation.Reference;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @author zoum
     * @create 2019/4/28 13:15
     */
    @RestController
    public class UserController {
    
        @Reference
        private UserService userService;
    
        @GetMapping("/hello")
        public String hello(String userName) {
            return userService.hello(userName);
        }
    
    }
    
    

    应用启动类

    package com.zm.demo.dubbo.sc.consumer;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    @EnableDiscoveryClient
    @EnableAutoConfiguration
    public class DubboSpringCloudClientBootstrap {
    
        public static void main(String[] args) {
            SpringApplication.run(DubboSpringCloudClientBootstrap.class);
        }
    }
    
    

    测试

    请求:
    http://127.0.0.1:8080/hello?userName=%E5%B0%8F%E6%98%8E1

    结果:
    hello 小明1

    源码

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

    相关文章

      网友评论

          本文标题:dubbo spring cloud 之 consumer

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