其他分享
首页 > 其他分享> > spring-boot-note9---dubbo基本使用

spring-boot-note9---dubbo基本使用

作者:互联网

4spring-boot-note9---dubbo的基本使用

工程简介:一个父项目demo82_dubbo_all,下面四个子项目:spring_dubbo_api、spring_dubbo_common、spring_dubbo_controller、spring_dubbo_service。

依赖关系:spring_dubbo_controller和spring_dubbo_service一起依赖spring_dubbo_api。而spring_dubbo_api依赖spring_dubbo_common。

spring_dubbo_common: 主要是pojo,工具类

spring_dubbo_api: 公共接口类

spring_dubbo_service:提供服务实现

spring_dubbo_controller:消费服务

一、开始建立项目--父pom.xml

<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>demo82_dubbo</groupId>
  <artifactId>demo82_dubbo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>demo82_dubbo</name>
  <description>demo82_dubbo</description>
  
    <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>2.0.4.RELEASE</version>
       <relativePath/>
    </parent>
    
     <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <project.version>1.0</project.version>
    </properties>
    
  	 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- dubbo -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>

        <!-- zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.8</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
  
  
    <modules>
    	<module>spring_dubbo_api</module>
    	<module>spring_dubbo_common</module>
    	<module>spring_dubbo_controller</module>
    	<module>spring_dubbo_service</module>
    </modules>
</project>

二、spring_dubbo_common项目

public class Product implements Serializable{

	private static final long serialVersionUID = 136863327838375848L;

	private Integer id;
	
	private String name;
	
	private Double price;
	
	private String desc;

三、spring_dubbo_api项目

1、pom
  <dependencies>
      <dependency>
           <groupId>demo82_dubbo</groupId>
        <artifactId>spring_dubbo_common</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
  </dependencies>
  2、com.wj.api包下就一个api类

/**
 * 公共接口
 */
public interface ProductService {

	List<Product> findAll();
	
	void addOne(Product p);
	
	void deleteOne(Integer id);
	
	void findOne(Integer id);
}

四、spring_dubbo_service项目 端口8080

  <dependencies>
  	<dependency>
	   	<groupId>demo82_dubbo</groupId>
	    <artifactId>spring_dubbo_api</artifactId>
	    <version>0.0.1-SNAPSHOT</version>
    </dependency>
  </dependencies>

4.1配置application.properties

#启动端口
server.port: 8080

#服务方需要配置前三项+port
dubbo.application.name:server-provider
dubbo.registry.address:zookeeper://127.0.0.1:2181
dubbo.protocol.name:dubbo
dubbo.protocol.port:20880

4.2 DaoKit

public class DaoKit {
	
	/**
	 * 内存数据库
	 */
	private static Map<Integer,Product> products = new HashMap<>();
	
	static{
		for (int i = 0; i < 10; i++) {
			Product p = new Product();
			p.setId(i);
			p.setName("name"+i);
			p.setPrice((double) i);
			p.setDesc("desc"+i);
			products.put(i, p);
		}
	}
	
	public static List<Product> findAll() {
		List<Product> list = new ArrayList<>();
		list.addAll(products.values());
		return list;
	}
	
	public static void deleteOne(Integer id) {
		products.remove(id);
	}
	
	public static void findOne(Integer id) {
		products.get(id);
	}
	
	public static void addOne(Product p) {
		if(p.getId()!=null) {
			Product old = products.get(p.getId());
			if(old==null) {
				products.put(p.getId(), p);
			}
		}
	}
	
}

4.3 ProductServiceImpl 需要标注@Service(interfaceClass = ProductService.class)

@Service(interfaceClass = ProductService.class)
@Component
public class ProductServiceImpl implements ProductService{

	@Override
	public List<Product> findAll() {
		System.out.println("服务被调用");
		return DaoKit.findAll();
	}

	@Override
	public void addOne(Product p) {
		DaoKit.addOne(p);
	}

	@Override
	public void deleteOne(Integer id) {
		DaoKit.deleteOne(id);
	}

	@Override
	public void findOne(Integer id) {
		DaoKit.findOne(id);
	}

}

4.4 启动类

@EnableDubbo
@SpringBootApplication
public class Application {

	/**
	 * main方式启动 :
	 */
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

}

主要有几个地方配置:1、app.prop配置地址等。2、实现接口的service标注@Service 3、使用@EnableDubbo开启

五、spring_dubbo_controller工程 端口8082

1、pom.xml同spring_dubbo_service

2、application.properties

#启动端口
server.port: 8082

#消费方需要配置前三项
dubbo.application.name:server-consumer
dubbo.registry.address:zookeeper://127.0.0.1:2181
dubbo.protocol.name:dubbo

3、ProductController

@RestController
public class ProductController {

	@Reference
	private ProductService service;
	
	@GetMapping("/findAll")
	public List<Product> findAll() {
		System.out.println("服务被消费");
		return service.findAll();
	}
	
}

主要有几个地方配置:1、app.prop配置地址等。2、注入的service标注@Reference  3、使用@EnableDubbo开启

 

六、测试

轮流启动service和controller2个项目,访问地址http://localhost:8082/findAll。

这个是service项目打印的

七、dubbo_admin的使用

本人已经打包一个新的dubbo_admin,地址到我的资源找,将此war部署到tomcat中即可.

访问地址:http://localhost:8090  (8090你配置tomcat的端口)。账号root root

 

 

 

 

标签:note9,dubbo,service,spring,void,public,id
来源: https://blog.csdn.net/shuixiou1/article/details/112200130