Dubbo(三) 服务消费者

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/u011414629/article/details/100179925

建立Dubbo服务消费者项目hello-dubbo-service-user-consumer

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.1.7.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.funtl</groupId>
   <artifactId>hello-dubbo-service-user-consumer</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>hello-dubbo-service-user-consumer</name>
   <description>Demo project for Spring Boot</description>

   <properties>
      <java.version>1.8</java.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-actuator</artifactId>
      </dependency>      

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>

      <dependency>
         <groupId>com.alibaba.boot</groupId>
         <artifactId>dubbo-spring-boot-starter</artifactId>
         <version>0.2.0</version>
      </dependency>

      <dependency>
         <groupId>com.funtl</groupId>
         <artifactId>hello-dubbo-service-user-api</artifactId>
         <version>1.0-SNAPSHOT</version>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
         <version>RELEASE</version>
      </dependency>
   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>

</project>

UserController

package com.funtl.hello.dubbo.service.user.consumer.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.funtl.hello.dubbo.service.user.api.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Reference(version="${user.service.version}")
    private UserService userService;

    @RequestMapping(value="hi", method= RequestMethod.GET)
    public String sayHi(){
        return userService.sayHi();
    }

}

启动类

package com.funtl.hello.dubbo.service.user.consumer;

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

@SpringBootApplication
public class HelloDubboServiceUserConsumerApplication {

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

}

配置文件

spring:
  application:
    name: hello-dubbo-service-user-consumer

server:
  port: 9090

user:
  service:
    version: 1.0.0

dubbo:
  scan:
    basePackages: com.funtl.hello.dubbo.service.user.consumer.controller
  application:
    id: hello-dubbo-service-user-consumer
    name: hello-dubbo-service-user-consumer
  protocol:
    id: dubbo
    name: dubbo
    port: 12345
    serialization: kryo
  registry:
    id: zookeeper
    address: zookeeper://192.168.25.132:2181?backup=192.168.25.132:2182,192.168.25.132:2183

猜你喜欢

转载自blog.csdn.net/u011414629/article/details/100179925