springboot dubbo demo

2021-01-07 16:29

阅读:554

标签:git   ack   引入   img   pac   beans   server   figure   ima   

1、代码结构

技术图片

2、整体 pom.xml

4.0.0dubbo-api-bootdubbo-movie-provider-bootdubbo-user-consumer-bootorg.springframework.boot
        spring-boot-starter-parent
        2.2.7.RELEASEcom.test
    dubbomicroservice-boot
    0.0.1-SNAPSHOTdubbomicroservice-bootDemo project for Spring Bootpom1.8org.springframework.boot
            spring-boot-starter
        org.springframework.boot
                spring-boot-maven-plugin
            

3、dubbo-api-boot

3.1 dubbo-api-boot 整体代码结构

技术图片

3.2 dubbo-api-boot pom.xml


        dubbomicroservice-boot
        com.test0.0.1-SNAPSHOT4.0.0

    dubbo-api-boot

org.apache.dubbo
        dubbo-spring-boot-starter
        2.7.6org.apache.curator
        curator-framework
        4.3.0org.apache.curator
        curator-recipes
        4.3.0

3.3 dubbo-api-boot bean

Movie

package com.test.dubbo.bean;

import java.io.Serializable;

public class Movie implements Serializable {


    private Integer id;
    private String movieName;
    private Double price;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getMovieName() {
        return movieName;
    }

    public void setMovieName(String movieName) {
        this.movieName = movieName;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }


    @Override
    public String toString() {
        return "Movie{" +
                "id=" + id +
                ", movieName=‘" + movieName + ‘\‘‘ +
                ", price=" + price +
                ‘}‘;
    }
}

User

package com.test.dubbo.bean;

import java.io.Serializable;

public class User implements Serializable {

    private Integer id;
    private String username;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}

3.4 dubbo-api-boot service接口

MovieService

package com.test.dubbo.service;

import com.test.dubbo.bean.Movie;

public interface MovieService {

    public Movie getNewMovie();
}

UserService

package com.test.dubbo.service;

import com.test.dubbo.bean.Movie;

public interface UserService {

    public Movie buyNewMovie();

}

4、dubbo-movie-provider-boot

4.1 dubbo-movie-provider-boot 代码整体结构

技术图片

4.2 dubbo-movie-provider-boot pom.xml


        dubbomicroservice-boot
        com.test0.0.1-SNAPSHOT4.0.0

    dubbo-movie-provider-boot

    com.test
            dubbo-api-boot
            0.0.1-SNAPSHOT

4.3 dubbo-movie-provider-boot 服务接口实现类

MovieServiceImpl

package com.test.dubbo.service.impl;

import com.test.dubbo.bean.Movie;
import com.test.dubbo.service.MovieService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

@Component //本工程内的组件可以Autowire
@Service //使用dubbo家的service暴露出来,为别的工程远程调用
public class MovieServiceImpl implements MovieService {
    public Movie getNewMovie() {
        //模拟经过数据库操作,等一些列操作获取了最新的电影
        Movie movie = new Movie();
        movie.setId(1);
        movie.setMovieName("电影名");
        movie.setPrice(96.99);
        System.out.println("电影服务被调用");
        return movie;
    }
}

4.4 dubbo-movie-provoder-boot application.properties

dubbo.application.name=dubbo-movie-provider-boot
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.protocol.port=20881
dubbo.protocol.name=dubbo

4.5 dubbo-movie-provoder-boot DubboMovieProviderBootApplication启动类

package com.test.dubbo;
import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/*
1、引入dubbo的starter
2、dubbo的相关场景配好了吗?
    1)以前配置的再配一下
    2)服务提供者使用@Service暴露服务
    3) 服务消费者使用@Reference引用服务
3、开启Dubbo基于注解的功能
 */

//@DubboComponentScan
@EnableDubbo
@SpringBootApplication
public class DubboMovieProviderBootApplication {

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

5、dubbo-user-consumer-boot

5.1 dubbo-user-consumer-boot 代码整体结构

技术图片

5.2 dubbo-user-consumer-boot pom.xml


        dubbomicroservice-boot
        com.test0.0.1-SNAPSHOT4.0.0

    dubbo-user-consumer-boot

    com.test
            dubbo-api-boot
            0.0.1-SNAPSHOTorg.springframework.boot
            spring-boot-starter-web
        

5.3 dubbo-user-consumer-boot 服务实现类

UserServiceImpl

package com.test.dubbo.service.impl;

import com.test.dubbo.bean.Movie;
import com.test.dubbo.service.MovieService;
import com.test.dubbo.service.UserService;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImpl implements UserService {

    @Reference
    MovieService movieService;

    public Movie buyNewMovie() {
        //Order order = new Order();
        //1.远程查询最新电影,并返回

        System.out.println("consumer消费者工程开始调用远程电影服务");
        Movie movie = movieService.getNewMovie();
        System.out.println("consumer消费者工程调用远程电影服务,获取了最新的电影"+movie);
        System.out.println("consumer消费者工程调用远程电影服务结束");

        //2.封装Order对象,并返回

//        order.setUserName(user.getUsername());
//        order.setMovieId(movie.getId());
//        order.setMovieName(movie.getMovieName());
        return movie;
    }
}

5.4 dubbo-user-consumer-boot Controller

package com.test.dubbo.controller;

import com.test.dubbo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ResponseBody
public class HelloController {
    @Autowired
    UserService userService;

    @RequestMapping("/hello")
    public Object hello(){

       userService.buyNewMovie();

        return "OK";
    }
}

5.5 dubbo-user-consumer-boot application.properties

dubbo.application.name=dubbo-user-consumer-boot
dubbo.registry.address=zookeeper://127.0.0.1:2181

server.port=8180

5.6 DubboUserConsumerBootApplication

package com.test.dubbo;

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


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

}

6、测试

启动dubbo-movie-provider-boot及dubbo-user-consumer-boot 工程
通过consumer消费者接口调用 http://127.0.0.1:8180/hello 查看调用效果

7、代码下载地址

github下载地址

springboot dubbo demo

标签:git   ack   引入   img   pac   beans   server   figure   ima   

原文地址:https://www.cnblogs.com/xidianzxm/p/12971629.html


评论


亲,登录后才可以留言!