SpringBoot之整合Dubbo

一、下载Dubbo-Admin并导入本地

dubbo-admin地址为:
GitHub - apache/dubbo-admin: The ops and reference implementation for Apache Dubbo

官方文档:
https://github.com/apache/dubbo-admin/blob/develop/README_ZH.md

注意需要安装Zookeeper(因为Dubbo使用Zookeeper作为注册中心)

二、建立服务提供者(这里是我博客的一个子项目名为blog-dubbo-provider)

1.导入Maven依赖

XML 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- dubbo的依赖 -->
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>2.7.6</version>
</dependency>


<!-- zk的依赖 -->
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-dependencies-zookeeper</artifactId>
    <version>2.7.6</version>
    <type>pom</type>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
    </exclusions>
</dependency>

2.编写启动类和配置文件

启动类:

java 复制代码
package com.dubbo.blog;

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

@SpringBootApplication
public class DubboProviderApplication {

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

配置文件(application.properties):

java 复制代码
# Spring boot application
spring.application.name=blog-dubbo-provider
# Base packages to scan Dubbo Component: @org.apache.dubbo.config.annotation.Service
dubbo.scan.base-packages=com.dubbo.blog.provider.service.impl   
# Dubbo Application
## The default value of dubbo.application.name is ${spring.application.name}
dubbo.application.name=${spring.application.name}
# Dubbo Protocol
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
## Dubbo Registry
dubbo.registry.address=zookeeper://127.0.0.1:2181
server.port=7010

3.编写接口和对应的实现类

TestService:

java 复制代码
package com.dubbo.blog.provider.service;

public interface TestService {

    String showName();
}

TestServiceImpl:

java 复制代码
package com.dubbo.blog.provider.service.impl;

import com.dubbo.blog.provider.service.TestService;
import org.apache.dubbo.config.annotation.Service;

@Service(version = "1.0.0")
public class TestServiceImpl implements TestService {
    @Override
    public String showName() {
        return "HELLO   DUBBO";
    }
}

TestService2:

java 复制代码
package com.dubbo.blog.provider.service;

public interface TestService2 {
    String showName();
}

TestService2Impl:

java 复制代码
package com.dubbo.blog.provider.service.impl;

import com.dubbo.blog.provider.service.TestService2;
import org.apache.dubbo.config.annotation.Service;

@Service(version = "1.0.0")
public class TestService2Impl implements TestService2 {
    @Override
    public String showName() {
        return "HELLO   TestService2";
    }
}

4.启动主类并打开Dubbo-Admin

下图表示Ok

三、建立服务消费者(这里是我博客的一个子项目名为blog-dubbo-consumer)

1.导入Maven依赖

XML 复制代码
<dependency>
    <groupId>com.challenger.blog</groupId>
    <artifactId>blog-dubbo-provider</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- dubbo的依赖 -->
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>2.7.6</version>
</dependency>


<!-- zk的依赖 -->
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-dependencies-zookeeper</artifactId>
    <version>2.7.6</version>
    <type>pom</type>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
    </exclusions>
</dependency>

2.编写启动类和配置文件

启动类:

java 复制代码
package com.dubbo.blog;

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

@SpringBootApplication
public class DubboConsumerApplication {

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

}

配置文件(application.properties):

复制代码
spring.application.name=blog-dubbo-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
server.port=7000

3.编写Controller

java 复制代码
package com.dubbo.blog;


import com.dubbo.blog.provider.service.TestService;
import com.dubbo.blog.provider.service.TestService2;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestDubboController {
    @Reference(version = "1.0.0")
    private TestService2 service2;
    @Reference(version = "1.0.0")
    private TestService service;

    @GetMapping("test1")
    public String test1() {
        return service.showName();
    }

    @GetMapping("test2")
    public String test2() {
        return service2.showName();
    }
}

4.切换到dubbo-admin看服务关系

5.可以请求Controller对应的url测试一下

相关推荐
丁总学Java7 分钟前
--spring.profiles.active=prod
java·spring
苹果醋310 分钟前
React系列(八)——React进阶知识点拓展
运维·vue.js·spring boot·nginx·课程设计
上等猿14 分钟前
集合stream
java
java1234_小锋17 分钟前
MyBatis如何处理延迟加载?
java·开发语言
菠萝咕噜肉i19 分钟前
MyBatis是什么?为什么有全自动ORM框架还是MyBatis比较受欢迎?
java·mybatis·框架·半自动
海绵波波10727 分钟前
flask后端开发(1):第一个Flask项目
后端·python·flask
林的快手33 分钟前
209.长度最小的子数组
java·数据结构·数据库·python·算法·leetcode
weisian1511 小时前
Redis篇--常见问题篇8--缓存一致性3(注解式缓存Spring Cache)
redis·spring·缓存
向阳12181 小时前
mybatis 缓存
java·缓存·mybatis
HEU_firejef1 小时前
Redis——缓存预热+缓存雪崩+缓存击穿+缓存穿透
数据库·redis·缓存