搭建Apache Dubbo工程

2021-03-08 00:30

阅读:717

标签:prot   int   ram   hose   evel   user   active   插入   group   

此项目是Spring Cloud Alibaba 和 Dubbo 整合 Demo

版本

Apache Dubbo Demo

JDK 1.8

Dubbo 2.7.2

Nacos Client 1.0.0

Spring Boot 2.1.6.RELEASE

Dubbo Registry Nacos 2.6.6

Dubbo Spring Boot Starter 2.7.1

Spring Cloud Greenwich.SR2

Spring Cloud Alibaba 2.1.0.RELEASE

Alibaba Spring Context Support 1.0.2

Spring Cloud Alibaba Nacos Config 2.1.0.RELEASE

创建父工程 apache-dubbo-demo

此工程只负责管理模块

一、项目结构

技术图片

二、pom.xml

4.0.0org.springframework.boot
        spring-boot-starter-parent
        2.1.6.RELEASEcom.cnbai
    apache-dubbo-demo
    0.0.1-SNAPSHOTpomhttp://www.cnbai.xyz此工程只负责管理模块 module apache-dubbo-dependencies1.8${java.version}${java.version}UTF-8UTF-8Apache 2.0https://www.apache.org/licenses/LICENSE-2.0.txtcnbaicnbaibaihaibo1024@163.comcom.cnbai
                apache-dubbo-dependencies
                ${project.version}pomimportdefault
            
                true
            
            
            0.0.12io.spring.javaformat
                        spring-javaformat-maven-plugin
                        ${spring-javaformat.version}org.apache.maven.plugins
                        maven-surefire-plugin
                        **/*Tests.java**/Abstract*.javafile:/dev/./urandomtrueorg.apache.maven.plugins
                        maven-enforcer-plugin
                        enforce-rulesenforcecommons-logging:*:*truetrueorg.apache.maven.plugins
                        maven-install-plugin
                        trueorg.apache.maven.plugins
                        maven-javadoc-plugin
                        truetruespring-milestoneSpring Milestonehttps://repo.spring.io/milestonefalsespring-snapshotSpring Snapshothttps://repo.spring.io/snapshottruespring-milestoneSpring Milestonehttps://repo.spring.io/milestonefalsespring-snapshotSpring Snapshothttps://repo.spring.io/snapshottrue

统一依赖管理 apache-dubbo-dependencies

此工程只负责管理依赖

一、项目结构

技术图片

二、pom.xml

4.0.0com.cnbai
    apache-dubbo-dependencies
    0.0.1-SNAPSHOTpomhttp://www.cnbai.xyz此工程只负责管理依赖 dependency 2.7.22.7.22.7.1Greenwich.SR21.0.02.6.62.1.0.RELEASE
        1.0.2
    Apache 2.0https://www.apache.org/licenses/LICENSE-2.0.txtcnbaicnbaibaihaibo1024@163.comorg.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}pomimportcom.alibaba.cloud
                spring-cloud-alibaba-dependencies
                ${spring-cloud-alibaba.verion}pomimportorg.apache.dubbo
                dubbo
                ${dubbo.version}org.springframework
                        spring
                    javax.servlet
                        servlet-api
                    log4j
                        log4j
                    org.apache.dubbo
                dubbo-spring-boot-actuator
                ${dubbo-actuator.version}com.alibaba.spring
                spring-context-support
                ${alibaba-spring-context-support.version}com.alibaba
                dubbo-registry-nacos
                ${dubbo-registry-nacos.version}com.alibaba.nacos
                nacos-client
                ${nacos-client.version}org.apache.dubbo
                dubbo-serialization-kryo
                ${dubbo-kryo.version}spring-milestoneSpring Milestonehttps://repo.spring.io/milestonefalsespring-snapshotSpring Snapshothttps://repo.spring.io/snapshottruespring-milestoneSpring Milestonehttps://repo.spring.io/milestonefalsespring-snapshotSpring Snapshothttps://repo.spring.io/snapshottrue

