dubbo--01--项目简单搭建

Posted 高高for 循环

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dubbo--01--项目简单搭建相关的知识,希望对你有一定的参考价值。

Dubbo工作原理

在这里插入图片描述

Dubbo入门案例

建一个spring-boot聚合工程

在这里插入图片描述

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>com.jt.dubbo</groupId>
	<artifactId>dubbo-jt</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.2</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<java.version>1.8</java.version>
		<!--添加maven插件 -->
		<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
	</properties>

	<dependencies>
		<!--springBoot动态的引入springMVC全部的配置 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<!--引入测试类 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<!--添加属性注入依赖 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>



		<!--引入数据库驱动 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>

		<!--引入druid数据源 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.1.12</version>
		</dependency>

		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.1.3</version>
		</dependency>



		<!--添加httpClient jar包 -->
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
		</dependency>

		<!--如果包报错 去本地仓库中删除之后,刷新即可-->
		<dependency>
			<groupId>com.alibaba.boot</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
			<version>0.2.0</version>
		</dependency>
	</dependencies>
	<modules>
		<module>dubbo-jt-demo-interface</module>
		<module>dubbo-jt-demo-provider</module>
		<module>dubbo-jt-demo-consumer</module>
		<module>dubbo-jt-demo-provider2</module>
	</modules>
</project>

yml配置

dubbo-jt-demo-interface

  • 不用配置

dubbo-jt-demo-consumer

  • application.yml
server:
  port: 9001
dubbo:
  scan:
    basePackages: com.jt
  application:
    name: consumer-user   #定义消费者名称
  registry:               #注册中心地址
    address: zookeeper://127.0.0.1:2181

dubbo-jt-demo-provider

  • application.yml
server:
  port: 9000   #定义端口

spring:
  datasource:
    #引入druid数据源
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/jtdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
    username: root
    password: root

#关于Dubbo配置   
dubbo:
  scan:
    basePackages: com.jt    #指定dubbo的包路径
  application:              #应用名称
    name: provider-user     #一个接口对应一个服务名称
  registry:
    address: zookeeper://127.0.0.1:2181
  protocol:  #指定协议
    name: dubbo  #使用dubbo协议(tcp-ip)  web-controller直接调用sso-Service
    port: 20880  #每一个服务都有自己特定的端口 不能重复.

mybatis:
  mapper-locations: classpath:mapper/*.xml

dubbo-jt-demo-provider2

  • application.yml
server:
  port: 9003

spring:
  datasource:
    #引入druid数据源
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/jtdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
    username: root
    password: root
    
dubbo:
  scan:
    basePackages: com.jt
  application:
    name: provider-user
  registry:
    address: zookeeper://127.0.0.1:2181
  protocol:
    name: dubbo
    port: 20882 #每个服务都应该有自己独立的端口

      
mybatis:
  mapper-locations: classpath:mapper/*.xml

本地zookeeper打开

在这里插入图片描述
在这里插入图片描述

本地mysql数据库

  • jtdb 数据库
  • user表
spring:
  datasource:
    #引入druid数据源
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/jtdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
    username: root
    password: root

在这里插入图片描述

dubbo-jt-demo-interface

在这里插入图片描述

User

package com.jt.dubbo.pojo;

import java.io.Serializable;

public class User implements Serializable{ 
							//dubbo协议中传输的对象必须序列化
	private static final long serialVersionUID = 1L;
	private Integer id;
	private String name;
	private Integer age;
	private String sex;

	public Integer getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}
}

UserService

package com.jt.dubbo.service;

import java.util.List;
import com.jt.dubbo.pojo.User;

public interface UserService {
	
	//查询全部的用户信息
	List<User> findAll();

}

dubbo-jt-demo-consumer

在这里插入图片描述

SpringBoot_Run

package com.jt;

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

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

UserController

package com.jt.dubbo.controller;

import java.util.List;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jt.dubbo.pojo.User;
import com.jt.dubbo.service.UserService;

@RestController
public class UserController {
	
	利用dubbo的方式为接口创建代理对象 利用rpc调用
	
	@Reference(loadbalance="leastactive")
	private UserService userService;
	/**
	 * Dubbo框架调用特点:远程RPC调用就像调用自己本地服务一样简单
	 * @return
	 */

	@RequestMapping("/findAll")
	public List<User> findAll(){
		
		//远程调用时传递的对象数据必须序列化.
		return userService.findAll();
	}

}

dubbo-jt-demo-provider

在这里插入图片描述

SpringBoot_Run

package com.jt;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.jt.dubbo.mapper")
public class SpringBoot_Run {
	
	public static void main(String[] args) {
		
		SpringApplication.run(SpringBoot_Run.class, args);
	}
}

UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jt.dubbo.mapper.UserMapper">

</mapper>

UserMapper

package com.jt.dubbo.mapper;
import com.jt.dubbo.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;


import java.util.List;

@Mapper
public interface UserMapper{

   @Select("select * from user")
    List<User> selectList(Object o);
}

UserServiceImpl

package com.jt.dubbo.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.jt.dubbo.mapper.UserMapper;
import com.jt.dubbo.pojo.User;
@Service(timeout=3000)	//3秒超时 内部实现了rpc
//@org.springframework.stereotype.Service//将对象交给spring容器管理
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserMapper userMapper;
	
	@Override
	public List<User> findAll() {
		
		System.out.println("我是第一个服务的提供者");

		return userMapper.selectList(null);
	}	

}

dubbo-jt-demo-provider2

在这里插入图片描述

测试

1.启动本地zookeeper

  • zKserver.cmd (Windows下)

在这里插入图片描述

2. 运行生产者启动类

在这里插入图片描述

3. 运行消费者启动类

在这里插入图片描述

4.打开网页 http://localhost:9001/findAll

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

以上是关于dubbo--01--项目简单搭建的主要内容,如果未能解决你的问题,请参考以下文章

maven+spring boot搭建简单微服务

node 环境下简单web服务器搭建代码

maven+spring boot搭建简单微服务

maven+spring boot搭建简单微服务

VsCode 代码片段-提升研发效率

在PaddlePaddle中的Notebook代码片段