拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 Spring Boot 入门(十四)使用dubbo(用redis作为注册中心)

Spring Boot 入门(十四)使用dubbo(用redis作为注册中心)

白鹭 - 2022-02-16 2111 0 0

结尾附原始码

redis安装教程参考:Spring Boot 入门(三)部署Redis服务

新建一个maven项目,划分三层

common-公共:存放服务宣告

comsuer-客户端:存放服务呼叫

server-服务端:存放服务实作

maven参考

<!-- dubbo 依赖-->
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>2.7.8</version>
</dependency>
<!-- dubbo redis 依赖-->
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-registry-redis</artifactId>
    <version>2.7.8</version>
</dependency>
<!-- redis 支持包 -->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.1</version>
</dependency>

common

/**
 * 服务宣告
 */
public interface ITicketService {
    String getTicket();
}

server

创建服务实作

import com.example.demo.dubbo.common.ITicketService;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.UUID;

@DubboService(version = "${dubbo.application.version}",
        application = "${dubbo.application.id}",
        protocol = "${dubbo.protocol.id}",
        registry = "${dubbo.registry.id}") //将服务发布出去,注意是dubbo的注解
/**
 * 服务实作
 */
public class TicketServiceImpl implements ITicketService {
    @Override
    public String getTicket() {
        return UUID.randomUUID().toString();
    }
}

创建服务端的启动类

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

/**
 * 服务启动类
 */
@SpringBootApplication
public class DubboServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(DubboServiceApplication.class, args);
    }
}

服务端配置

# dubbo 服务端配置
dubbo..application.name=server
dubbo.application.id=server
dubbo.application.version=1.0.0
dubbo.protocol.id=dubbo
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
dubbo.registry.id=dubbo-resis
dubbo.registry.address=redis://192.168.233.128:6379
dubbo.registry.username=root
dubbo.registry.password=123456
# 服务扫描位置
dubbo.scan.base-packages=com.example.demo.dubbo

redis没有密码可以洗掉这两个配置(dubbo.registry.username、dubbo.registry.password)

我的redis有密码,所以需要设定密码,并且dubbo.registry.username随便填,

 

启动DubboServiceApplication

用RDM查询到服务注册成功

comsuer

创建服务呼叫

import com.example.demo.dubbo.common.ITicketService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

/**
 * 服务呼叫
 */
@Component
public class UserService {
    @DubboReference(version = "${dubbo.application.version}",
            application = "${dubbo.application.id}",
            registry = "${dubbo.registry.address}") //远程参考指定的服务,他会按照全类名进行匹配,看谁给注册中心注册了这个全类名
    ITicketService ticketService;

    public String bugTicket() {
        String ticket = ticketService.getTicket();
        return "在注册中心拿到:" + ticket;
    }
}

创建客户端的启动类

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

/**
 * 服务启动类
 */
@SpringBootApplication
public class DubboComsuerApplication {
    public static void main(String[] args) {
        SpringApplication.run(DubboComsuerApplication.class, args);
    }
}

客户端配置

# dubbo 客户端配置
dubbo..application.name=comsuer
dubbo.application.id=comsuer
dubbo.application.version=1.0.0
dubbo.protocol.id=dubbo
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
dubbo.registry.id=dubbo-resis
dubbo.registry.address=redis://192.168.233.128:6379
dubbo.registry.username=root
dubbo.registry.password=123456

测验客户端远程呼叫服务

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DubboComsuerApplication.class)
public class UserServiceTest {
    @Autowired
    UserService userService;

    @Test
    public void bugTicket() {
        System.err.println(userService.bugTicket());
    }

    /**
     * 注入组态档
     */
    @Configuration
    @PropertySource("classpath:application.properties")
    static class PropertiesWithJavaConfig {

        @Bean
        public static PropertySourcesPlaceholderConfigurer
        propertySourcesPlaceholderConfigurer() {
            return new PropertySourcesPlaceholderConfigurer();
        }
    }
}

测验成功,拿到一串UUID

 原始码下载地址:https://download.csdn.net/download/yfq961651795/74416295

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *