springboot dubbo demo
1、代码结构
2、整体 pom.xml
<?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>
<modules>
<module>dubbo-api-boot</module>
<module>dubbo-movie-provider-boot</module>
<module>dubbo-user-consumer-boot</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>dubbomicroservice-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbomicroservice-boot</name>
<description>Demo project for Spring Boot</description>
<packaging>pom</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-test</artifactId>-->
<!-- <scope>test</scope>-->
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>org.junit.vintage</groupId>-->
<!-- <artifactId>junit-vintage-engine</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
<!-- </dependency>-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、dubbo-api-boot
3.1 dubbo-api-boot 整体代码结构
3.2 dubbo-api-boot pom.xml
<?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">
<parent>
<artifactId>dubbomicroservice-boot</artifactId>
<groupId>com.test</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-api-boot</artifactId>
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.6</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.apache.zookeeper</groupId>-->
<!-- <artifactId>zookeeper</artifactId>-->
<!-- <version>3.6.1</version>-->
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>org.slf4j</groupId>-->
<!-- <artifactId>slf4j-log4j12</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
<!-- </dependency>-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.3.0</version>
</dependency>
</dependencies>
</project>
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
<?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">
<parent>
<artifactId>dubbomicroservice-boot</artifactId>
<groupId>com.test</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-movie-provider-boot</artifactId>
<dependencies>
<dependency>
<groupId>com.test</groupId>
<artifactId>dubbo-api-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
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
<?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">
<parent>
<artifactId>dubbomicroservice-boot</artifactId>
<groupId>com.test</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-user-consumer-boot</artifactId>
<dependencies>
<dependency>
<groupId>com.test</groupId>
<artifactId>dubbo-api-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
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下载地址