首页 > 编程语言 > 详细

springboot + springcloud +nacos实战

时间:2019-12-18 10:10:11      阅读:66      评论:0      收藏:0      [点我收藏+]
首先从整个软件的功能和应用场景来说,nacos更像consul,而非eureka,nacos设计的时候自带的配置中心功能,让我们省下了去搞springcloud config的时间,但这里并不是说nacos或者consul比eureka优秀,只是因为eureka在分布式环境下更加注重的是可用性,而牺牲了他的最终一致性,在不同的领域上,nacos和eureka都有他们自己的建树,关键还是看具体的架构需求。废话不多说直接进入nacos的初探寻。

1.nacos服务端应用,启动

1.1 下载安装

nacos服务端对比euerka来说相对比较麻烦一点,需要我们自己去下载安装,而非引入跑程序
下载地址
https://github.com/alibaba/nacos/releases

1.2 解压,启动

选择完之后解压
linux下就是tar -zxvf nacos-server-1.0.0.tar.gz
windows就直接用软件解压即可

进入如软件bin目录下
linux 启动命令 sh startup.sh -m standalone

windows 直接双击即可

启动成功的话效果会是

 
技术分享图片

浏览器请求http://localhost:8848/nacos/index.html 可查看情况

新版本的nacos自带着用户登录验证,默认账号和密码都都输入nacos即可

 
技术分享图片
 

1.3 springcloud微服务以nacos为注册中心启动

1.3.1 父级项目配置

因为springcloud是一个集成了很多框架组件的一个项目,所以项目也为了方便把一些公用的依赖到父级的pom文件下其中的关键主要是

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>0.9.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

上图两个依赖分别是springboot的版本依赖,和springcloud alibaba的依赖,下面贴出来的就是详细的父级pom文件,下面打包的配置可以不用管,只需要看dependency的依赖即可

