dubbo微服务入门示例

1.目标

  • 搭建dubbo微服务基础环境并编写示例
  • 环境(docker镜像)
    • centos7服务器
    • zookeeper单机版本:Registry,服务注册中心
    • dubbo-admin后台管理:Monitor,统计服务和调用次数,调用时的监控中心
  • 系统
    • spring-dubbo-api:定义接口,方便provider和consumer端使用,避免重复编码
    • spring-dubbo-provider:Provider,暴露服务的提供方
    • spring-dubbo-consumer:Consumer:调用远程服务的服务消费方
  • 参考

2.编写代码

2.1.zookeeper环境
  • centos-dubbo(dockerfile)
# 系统镜像zookeeper
FROM docker.io/zookeeper:3.4.13

# 维护者
MAINTAINER Pings 275598139@qq.com

# 环境变量
ENV LANG en_US.UTF-8
# 设置时区
RUN ln -s -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

# 安装tomcat
COPY apache-tomcat-8.5.8.tar.gz /opt/java/tomcat/apache-tomcat-8.5.8.tar.gz
RUN tar xzf /opt/java/tomcat/apache-tomcat-8.5.8.tar.gz -C /opt/java/tomcat && rm -f /opt/java/tomcat/apache-tomcat-8.5.8.tar.gz
# 配置(修改端口号为8085)
RUN sed -i "s/<Connector port=\"8080\"/<Connector port=\"8085\"/" /opt/java/tomcat/apache-tomcat-8.5.8/conf/server.xml

# 部署dubbo-admin
COPY dubbo-admin-2.5.4-SNAPSHOT.war /opt/java/tomcat/apache-tomcat-8.5.8/webapps/

# 开启端口
EXPOSE 8085

# 启动服务
CMD ["zkServer.sh", "start-foreground"]

# 运行命令
# docker build -t pings/centos-dubbo -f centos-dubbo .
# docker run -p 2181:2181 -p 8085:8085 --name dubbo pings/centos-dubbo
# 需要进入容器手动启动tomcat
# docker exec -it dubbo /bin/bash
# /opt/java/tomcat/apache-tomcat-8.5.8/bin/catalina.sh run
# http://192.168.1.233:8085/dubbo-admin-2.5.4-SNAPSHOT
# dubbo-admin用户:root/root
  • 问题
    • 同时启动zookeeper和tomcat错误
      • CMD [“zkServer.sh”, “start-foreground”, “/opt/java/tomcat/apache-tomcat-8.5.8/bin/startup”],start-foreground启动参数无效
      • 使用shell脚本启动:zkServer.sh start-foreground,start-foreground启动参数无效
    • 现阶段只能先启动zookeeper然后进入容器启动tomcat
  • 源码:dubbo zookeeper环境
2.2.spring-dubbo-api
  • 功能:定义接口,方便provider和consumer端使用,避免重复编码
  • springboot项目,定义需要暴露服务的接口、实体类和公用类,不需要引入dubbo相关配置;
  • 实体类:User.java
public class User implements Serializable {
    private Integer id;
    private String name;
    private Integer age;
    private Integer sex;
    private String phone;

    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 Integer getSex() {
        return sex;
    }

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

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex=" + sex +
                ", phone='" + phone + '\'' +
                '}';
    }
}
  • 接口:UserService.java
public interface UserService {
    User getById(int id);
}
2.3.spring-dubbo-provider
  • 功能:暴露服务的提供方,实现业务逻辑的地方;
  • springboot项目,引入dubbo相关配置;
  • 引入依赖:pom.xml
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- dubbo-springBoot -->
<dependency>
	<groupId>com.alibaba.spring.boot</groupId>
	<artifactId>dubbo-spring-boot-starter</artifactId>
	<version>2.0.0</version>
</dependency>
<!-- spring-dubbo-api -->
<dependency>
	<groupId>com.pings</groupId>
	<artifactId>spring-dubbo-api</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- zookeeper -->
<dependency>
	<groupId>org.apache.zookeeper</groupId>
	<artifactId>zookeeper</artifactId>
	<version>3.4.13</version>
</dependency>
<!-- ZkClient -->
<dependency>
	<groupId>com.101tec</groupId>
	<artifactId>zkclient</artifactId>
	<version>0.11</version>
</dependency>
  • 配置:application.properties
#dubbo springboot 配置
spring.dubbo.application.id=spring-dubbo-provider     //**注册到zookeeper上的服务id
spring.dubbo.application.name=spring-dubbo-provider   //**注册到zookeeper上的服务名称
spring.dubbo.registry.address=zookeeper://192.168.1.233:2181 //**注册的zookeeper地址
spring.dubbo.server=true                              //**dubbo服务端
spring.dubbo.protocol.name=dubbo                      //**dubbo连接协议
spring.dubbo.protocol.port=20880
  • 启动配置:SpringDubboProviderApplication
@SpringBootApplication
//**添加些配置,启用dubbo配置
@EnableDubboConfiguration
public class SpringDubboProviderApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringDubboProviderApplication.class, args);
	}

}
  • 接口实现:UserServiceImpl.java
//**dubbo配置
@Service(interfaceClass = UserService.class)
//**spring配置
@Component
public class UserServiceImpl implements UserService {

    @Override
    public User getById(int id) {
        User user = new User();
        user.setId(id);
        user.setName("Pings");
        user.setAge(22);
        user.setSex(1);
        user.setPhone("13333333333");

        return user;
    }
}
2.4.spring-dubbo-consumer(调用远程服务的服务消费方)
  • 功能:调用远程服务的服务消费方
  • springboot项目,引入dubbo相关配置,pom.xml和SpringDubboConsumerApplication和provider相同;
  • 配置:application.properties
server.port=8000   //**启动端口不要与provider相同

#dubbo springboot 配置
spring.dubbo.application.name=spring-dubbo-consumer  //**注册到zookeeper上的服务id
spring.dubbo.application.id=spring-dubbo-consumer    //**注册到zookeeper上的服务名称
spring.dubbo.registry.address=zookeeper://192.168.1.233:2181  //**注册的zookeeper地址
  • 消费:UserController
@RestController
public class UserController {

    @Reference
    public UserService userService;

    @RequestMapping(value = "/")
    public String getById(int id){
        return this.userService.getById(id).toString();
    }
}

3.验证

  • 启动spring-dubbo-provider
    • 打开http://192.168.1.233:8085/dubbo-admin-2.5.4-SNAPSHOT/
    • 查看服务com.pings.demo.service.UserService提供者:192.168.189.129:20880 100 动态
    • 如果没有安装dubbo-admin,也可通过idea zookeeper插件进行查看;
  • 启动spring-dubbo-consumer
    • 打开http://192.168.1.233:8085/dubbo-admin-2.5.4-SNAPSHOT/
    • 查看服务com.pings.demo.service.UserService消费者:192.168.189.129 spring-dubbo-consumer 已允许
    • 如果没有安装dubbo-admin,也可通过idea zookeeper插件进行查看;
  • 访问
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值