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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.test</groupId>
<artifactId>dubbomicroservice</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>dubbo-movie-provider</module>
<module>dubbo-user-consumer</module>
<module>dubbo-api</module>
</modules>
</project>
3、dubbo-api
3.1、dubbo-api 代码结构
3.2、dubbo-api 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</artifactId>
<groupId>org.test</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>dubbo-api</artifactId>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.8</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.apache.dubbo</groupId>-->
<!-- <artifactId>dubbo</artifactId>-->
<!-- <version>2.7.6</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.3.0</version>
</dependency>
</dependencies>
</project>
3.3 dubbo-api 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;
//import com.test.dubbo.bean.Order;
import com.test.dubbo.bean.User;
public interface UserService {
public Movie buyNewMovie();
}
3.4 dubbo-api 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;
}
}
4、dubbo-movie-provider
4.1、dubbo-movie-provider 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</artifactId>
<groupId>org.test</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>dubbo-movie-provider</artifactId>
<dependencies>
<dependency>
<groupId>com.test</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
4.2 dubbo-movie-provider MovieServiceImpl
package com.test.dubbo.service.impl;
import com.test.dubbo.bean.Movie;
import com.test.dubbo.service.MovieService;
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.3 dubbo-movie-provoder provider.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 提供方应用信息,用于计算依赖关系 -->
<!--1、为当前应用起个名字-->
<dubbo:application name="dubbo-movie-provider" />
<!--2、告诉Dubbo注册中心地址在哪里?注册中心用的是zookeeper -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!--3、 用dubbo协议在20880端口暴露服务,消费者就会与本机的20880建立连接并进行通信-->
<dubbo:protocol name="dubbo" port="20880" />
<!--4、 声明需要暴露的服务接口(哪些接口能对外提供功能都在这里一一注册;ref指定接口实现类的名字-->
<dubbo:service interface="com.test.dubbo.service.MovieService" ref="movieService" />
<!--5、 和本地bean一样实现服务 -->
<bean id="movieService" class="com.test.dubbo.service.impl.MovieServiceImpl" />
</beans>
4.4 dubbo-movie-provider MainApplication
package com.test.dubbo;
import org.apache.jute.CsvOutputArchive;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class MainApplication {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("provider.xml");
System.out.println("容器启动......");
applicationContext.start();
System.in.read();//按任意键退出
}
}
5、dubbo-user-consumer
5.1、dubbo-user-consumer 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</artifactId>
<groupId>org.test</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>dubbo-user-consumer</artifactId>
<dependencies>
<dependency>
<groupId>com.test</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
5.2 dubbo-user-consumer UserServiceImpl
package com.test.dubbo.service.impl;
import com.test.dubbo.bean.Movie;
//import com.test.dubbo.bean.Order;
import com.test.dubbo.bean.User;
import com.test.dubbo.service.MovieService;
import com.test.dubbo.service.UserService;
public class UserServiceImpl implements UserService {
MovieService movieService;
public MovieService getMovieService() {
return movieService;
}
public void setMovieService(MovieService movieService) {
this.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.3 dubbo-user-consumer consumer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 提供方应用信息,用于计算依赖关系 -->
<!--1、为当前应用起个名字-->
<dubbo:application name="dubbo-user-consumer" />
<!--2、告诉Dubbo注册中心地址在哪里?注册中心用的是zookeeper -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!--3、 要调用哪个远程服务【服务引用】-->
<dubbo:reference id="movieService" interface="com.test.dubbo.service.MovieService" />
<!--4、将消费者加入到容器中 -->
<bean id="userService" class="com.test.dubbo.service.impl.UserServiceImpl" >
<property name="movieService" ref="movieService">
</property>
</bean>
</beans>
5.4 dubbo-user-consumer MainApplication
package com.test.dubbo;
import com.test.dubbo.bean.Movie;
import com.test.dubbo.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class MainApplication {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext applicationContext =
new ClassPathXmlApplicationContext("consumer.xml");
applicationContext.start();
UserService userService = applicationContext.getBean(UserService.class);
//测试这次远程调用是否成功
Movie newestMovie = userService.buyNewMovie();
System.out.println(newestMovie);
System.in.read();
}
}
6、docker启动Zookeeper
docker run --privileged=true -d --name zookeeper --publish 2181:2181 -d zookeeper:latest
7、运行provider MainApplication
8、运行consumer MainApplication 消费者调用提供者
9、 github代码地址
https://github.com/xidianzxm/dubbomicroservice