<?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.gitee.simons.cloud</groupId>
    <artifactId>simons-cloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>simons-cloud</name>
    <description>simons parent</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>0.9.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <properties>
        <spring-boot.version>2.1.3.RELEASE</spring-boot.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <app.version>0.0.1-SNAPSHOT</app.version>
        <java.version>1.8</java.version>
        <mediaroom.admin.version>2.0.4</mediaroom.admin.version>
        <jjwt.version>0.9.0</jjwt.version>
        <druid.version>1.0.14</druid.version>
        <crypto.version>0.5.7</crypto.version>
        <swagger2.version>2.8.0</swagger2.version>
        <mediaroom.util.version>2.6.6-SNAPSHOT</mediaroom.util.version>
        <mediaroom.middlemq.utils.version>2.0.0-SNAPSHOT</mediaroom.middlemq.utils.version>
        <mediaroom.j2ee.version>0.1.0-SNAPSHOT</mediaroom.j2ee.version>
        <curator.version>2.10.0</curator.version>
        <quartz.version>2.3.0</quartz.version>
        <fastjson.version>1.1.28</fastjson.version>
        <p3c-pmd.version>1.3.1</p3c-pmd.version>
        <maven-pmd-plugin.version>3.8</maven-pmd-plugin.version>
        <!--jsw daemon -->
        <daemon-name>service</daemon-name>
        <daemon-JAVA_Xms>1024M</daemon-JAVA_Xms>
        <daemon-JAVA_Xmx>1024M</daemon-JAVA_Xmx>


    </properties>

    <dependencies>

        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-yaml</artifactId>
        </dependency>

        <!-- spring aop -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>${mediaroom.admin.version}</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
                <!-- 工具 -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>19.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>3.2.4</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>

            <!-- PMD插件 -->
            <!--
                Alibaba p3c checkstyle
            -->
            <plugin>
                <!--
                    mvn -Dpmd.language=en pmd:check
                    mvn -Dpmd.language=en pmd:pmd
                -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>${maven-pmd-plugin.version}</version>
                <configuration>
                    <sourceEncoding>${project.build.sourceEncoding}</sourceEncoding>
                    <targetJdk>${java.version}</targetJdk>
                    <!-- minimumTokens>100</minimumTokens -->
                    <rulesets>
                        <ruleset>rulesets/java/ali-comment.xml</ruleset>
                        <ruleset>rulesets/java/ali-concurrent.xml</ruleset>
                        <ruleset>rulesets/java/ali-constant.xml</ruleset>
                        <ruleset>rulesets/java/ali-exception.xml</ruleset>
                        <ruleset>rulesets/java/ali-flowcontrol.xml</ruleset>
                        <ruleset>rulesets/java/ali-naming.xml</ruleset>
                        <ruleset>rulesets/java/ali-oop.xml</ruleset>
                        <ruleset>rulesets/java/ali-orm.xml</ruleset>
                        <ruleset>rulesets/java/ali-other.xml</ruleset>
                        <ruleset>rulesets/java/ali-set.xml</ruleset>
                        <ruleset>rulesets/vm/ali-other.xml</ruleset>
                    </rulesets>
                    <printFailingErrors>true</printFailingErrors>
                    <!-- 1: blocker ; 2: critical ; 3: major -->
                    <minimumPriority>2</minimumPriority>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>com.alibaba.p3c</groupId>
                        <artifactId>p3c-pmd</artifactId>
                        <version>${p3c-pmd.version}</version>
                    </dependency>
                </dependencies>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-source-plugin</artifactId>
                <configuration>
                    <attach>true</attach>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>versions-maven-plugin</artifactId>
            </plugin>
        </plugins>

    </build>

    <repositories>
        <repository>
            <id>maven-ali</id>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
                <checksumPolicy>fail</checksumPolicy>
            </snapshots>
        </repository>

        <!--<repository>-->
            <!--<id>pentaho-public</id>-->
            <!--<name>Pentaho Public</name>-->
            <!--<url>http://nexus.pentaho.org/content/groups/omni/</url>-->
            <!--<releases>-->
                <!--<enabled>true</enabled>-->
                <!--<updatePolicy>daily</updatePolicy>-->
            <!--</releases>-->
            <!--<snapshots>-->
                <!--<enabled>true</enabled>-->
                <!--<updatePolicy>interval:15</updatePolicy>-->
            <!--</snapshots>-->
        <!--</repository>-->

    </repositories>

    <modules>
        <module>simons-nacos</module>
        <module>simons-gateway</module>
        <module>simons-base</module>
    </modules>
</project>

父级项目创建完成之后,开始创建第一个springcloud alibaba的项目加入到nacos中,那么在下面父亲目录下创建一个springboot目录,名字叫simons-nacos,

 
技术分享图片
image.png

具体的文件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>
        <groupId>com.gitee.simons.cloud</groupId>
        <artifactId>simons-cloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>simons-nacos</artifactId>
    <name>${project.artifactId} [web]</name>
    <packaging>jar</packaging>

    <!-- 版本,字段关键字 -->
    <properties>
        <start-class>com.gitee.simons.nacos.NacosApplication</start-class>
    </properties>

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

<profiles>
        <profile>
            <id>spring-boot</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <version>${spring-boot.version}</version>
                        <configuration>
                            <mainClass>${start-class}</mainClass>
                        </configuration>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>repackage</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>


    </profiles>


    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <!--解決持續集成無法找到main類 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>${start-class}</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

NacosApplication

package com.gitee.simons.nacos;

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

@SpringBootApplication
@EnableDiscoveryClient
public class NacosApplication {

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

application.yml

server:
  port: 8111

spring:
  application:
    name: nacos
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848
      discovery:
        server-addr: 127.0.0.1:8848

management:
  endpoints:
    web:
      exposure:
        include: ‘*‘

启动即可完成


 
技术分享图片
 

springboot + springcloud +nacos实战

原文:https://www.cnblogs.com/green-light/p/12058239.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!