服务提供者 apache-dubbo-provider

服务提供者

一、项目结构

技术图片

二、apache-dubbo-provider/pom.xml

4.0.0com.cnbai
        apache-dubbo-demo
        0.0.1-SNAPSHOT

    apache-dubbo-provider
    pomhttp://www.cnbai.xyz2020-NowApache 2.0https://www.apache.org/licenses/LICENSE-2.0.txtcnbaicnbaibaihaibo1024@163.comapache-dubbo-provider-apiapache-dubbo-provider-service

三、apache-dubbo-provider-api/pom.xml

4.0.0com.cnbai
        apache-dubbo-provider
        0.0.1-SNAPSHOT

    apache-dubbo-provider-api
    jarhttp://www.cnbai.xyz2020-Now服务提供者接口模块,该模块只负责定义接口Apache 2.0https://www.apache.org/licenses/LICENSE-2.0.txtcnbaicnbaibaihaibo1024@163.com

四、EchoService

package com.cnbai.apache.dubbo.provider.api;

/**
 * 测试动态化配置
 *
 * @author bai
 * @since 2020/5/12
 */
public interface EchoService {

    /**
     * 测试 nacos 动态化配置文件
     *
     * @param string   string
     * @return {@link String}
     * @author bai
     * @since 2020/5/12
     */
    String echo(String string);
}

五、apache-dubbo-provider-service/pom.xml

4.0.0com.cnbai
        apache-dubbo-provider
        0.0.1-SNAPSHOT

    apache-dubbo-provider-service
    jarhttp://www.cnbai.xyz2020-Now服务提供者接口实现Apache 2.0https://www.apache.org/licenses/LICENSE-2.0.txtcnbaicnbaibaihaibo1024@163.comorg.springframework.boot
            spring-boot-starter
        org.springframework.boot
            spring-boot-starter-test
            testorg.apache.dubbo
            dubbo-spring-boot-starter
        org.apache.dubbo
            dubbo
        com.alibaba
            dubbo-registry-nacos
        com.alibaba.nacos
            nacos-client
        com.alibaba.spring
            spring-context-support
        org.apache.dubbo
            dubbo-serialization-kryo
        com.cnbai
            apache-dubbo-provider-api
            ${project.parent.version}org.springframework.boot
                spring-boot-maven-plugin
                com.cnbai.apache.dubbo.provider.ProviderApplication

六、EchoServiceImpl

package com.cnbai.apache.dubbo.provider.service;

import com.cnbai.apache.dubbo.provider.api.EchoService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Value;

/**
 * 测试负载均衡,动态化配置
 *
 * @author bai
 * @since 2020/5/12
 */
@Service(version = "1.0.0")
public class EchoServiceImpl implements EchoService {

    /**
     * 测试负载均衡
     */
    @Value("${dubbo.protocol.port}")
    private String port;

    /**
     * 测试负载均衡,动态化配置
     */
    @Override
    public String echo(String string) {
        return "Echo Hello Dubbo " + string + " i am from port: " + port;
    }
}

七、ProviderApplication

package com.cnbai.apache.dubbo.provider;

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

/**
 * 服务提供者启动类
 *
 * @author bai
 * @since 2020/5/12
 */
@SpringBootApplication
public class ProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
}

八、application.yml

spring:
  application:
    name: dubbo-provider
  # 定义两个 id 相同的 bean 起冲突时,以后定义的为主
  main:
    allow-bean-definition-overriding: true

dubbo:
  scan:
    base-packages: com.cnbai.apache.dubbo.provider.service
  protocol:
    name: dubbo
    # 自动分配端口号 20880
    # port: -1
    port: 20881
    serialization: kryo
  registry:
    address: nacos://192.168.205.130:8848
  # 负载均衡策略 random 随机(默认),roundrobin 轮循,leastactive 最少活跃调用数,consistenthash 一致性 Hash
  provider:
    loadbalance: roundrobin

服务消费者 apache-dubbo-consumer

服务消费者

