下载地址
不会使用Maven的话 请参考 Maven的简单使用
mvn clean package -Dmaven.test.skip=true #使用maven命令清理并打包 跳过测试
出现 BUILD SUCCESS 即为打包成功!
进入 dubbo-admin/target 目录 找到 dubbo-admin-2.8.4.war
把改war包复制至tomcat的webapps目录下
一定要先开启zookeeper才可以开启dubbo-admin哦!
访问地址
环境准备
dubbo项目讲究分层架构
所以我们找个合适的位置创建一个空文件夹
创建统一的api接口
hello-dubbo-api
IDEA中右键 => new => Module => Maven项目 => 什么都不选 => 填写你想要的项目信息 => 一直下一步
package service;
/**
*
*
* Description: 统一对外提供服务的接口
*
*
* @author yufire
* @version v1.0.0
* @since 2020-06-11 18:36:20
* @see service
*
*/
public interface HelloDubboApi {
/**
* 测试方法
* @param name
* @return
*/
String sayHi(String name);
}
mvn clean install
创建提供服务的 provider
hello-dubbo-provider
右键=> new => Module => SpringInitializr => 下一步
添加 provider所需的 maven依赖
<dependency>
<groupId>cn.yufiregroupId>
<artifactId>hello-dubbo-apiartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>com.alibaba.bootgroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>0.2.0version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<exclusions>
<exclusion>
<groupId>org.junit.vintagegroupId>
<artifactId>junit-vintage-engineartifactId>
exclusion>
exclusions>
dependency>
package cn.yufire.hellodubboprovider.api.impl;
import com.alibaba.dubbo.config.annotation.Service;
import service.HelloDubboApi;
// 注意导包 是 com.alibaba.duboo 包下的 @Service注解
@Service(interfaceClass = HelloDubboApi.class)
public class HelloDubboApiImpl implements HelloDubboApi {
@Override
public String sayHi(String name) {
return "Hello Dubbo " + name;
}
}
# 配置连接注册中心的信息
dubbo:
application:
name: hello-dubbo-provider # 服务名称
registry:
address: zookeeper://zool.yufure.cn:2181 # 注册中心地址
protocol: # 协议
name: dubbo # 协议名称
port: 20888 # 服务的端口号
package cn.yufire.hellodubboprovider;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo //启动 dubbo
@SpringBootApplication
public class HelloDubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(HelloDubboProviderApplication.class, args);
}
}
创建 服务消费者
hello-dubbo-consumer
右键=> new => Module => SpringInitializr => 下一步
添加 consumer 所需的 maven依赖
<dependency>
<groupId>cn.yufiregroupId>
<artifactId>hello-dubbo-apiartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>com.alibaba.bootgroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>0.2.0version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<exclusions>
<exclusion>
<groupId>org.junit.vintagegroupId>
<artifactId>junit-vintage-engineartifactId>
exclusion>
exclusions>
dependency>
package cn.yufire.hello.dubbo.consumer.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import service.HelloDubboApi;
@RestController
public class HelloDubboController {
// 使用 dubbo提供的 @Reference 远程调用 注册中心的 HelloDubboApi类
@Reference
private HelloDubboApi helloDubboApi;
@GetMapping("/hello")
public String helloDubbo(String name) {
return helloDubboApi.sayHi(name);
}
}
package cn.yufire.hello.dubbo.consumer;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo // 开启Dubbo
@SpringBootApplication
public class HelloDubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(HelloDubboConsumerApplication.class, args);
}
}
server:
port: 8090 # consumer项目需要对外 提供web服务 避免与本地tomcat冲突 所以 改下端口号
dubbo:
application:
name: hello-dubbo-consumer
registry:
address: zookeeper://zool.yufuie.cn:2181
protocol:
name: dubbo
consumer:
timeout: 2000
测试地址
已完成 dubbo的基本操作!
random
随机调用。官网的负载均衡策略
Random LoadBalance
RoundRobin LoadBalance
LeastActive LoadBalance
ConsistentHash LoadBalance
配置负载均衡
# 配置连接注册中心的信息
dubbo:
application:
name: hello-dubbo-provider # 服务名称
registry:
address: zookeeper://zool.yufire.cn:2181 # 注册中心地址
protocol: # 协议
name: dubbo # 协议名称
port: 12345 # 服务的端口号
provider:
loadbalance: roundrobin # 配置负载均衡 策略为: 轮询
package cn.yufire.hellodubboprovider.api.impl;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Value;
import service.HelloDubboApi;
// 注意导包 是 com.alibaba.duboo 包下的 @Service注解
@Service(interfaceClass = HelloDubboApi.class)
public class HelloDubboApiImpl implements HelloDubboApi {
@Value("${dubbo.protocol.port}")
private String port;
@Override
public String sayHi(String name) {
return "Hello Dubbo " + name + " 我来自:" + port;
}
}
idea上方选项卡 => RUN => Edit Configurations
完!