Spring Cloud Alibaba集成架构(案例)(dubbo+nacos)

参考和原创性申明:本部分设计架构和代码部分来源于Spring官网参考文档,Spring.CN官方文档,黑马程序员2019年b站nacos相关视频,Nacos官方文档,《Nacos架构和原理》,Dubbo.cn中国文档,csdn中直接引用博客将使用默认格式引入,部分引入将使APA个格式+出处来声明

这个只是学习目前阿里巴巴在2.2.8.RELEASE之后已经在废弃dubbo组件,Dubbo 3和其他组件泵很好的适应nacos,而2.2.8.RELEASE有一堆兼容性问题,本篇博客目前不具有任何参考价值

1.工程结构说明

nacos-micro-service 整体父工程

*api-gateway API网关,端口:56010

*application-1 应用1,端口:56020<属于应用层,提供http接口服务>

*service-1 服务1父工程

        #service-1-api 服务1API

        #service-1-sever 服务1实现,端口:56030

*service-2 服务2父工程

        #service-2-api 服务API

        #service-2-server 服务2实现,端口:56040

Spring Cloud Alibaba集成架构(案例)(dubbo+nacos)_第1张图片

2.实现流程

2.1

#1初始化application-1 Maven工程

老三样:阿里的nacos-cofig(nacos配置)、nacos-discovery(nacos服务发现)和spring的boot-starter-web(spring服务驱动)


    4.0.0
    
        com.itheima
        nacos-micro-service
        1.0-SNAPSHOT
    

    application-1
    jar

    application-1
    http://maven.apache.org

    
        UTF-8
    

    
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-config
        

        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        

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

        
            junit
            junit
            3.8.1
            test
        
    

#2 application功能(自顶向下的构建,注释部分为预留了基于Dubbo协议的service1和service2)

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ApplicationController {

    //注入service1和service2(基于Dubbo协议+)

    //application对外暴露的服务接口
    @GetMapping("/service")
    public String service() {
        return "test";
    }
}

#3 springstrap.yml文件

server:
  port: 56020
  servlet:
    context-path: /application1

spring:
  application:
    name: application1
  main:
    allow-bean-definition-overriding: true #Spring Boot 2.1 需要设定
  cloud:
    nacos:
      discovery: #服务发现
        server-addr: 127.0.0.1:8850
        namespace: db84d9d4-4f09-4fac-846f-38064f7ef28a #dev namespace
        cluster-name: DEFAULT
      config: #服务配置
        server-addr: 127.0.0.1:8850
        file-extension: yml
        namespace: db84d9d4-4f09-4fac-846f-38064f7ef28a #dev namespace
        group: NACOS_MICROSERVICE_GROUP

#App

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication //springboot程序
@EnableDiscoveryClient //服务发现的客户端
@EnableFeignClients //feign客户端
public class ApplicationBootstrap {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationBootstrap.class, args);
    }
}

2.2 定义service-1-api<在内部使用Dubbo接口调用,单独抽离>

关于Dubbo的说明:Apache Dubbo 是一款 RPC 服务开发框架,用于解决微服务架构下的服务治理与通信问题,官方提供了 Java、Golang 等多语言 SDK 实现。使用 Dubbo 开发的微服务原生具备相互之间的远程地址发现与通信能力, 利用 Dubbo 提供的丰富服务治理特性,可以实现诸如服务发现、负载均衡、流量调度等服务治理诉求。Dubbo 被设计为高度可扩展,用户可以方便的实现流量拦截、选址的各种定制逻辑

一些其他关于Dubbo的说明较老的版本中的@Service建议写成全路径(代码如下),@DubboService是新的<这写东西在-service1-sever的接口实现类中>,包和子包中有Service的都会暴露给Dubbo

@org.apache.dubbo.config.annotation.Service

pom.xml文件


    4.0.0
    
        com.itheima
        service-1
        1.0-SNAPSHOT
    
    service-1-api
    jar

    service-1-api
    http://maven.apache.org

    
        UTF-8
    

    
        
            junit
            junit
            3.8.1
            test
        
    

接口文件

public interface ConsumerService {
    public String service();
}

2.3 service1-server

pom文件


    4.0.0
    
        com.itheima
        service-1
        1.0-SNAPSHOT
    

    service-1-server
    jar

    service-1-server
    http://maven.apache.org

    
        UTF-8
    

    
        
        
            com.itheima
            service-1-api
            1.0-SNAPSHOT
        
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-config
        
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        
        
        
            com.alibaba.cloud
            spring-cloud-starter-dubbo
        
        
            junit
            junit
            3.8.1
            test
        
    

bootstrap.yml

P.S.protocol可以多协议,但是每个协议要有自己的端口

server:
  port: ${port:56030} #启动端口 命令行注入

spring:
  application:
    name:
  main:
    allow-bean-definition-overriding: true #Spring Boot 2.1 需要设定
  cloud:
    nacos:
      discovery: #service
        cluster-name: DEFAULT
        server-addr: 127.0.0.1:8850
        namespace: db84d9d4-4f09-4fac-846f-38064f7ef28a
      config: #config
        server-addr: 127.0.0.1:8850
        file-extension: yml
        namespace: db84d9d4-4f09-4fac-846f-38064f7ef28a
bubbo:
  scan:
    #服务扫描基准包
    base-package: com.itheima.microservice
  protocol: #协议
    name: dubbo #协议名<其他的协议IMI协议,Hessian协议>
    port: ${dubbo_port:20881} #dubbo协议端口
  registry:
    address: nacos://127.0.0.1:8850 #dubbo协议也要注册到nacos
  application:
    qos-enable: false #dubbo运维服务是否开启
  consumer:
    check: false #启动时就检查是否

接口实现类

@org.apache.dubbo.config.annotation.Service
//Service Spring和Dubbo相同,写全路径,将此类的方法暴露为dubbo籍人口

public class ConsumerServiceImpl implements ConsumerService {

    //dubbo接口实现内容
    @Override
    public String service() {
        //返回信息表示dubbo接口调用
        return "Consumer Service";
    }
}

App启动类:

你可能感兴趣的:(架构,java,开发语言)