springboot整合dubbo(4)-消费者

1.导入依赖

 父工程

<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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/>
    </parent>

    <groupId>com.xt</groupId>
    <artifactId>springboot-dubbo-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>springboot-dubbo-demo</name>
    <url>http://maven.apache.org</url>
    <modules>
        <module>springboot-dubbo-provider</module>
        <module>springboo-dubbo-consumer</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <dubbo.version>2.7.3</dubbo.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!--dubbo依赖-->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>${dubbo.version}</version>
            </dependency>

            <!--zookeeper注册中心客户端引入 curator客户端-->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-dependencies-zookeeper</artifactId>
                <version>${dubbo.version}</version>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

 

消费者

<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.xt</groupId>
        <artifactId>springboot-dubbo-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>springboo-dubbo-consumer</artifactId>
    <packaging>jar</packaging>

    <name>springboo-dubbo-consumer</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.xt</groupId>
            <artifactId>springboot-dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
        </dependency>
    </dependencies>
</project>

 

2.application.properties配置

#tomcat端口号
server.port=8081
spring.datasource.url=jdbc:mysql://localhost:3306/study2020
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
mybatis.configuration.map-underscore-to-camel-case=true
spring.resources.static-locations:classpath:/static/,classpath:/templates/
#mybatis 配置
# 配置映射文件加载
mybatis.mapper-locations=classpath*:mappers/*.xml
# 实体类通过别名使用
#mybatis.type-aliases-package=com.example.springboot.mybatis.entity
spring.devtools.restart.enabled=true
#dubbo相关配置
#服务名称
dubbo.application.name=springboot-meeting-service
#注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181

 

application.yml配置

server:
  port: 8002
dubbo:
  application:
    name: dubbo-consumer
  ##配置注册中心
  registry:
    address: zookeeper://127.0.0.1:2181
  ##注册中心采用的协议 端口号
  protocol:
    name: dubbo
    port: 20880

 

3.实现类上添加@Reference注解(注册中心中引用服务),相当于consumer.xml中的配置:

<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="userService" interface="com.atxuetao.service.UserService" />

 

package com.xt.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xt.mapper.MeetingPubMapper;
import com.xt.pojo.MeetingPub;
import com.xt.pojo.User;
import com.xt.service.MeetingPubService;
import com.xt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MeetingPubServiceImpl implements MeetingPubService {

    @Autowired
    MeetingPubMapper meetingPubMapper;

    @Reference
    UserService userService;
    @Override
    public MeetingPub selectByPrimaryKey(Integer pCode) {
        MeetingPub meetingPub = meetingPubMapper.selectByPrimaryKey(pCode);
        //调用服务完成服务消费功能
        User user = userService.selectByPrimaryKey(meetingPub.getUid());
        meetingPub.setUser(user);
        return meetingPub;
    }
}

  

4.在启动类上添加@EnableDubbo注解

package com.xt;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.xt.mapper")
@EnableDubbo
public class SpringbootMeetingServiceApplication {

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

}

 

posted @ 2021-06-28 22:59  Mr_sven  阅读(331)  评论(0编辑  收藏  举报