Spring Cloud alibaba之一 部署nacos & 集成dubbo

环境
  nacos-2.2.0
  Spring Cloud-2.1.2.RELEASE
  CentOS Linux release 7.9.2009 (Core)
  部署三台虚拟机IP:192.168.6.102:8848,192.168.6.103:8848,192.168.6.104:8848

一、部署nacos
1、下载、编译nacos

#下载代码 这里下载2.2.0版本
git clone -b 2.2.0 https://github.com/alibaba/nacos.git
#构建
cd nacos/
mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U 
#查看构建结果
ls -al distribution/target/
cd distribution/target/nacos-server-2.2.0/nacos/

 将distribution/target/nacos-server-2.2.0 下nacos上传到服务器即可

  2、数据库配置

(1)mysql创建UTF8数据的数据库:nacos_config,然后使用navicat接连该数据库执行脚本:nacos/conf/mysql-schema.sql

(2)修改nacos数据库连接:nacos/conf/application.properties

#*************** Config Module Related Configurations ***************#
### If use MySQL as datasource:  如果使用mysql 开启这一行注释
spring.datasource.platform=mysql

### Count of DB:
db.num=1

### Connect URL of DB:
db.url.0=jdbc:mysql://node102:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user.0=root
db.password.0=123456

3、集群配置 

复制一份集群配置文件:cp nacos/conf/cluster.conf.example nacos/conf/cluster.conf

#2023-01-12T11:24:11.094
192.168.6.102:8848
192.168.6.103:8848
192.168.6.104:8848

4、启停

#启动
[root@node104 bin]# sh ./startup.sh
/usr/local/jdk1.8.0_271/bin/java -Djava.ext.dirs=/usr/local/jdk1.8.0_271/jre/lib/ext:/usr/local/jdk1.8.0_271/lib/ext  -server -Xms2g -Xmx2g -Xmn1g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/usr/local/nacos/logs/java_heapdump.hprof -XX:-UseLargePages -Dnacos.member.list= -Xloggc:/usr/local/nacos/logs/nacos_gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M -Dloader.path=/usr/local/nacos/plugins,/usr/local/nacos/plugins/health,/usr/local/nacos/plugins/cmdb,/usr/local/nacos/plugins/selector -Dnacos.home=/usr/local/nacos -jar /usr/local/nacos/target/nacos-server.jar  --spring.config.additional-location=file:/usr/local/nacos/conf/ --logging.config=/usr/local/nacos/conf/nacos-logback.xml --server.max-http-header-size=524288
nacos is starting with cluster
nacos is starting,you can check the /usr/local/nacos/logs/start.out
#停止
[root@node104 bin]# sh ./shutdown.sh
The nacosServer(1826) is running...
Send shutdown request to nacosServer(1826) OK

5、控制台

http://192.168.6.102:8848/nacos/    用户名/密码:nacos/nacos
http://192.168.6.103:8848/nacos/    用户名/密码:nacos/nacos
http://192.168.6.104:8848/nacos/    用户名/密码:nacos/nacos

 

 

二、集成dubbo

 

 0、代码模块整体pom

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zhss.demo</groupId>
    <artifactId>demo-dubbo-nacos</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <modules>
        <module>demo-dubbo-nacos-api</module>
        <module>demo-dubbo-nacos-ServiceA</module>
        <module>demo-dubbo-nacos-ServiceB</module>
    </modules>

</project>

1、接口模块pom

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>demo-dubbo-nacos</artifactId>
        <groupId>com.zhss.demo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>demo-dubbo-nacos-api</artifactId>


</project>

2、服务提供者

2.1 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>demo-dubbo-nacos</artifactId>
        <groupId>com.zhss.demo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>demo-dubbo-nacos-ServiceA</artifactId>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <!-- spring-cloud依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter</artifactId>
            <version>2.1.2.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-context</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- dubbo依赖 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.zhss.demo</groupId>
            <artifactId>demo-dubbo-nacos-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- nacos依赖 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
            <version>2.1.1.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-context</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-context</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
    </dependencies>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.1.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>
View Code

2.2 application.properties

#微服务名称
spring.application.name=demo-dubbo-nacos-ServiceA
#扫描包
dubbo.scan.base-packages=com.zhss.demo.dubbo.nacos
#dubbo使用的协议名称、监听端口
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
#dubbo所使用的注册中心(这里使用spring-cloud中集成的注册中心,当然也可以是zk)
dubbo.registry.address=spring-cloud://localhost
#注册中心地址
spring.cloud.nacos.discovery.server-addr=192.168.6.102:8848,192.168.6.103:8848,192.168.6.104:8848

3、服务消费者pom

2.1 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>demo-dubbo-nacos</artifactId>
        <groupId>com.zhss.demo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>demo-dubbo-nacos-ServiceB</artifactId>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>
        <!-- spring-cloud依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>
        <!-- dubbo依赖 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.zhss.demo</groupId>
            <artifactId>demo-dubbo-nacos-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- nacos依赖 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
            <version>2.1.1.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-context</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-context</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
        <!-- http依赖 用于cntroller测试 -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.5</version>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>
View Code

2.2 application.properties

#微服务名称
spring.application.name=demo-dubbo-nacos-ServiceB
#dubbo订阅的微服务名称
dubbo.cloud.subscribed-services=demo-dubbo-nacos-ServiceA
#扫描包
dubbo.scan.base-packages=com.zhss.demo.dubbo.nacos
#注册中心地址
spring.cloud.nacos.discovery.server-addr=192.168.6.102:8848,192.168.6.103:8848,192.168.6.104:8848

4、nacos查看dubbo服务列表

 

 

5、代码参考:https://github.com/cac2020/deme-nacos-dubbo

posted @ 2023-01-12 14:24  cac2020  阅读(421)  评论(0编辑  收藏  举报