dubbo spring cloud 之 consumer

描述

dubbo服务调用方

maven依赖


        
            com.zm.demo.dubbo
            dubbo-sc-api
            ${project.version}
        

        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-actuator
        

        
        
            org.springframework.cloud
            spring-cloud-starter-dubbo
        

        
        
            org.springframework.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        

配置

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)