创建服务消费者

服务消费者工程项目

创建一个名为 spring-cloud-alibaba-dubbo-consumer 的模块,其父工程为 spring-cloud-alibaba-dubbo-examples

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>
    <parent>
        <groupId>com.funtl</groupId>
        <artifactId>spring-cloud-alibaba-dubbo-examples</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <artifactId>spring-cloud-alibaba-dubbo-consumer</artifactId>
    <packaging>pom</packaging>
    <url>http://www.funtl.com</url>

    <modules>
        <module>spring-cloud-alibaba-dubbo-consumer-service</module>
    </modules>
</project>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

创建服务消费者模块

创建一个名为 spring-cloud-alibaba-dubbo-consumer-service 的模块,其父工程为 spring-cloud-alibaba-dubbo-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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.funtl</groupId>
        <artifactId>spring-cloud-alibaba-dubbo-consumer</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <artifactId>spring-cloud-alibaba-dubbo-consumer-service</artifactId>
    <url>http://www.funtl.com</url>

    <dependencies>
        <dependency>
            <groupId>com.funtl</groupId>
            <artifactId>spring-cloud-alibaba-dubbo-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>com.funtl</groupId>
            <artifactId>spring-cloud-alibaba-dubbo-provider-api</artifactId>
            <version>${project.parent.version}</version>
        </dependency>
    </dependencies>

</project>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

创建服务消费者实现

package com.funtl.alibaba.dubbo.consumer.service;

import com.funtl.alibaba.dubbo.provider.service.FooService;
import com.funtl.alibaba.dubbo.provider.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestService {

    @Autowired
    private ProviderService providerService;

    @Autowired
    private FooService fooService;

    @GetMapping("/test")
    public String test() {
        return providerService.hi();
    }

    @GetMapping("/test/foo")
    public String testFoo() {
        return fooService.foo();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

Application

注意此时需要使用 @EnableFeignClients(basePackages = "com.funtl.alibaba.dubbo") 做包路径扫描,这样才能使用 @Autowired 方式将 Provider API 项目中的接口注入进来

package com.funtl.alibaba.dubbo.consumer;

import com.alibaba.dubbo.config.ProtocolConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients(basePackages = "com.funtl.alibaba.dubbo")
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class ,args);
    }

    @Bean
    public ProtocolConfig protocolConfig() {
        ProtocolConfig protocolConfig = new ProtocolConfig();
        protocolConfig.setPort(30880);
        protocolConfig.setName("dubbo");
        return protocolConfig;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

application.yml

spring:
  main:
    allow-bean-definition-overriding: true
  application:
    name: consumer
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.10.146:8848

server:
  port: 10201

dubbo:
  application:
    name: consumer
    qos-enable: false
  registry:
    address: nacos://192.168.10.146:8848
  scan:
    basePackages: com.funtl.alibaba.dubbo.consumer.service

management:
  endpoints:
    web:
      exposure:
        include: "*"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

运行测试

访问 Nacos 可以看到同时注册了 Dubbo 和 Http 服务

上次更新: 2019-3-16 10:19:40