Dubbo是什么?

dubbo是一款高性能,轻量级的开源java RPC框架。

其三大核心:

远程通讯,面向接口的远程方法调用

智能容错和负载均衡

服务的自动注册和发现

概念

1571398361922

服务提供者(Provider):提供服务的服务提供方,服务提供方在启东时,像注册中心注册自身服务。

服务消费者(Consumer):调用远程服务的服务消费方,服务消费者在启动时,向注册中心订阅自己所需的服务,服务消费者,从提供地址列表中,根据软件负载均衡算法,选一台提供者进行调用,如果调用失败,在选取另一台调用 。

注册中心(Register):注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者 。

监控中心(Monitor):服务消费者和提供者,在内存只累计调用次数和调用时间,定义每分钟发送一次统计数据到监控中心。

安装dubbo监控

dubbo本身不是一个服务软件,其自身为一个jar包。作用为帮你的java程序连接到zookeeper,利用zookeeper实现消费,提供服务 。

所需要的工具和解压如图

1571399947862

1571400436979

1571400887606

配置文件的内容

1571401140445

注意:用zookeeper作为注册中心时,要提前将zookeeper启动!

运行

首先运行zookepper

1571401938011

在运行zookepper文件之前,复制配置文件,并将其改名。否则启动的时候会出现闪退的现象

1571402259886

具体的原因是启动的时候寻找的是zoo.cfg文件,而不是zoo_simple.cfg。在找不到文件的时候就会闪退 。

启动

1571402535774

根据自身环境,选择合适的运行方法。

运行成功后

1571402621208

运行dubbo

在zookeeper运行成功后,运行dubbo。

1571402763399

此时在cmd命令下运行jar包

1571403057192

成功的标志

1571403144915

验证登录

1571403279962

登录界面首页

1571403321450

注册中心的状态

1571403447327

Dubbo整合springboot (这才是正文啊啊啊啊)

项目 :如下图

1571461003138

其中,实体类和服务接口,被消费者和服务者依赖。

实体类和服务接口架构

1571461153816

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.bc</groupId>
  <artifactId>spring-user-interface</artifactId>
  <version>1.0</version>
  
  <properties>
		<lombok.version>1.18.8</lombok.version>
  </properties>
  <dependencies>
  	<dependency>
  			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>${lombok.version}</version>
			<scope>provided</scope>
  	</dependency>
  </dependencies>
</project>

domain包下的实体类代码

package com.bc.domain;

import java.io.Serializable;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable{
	private static final long serialVersionUID = 1L;
	private Integer id ;
	private String name ;
	private String address ;
}

service包下的接口代码

OrderService

package com.bc.service;

import java.util.List;

import com.bc.domain.User;

public interface OrderService {
	public List<User> initOrder(Integer id) ;
}

UserService

package com.bc.service;

import java.util.List;

import com.bc.domain.User;

public interface UserService {
	public List<User> getUserList();
}

服务者相关结构和代码

1571461478088

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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.9.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.bc</groupId>
	<artifactId>springboot-service-provider</artifactId>
	<version>1.0</version>
	<name>springboot-service-provider</name>
	<description>springboot与dubbo的集成服务提供者(白茶弟弟)</description>

	<properties>
		<java.version>1.8</java.version>
		<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
		<dubbo.version>2.6.5</dubbo.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>com.bc</groupId>
  			<artifactId>spring-user-interface</artifactId>
 			<version>1.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		  <!-- Dubbo Spring Boot Starter -->
		<dependency>
			<groupId>com.alibaba.boot</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
			<version>0.2.1.RELEASE</version>
		</dependency>	
		 <dependency>
		 	<groupId>com.alibaba</groupId>
		 	<artifactId>dubbo</artifactId>
		 	<version>${dubbo.version}</version>
		 </dependency>
		 <dependency>
		 	<groupId>io.netty</groupId>
		 	<artifactId>netty-all</artifactId>
		 </dependency>
		 <!-- curator-framework -->
		 <dependency>
		 	<groupId>org.apache.curator</groupId>
		 	<artifactId>curator-framework</artifactId>
		 	<version>2.12.0</version>
		 </dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

UserServiceImpl代码

package com.bc.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.dubbo.config.annotation.Service;
import com.bc.domain.User;
import com.bc.service.UserService;
//注意此时的service为dubbo的注解
@Service
public class UserServiceImpl implements UserService{
	
	public static List<User> user=new ArrayList<>();
	static {
		user.add(new User(1,"白菜弟弟","安徽"));
	}
	@Override
	public List<User> getUserList() {
		// TODO Auto-generated method stub
		return user;
	}
	
}

启动类代码

package com.bc;

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

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;

@SpringBootApplication
@EnableDubbo //注意需要这个注解
public class SpringbootServiceProviderApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootServiceProviderApplication.class, args);
		System.out.println("服务者启动成功");
	}
}

配置文件代码application.Properties


#Dubbo config
dubbo.application.name=springboot-service-provider
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880

至此服务提供完成,运行启动类查看

1571461867495

浏览器查看

1571461921644

消费者相关部分和代码

1571462023873

代码

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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.9.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.bc</groupId>
	<artifactId>springboot-service-consumer</artifactId>
	<version>1.0</version>
	<name>springboot-service-consumer</name>
	<description>springboot与dubbo的集成服务消费者(白茶弟弟)</description>

	<properties>
		<java.version>1.8</java.version>
		<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
		<dubbo.version>2.6.5</dubbo.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>com.bc</groupId>
  			<artifactId>spring-user-interface</artifactId>
  			<version>1.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		
		 <!-- Dubbo Spring Boot Starter -->
		<dependency>
			<groupId>com.alibaba.boot</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
			<version>0.2.1.RELEASE</version>
		</dependency>	
		 <dependency>
		 	<groupId>com.alibaba</groupId>
		 	<artifactId>dubbo</artifactId>
		 	<version>${dubbo.version}</version>
		 </dependency>
		 <dependency>
		 	<groupId>io.netty</groupId>
		 	<artifactId>netty-all</artifactId>
		 </dependency>
		 <!-- curator-framework -->
		 <dependency>
		 	<groupId>org.apache.curator</groupId>
		 	<artifactId>curator-framework</artifactId>
		 	<version>2.12.0</version>
		 </dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

配置文件代码application.Properties

#Dubbo 
dubbo.application.name=springboot-service-provider
dubbo.registry.address=zookeeper://127.0.0.1:2181

server.port=8888

OrderServiceImpl实现类

package com.bc.service;

import java.util.List;

import org.springframework.stereotype.Service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.bc.domain.User;

@Service//此时的这个是spring下的
public class OrderServiceImpl implements OrderService {
	
	@Reference  //这个是dubbo下的
	private UserService userService ;
	@Override
	public List<User> initOrder(Integer id) {
		// TODO Auto-generated method stub
		return userService.getUserList();
	}
	
}

Controller

package com.bc.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.bc.domain.User;
import com.bc.service.OrderService;


@RestController
public class CusController {
	
	@Autowired
	private OrderService orderSrvice ;
	
	@RequestMapping("getUser")
	public List<User> getUser(){
		return this.orderSrvice.initOrder(1);
	}
}

启动类

package com.bc;

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

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;

@SpringBootApplication
@EnableDubbo
public class SpringbootServiceConsumerApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootServiceConsumerApplication.class, args);
		System.out.println("消费者启动成功");
	}
}

以上就是代码部分

启动消费者

1571462355993

浏览器查看

1571462408452

1571462468130

至此说明已完成。