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

2022/1/6 19:34:32

本文主要是介绍Spring Boot 入门(十四)使用dubbo(用redis作为注册中心),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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();
        }
    }
}

 



这篇关于Spring Boot 入门(十四)使用dubbo(用redis作为注册中心)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程