Spring Cloud Alibaba + Dubbo + Nacos 整合例子

前言

我一共搭建了3个环境。
整体入口:
https://blog.csdn.net/lwb314/article/details/108222433

注意事项

  1. 这次使用的依赖是spring-cloud-starter-dubbo,这个我在git上没搜到,最后我发现他是spring-cloud-alibaba下的,spring-cloud-starter下的子项目,maven仓库里也找了一会才找到的,这个jar其实依赖的 例子2 里的dubbo-spring-boot-starter,所以这个例子应该跟例子2差不多
  2. 个人觉得还是使用这个例子3的比较靠谱,都知道在spring cloud家族里现在有个spring cloud alibaba,使用的是一套阿里的技术方案,性能更高,使用也更方便,我个人肯定是拥抱阿里,虽然阿里内部用的也不是这套开源技术╮(╯▽╰)╭无奈。不过毕竟像国外这些开源android,apache,spring这些东西可能用着用着就不让用了,呵呵。

代码

快速搭建

我的开发工具是IDEA,使用的是快速构建项目的Spring Initializr,前两个项目也是这么构建的,为什么要在这里单独提一下,因为这里必须要用阿里云快速构建,而不是用默认的spring快速构建,阿里云的构建地址是http://start.aliyun.com
具体看图
在这里插入图片描述
这页没啥说了,取个工程名,选下jdk版本
在这里插入图片描述
这一步一共有3个要注意的点,我已经圈出来了
第一个dubbo依赖,这个只有在阿里云里可以搜到,在spring-starter里搜不到,所以第一张图必须选阿里云;
第2个是版本使用spring boot 2.2这个大版;
第3个是我已经选好的2个依赖一个是spring boot web ,一个是nacos依赖,最后再把dubbo这个依赖选上就完成了。
在这里插入图片描述

依赖pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.lwb</groupId>
    <artifactId>spring-cloud-alibaba-dubbo-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud-alibaba-dubbo-provider</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.2.7.RELEASE</spring-boot.version>
        <spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-actuator</artifactId>-->
<!--        </dependency>-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.2.7.RELEASE</version>
                <configuration>
                    <mainClass>com.lwb.springcloudalibabadubboprovider.SpringCloudAlibabaDubboProviderApplication
                    </mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

服务生产者代码-provider

application.properties

server.port=8003
spring.application.name=alibabadubboprovider
dubbo.application.id=alibabadubboprovider
dubbo.application.name=alibabadubboprovider
# 禁用QOS同一台机器可能会有端口冲突现象
dubbo.application.qos-enable=false
dubbo.application.qos-accept-foreign-ip=false
# 配置注册中心
dubbo.registry.address=nacos://10.10.171.249:8848
spring.cloud.nacos.discovery.server-addr=10.10.171.249:8848

dubbo.protocol.name=dubbo
dubbo.protocol.port=20883

dubbo.protocol.id=dubbo

启动类

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableDubbo
@SpringBootApplication
public class SpringCloudAlibabaDubboProviderApplication {

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

}

服务实现类

@Service(version = "1.0.2",group = "lwb")
public class DubboDemoServiceImpl implements DubboDemoService {
    @Override
    public String helloDubbo() {
        return "helloDubboAlibaba";
    }
}

服务接口

public interface DubboDemoService {
    public String helloDubbo();
}

服务消费者代码-consumer

package com.lwb.springcloudalibabadubboconsumer;

import com.lwb.DubboDemoService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.annotation.PostConstruct;

@EnableDubbo
@SpringBootApplication
public class SpringCloudAlibabaDubboConsumerApplication {

    @Reference(version = "1.0.2", check = false, group = "lwb")
    private DubboDemoService demoService;

    @PostConstruct
    public void init() {
        String a = demoService.helloDubbo();
        System.out.println(a);
    }

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

}

application.properties

server.port=8004
# 禁用QOS同一台机器可能会有端口冲突现象
spring.application.name=dubbo-starter-consumer
dubbo.application.name=dubbo-starter-consumer
dubbo.application.id=dubbo-starter-consumer
dubbo.application.qos-enable=false
dubbo.application.qos-accept-foreign-ip=false
# 配置注册中心
dubbo.registry.address = nacos://10.10.171.249:8848
spring.cloud.nacos.discovery.server-addr=10.10.171.249:8848
dubbo.registry.register=false

#dubbo.protocol.id=dubbo


遇坑总结

  1. spring.application.name= 不写报错:提示使用spring.application.name的时候报错了,写上了但是并不能改变nacos上的服务名。
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'targeterBeanPostProcessor' defined in class path resource [com/alibaba/cloud/dubbo/autoconfigure/DubboOpenFeignAutoConfiguration.class]: Unsatisfied dependency expressed through method 'targeterBeanPostProcessor' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.application.name' in value "${spring.application.name}"
  1. spring.cloud.nacos.discovery.server-addr= 不写报错,我的nacos服务器不在本地,日志却提示连本地连不上,肯定是因为我没写nacos地址
com.alibaba.nacos.api.exception.NacosException: failed to req API:/api//nacos/v1/ns/service/list after all servers([localhost:8848]) tried: java.net.ConnectException: Connection refused: connect
  1. dubbo.application.id=不写报错:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springCloudAlibabaDubboConsumerApplication': Injection of @Reference dependencies is failed; nested exception is java.lang.IllegalStateException: Invalid name="org.apache.dubbo.config.ApplicationConfig#0" contains illegal character, only digit, letter, '-', '_' or '.' is legal.

  1. 比前2个例子多出2个服务,是元数据,具体作用未知,怎么下掉未知,未来会慢慢研究。
    在这里插入图片描述

最近我准备从0搭建一套基于Spring Cloud Alibaba的微服务框架

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页