轻松搭建Dubbo架构体系,你也能成为一流架构师

基于Dubbo架构体系,拿来即用,毫无任何杂糅的组件,可以基于此作为Dubbo分布式微服务的脚手架。

dubbo-admin地址为:

https://github.com/apache/dubbo-admin

官方文档:

https://github.com/apache/dubbo-admin/blob/develop/README_ZH.md

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

一定要把dubbo-admin中的duboo-admin-server运行起来,对应的dubbo-ui通过npm run dev命令也要将其运行下来(记得npm install)

对于学习Java还有相关的一切疑惑与难题,皆可随时私信我咨询,朋友圈 输入:huany6880
本人接触Java语言将近20年时间,敢说自己对于Java的了解是精通的水平,如能尽自己的绵薄之力为大家提供一些微薄的帮助,不胜荣幸。另外还有专属IT编程就业规划师(Java、人工智能方向)和Java架构师路线图、面试题资料。

一、生产者

1.导入Maven依赖


       
           org.springframework.boot
           spring-boot-starter
       

       
           org.apache.dubbo
           dubbo-spring-boot-starter
           3.0.2.1
       

       
           org.apache.curator
           curator-x-discovery
           5.1.0
       

       
       
           org.apache.dubbo
           dubbo-dependencies-zookeeper
           2.7.6
           pom
           
               
                   org.slf4j
                   slf4j-log4j12
               
           
       
   


2.主类

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

3.Service极其ServiceImpl

TestService.java

public interface TestService {
    String showName();
}


TestServiceImpl.java

@DubboService(version = "1.0")
public class TestServiceImpl implements TestService {
    @Override
    public String showName() {
        return "show server name";
    }
}

4.配置文件

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

5.运行效果

轻松搭建Dubbo架构体系,你也能成为一流架构师_第1张图片

二、消费者

1.导入Maven依赖


        
            com.yc.framework
            yc-example-dubbo-provider
            1.0
        

        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
        
            org.apache.dubbo
            dubbo-spring-boot-starter
            3.0.2.1
        

        
            org.apache.curator
            curator-x-discovery
            5.1.0
        

        
        
            org.apache.dubbo
            dubbo-dependencies-zookeeper
            2.7.6
            pom
            
                
                    org.slf4j
                    slf4j-log4j12
                
            
        
    

2.主类

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

3.配置文件

spring.application.name=dubbo-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
server.port=7000
dubbo.protocol.name=dubbo
dubbo.protocol.port=20881

4.测试类

TestController.java如下:

@RestController
public class TestController {
    @DubboReference(version = "1.0")
    private TestService testService;

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


5.测试效果

轻松搭建Dubbo架构体系,你也能成为一流架构师_第2张图片

轻松搭建Dubbo架构体系,你也能成为一流架构师_第3张图片

三、源代码

https://github.com/developers-youcong/yc-framework/tree/main/yc-example/yc-example-dubbo

你可能感兴趣的:(java进阶分享教学,架构,java,Dubbo,java进阶,java教程)