一、项目结构

技术图片

二、pom.xml

4.0.0com.cnbai
        apache-dubbo-demo
        0.0.1-SNAPSHOT

    apache-dubbo-consumer
    jarhttp://www.cnbai.xyz2020-Now服务消费者Apache 2.0https://www.apache.org/licenses/LICENSE-2.0.txtcnbaicnbaibaihaibo1024@163.comorg.springframework.boot
            spring-boot-starter-web
        org.springframework.boot
            spring-boot-starter-actuator
        org.springframework.boot
            spring-boot-starter-test
            testcom.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-config
        org.apache.dubbo
            dubbo-spring-boot-starter
        org.apache.dubbo
            dubbo-spring-boot-actuator
        org.apache.dubbo
            dubbo
        com.alibaba
            dubbo-registry-nacos
        com.alibaba.nacos
            nacos-client
        com.alibaba.spring
            spring-context-support
        org.apache.dubbo
            dubbo-serialization-kryo
        com.cnbai
            apache-dubbo-provider-api
            ${project.parent.version}org.springframework.boot
                spring-boot-maven-plugin
                com.cnbai.apache.dubbo.consumer.ConsumerApplication

三、EchoController

package com.cnbai.apache.dubbo.consumer.controller;

import com.cnbai.apache.dubbo.provider.api.EchoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试负载均衡,动态化配置
 *
 * @author bai
 * @since 2020/5/12
 */
@RefreshScope
@RestController
public class EchoController {

    @Reference(version = "1.0.0")
    private EchoService echoService;

    /**
     * 测试动态化配置
     */
    @Value("${user.name}")
    private String username;

    /**
     * 测试 nacos 动态化配置
     *
     * @param string string
     * @author cnbai
     * @return {@link String}
     * @since 2020/5/12
     */
    @GetMapping(value = "/echo/{string}")
    public String echo(@PathVariable String string) {
        return echoService.echo(string) + " " + username;
    }
}

四、ConsumerApplication

package com.cnbai.apache.dubbo.consumer;

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

/**
 * 服务消费者启动类
 *
 * @author bai
 * @since 2020/5/12
 */
@SpringBootApplication
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
}

五、bootstrap.properties

##########################################################################
#
# Spring Boot 配置文件的加载顺序:
# bootstrap.properties
# > bootstrap.yml
# > application.properties
# > application.yml
#
# 注:bootstrap.properties 配置为最高优先级一定会加载
#
##########################################################################
spring.application.name=dubbo-consumer-config
spring.cloud.nacos.config.server-addr=192.168.205.130:8848
spring.cloud.nacos.config.file-extension=yaml

六、dubbo-consumer-config.yaml

注:
yml文件都写在Nacos配置管理中,Nacos中的配置不能写注释,要删掉
yml文件都写在Nacos配置管理中,Nacos中的配置不能写注释,要删掉
yml文件都写在Nacos配置管理中,Nacos中的配置不能写注释,要删掉

技术图片

spring:
  application:
    name: dubbo-consumer
  main:
    allow-bean-definition-overriding: true

dubbo:
  scan:
    base-packages: com.cnbai.apache.dubbo.consumer.controller
  protocol:
    name: dubbo
    port: -1
    serialization: kryo
  registry:
    address: nacos://192.168.205.130:8848

server:
  port: 8080

# Dubbo 端口检查
endpoints:
  dubbo:
    enabled: true
    # Dubbo 健康检查
management:
  health:
    dubbo:
      status:
        defaults: memory
        extras: threadpool
  # web 健康检查
  endpoints:
    web:
      exposure:
        include: "*"

# 测试 Nacos 动态化配置
user:
  name: "只狼"
  # name: "条狗"  

代码

https://github.com/baihaibo1024/apache-dubbo-demo

搭建Apache Dubbo工程

标签:prot   int   ram   hose   evel   user   active   插入   group   

原文地址:https://www.cnblogs.com/cnbai/p/12879804.html


评论


亲,登录后才可以留言!