springcloud使用nacos做注册、配置中心

springcloud使用nacos做注册、配置中心

  • 环境准备
  • 创建项目(引入依赖pom)
  • 创建 bootstrap.yml
  • 启动配置
  • nacos上添加配置文件
  • 启动项目
  • 附加swagger UI文档配置

环境准备

  • nacos
  • idea
  • mysql

创建项目(引入依赖pom)

"1.0" encoding="UTF-8"?>
"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">
    4.0.0</modelVersion>
    pom</packaging>
    
        org.springframework.boot</groupId>
        spring-boot-starter-parent</artifactId>
        2.2.5.RELEASE</version>
        /> <!-- lookup parent from repository -->
    </parent>
    com.xq.vitea</groupId>
    gateway-manager</artifactId>
    0.0.1-SNAPSHOT</version>
    gateway-manager</name>
    Demo project for Spring Boot</description>

    
        .version>1.8</java.version>
    </properties>

    
    	
            org.springframework.boot</groupId>
            spring-boot-starter-web</artifactId>
        </dependency>
        <!--nacos注册中心-->
        
            com.alibaba.cloud</groupId>
            spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            2.2.0.RELEASE</version>
            
                
                    com.alibaba</groupId>
                    fastjson</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        
            com.alibaba</groupId>
            fastjson</artifactId>
            1.2.57</version>
        </dependency>

        <!--nacos配置中心 -->
        
            com.alibaba.cloud</groupId>
            spring-cloud-starter-alibaba-nacos-config</artifactId>
            2.2.0.RELEASE</version>
        </dependency>

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

        
            org.projectlombok</groupId>
            lombok</artifactId>
            true</optional>
        </dependency>
        <!--引入ui包 swagger doc.html-->
        
            com.github.xiaoymin</groupId>
            knife4j-spring-boot-starter</artifactId>
            2.0.1</version>
        </dependency>

        
            io.springfox</groupId>
            springfox-swagger2</artifactId>
            2.9.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
        
            org.springframework.cloud</groupId>
            spring-cloud-starter-openfeign</artifactId>
            2.2.2.RELEASE</version>
        </dependency>
        
            cn.hutool</groupId>
            hutool-all</artifactId>
            5.0.7</version>
        </dependency>
        
            org.apache.httpcomponents</groupId>
            httpclient</artifactId>
        </dependency>

    </dependencies>

    
        
            
                org.springframework.cloud</groupId>
                spring-cloud-dependencies</artifactId>
                Hoxton.SR3</version>
                <type>pom</type>
                runtime</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    
        
            
                org.springframework.boot</groupId>
                spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

创建 bootstrap.yml

spring:
  main:
    allow-bean-definition-overriding: true
  application:
    name: gateway-manager-auth(服务名)
  cloud:
    nacos:
      ################### 注册中心 #####################
      discovery:
        server-addr: ip:8848 # 对应nacos地址
        namespace: daba22a2-228e-4e62-9418-23ec9bab8eb9 #命名空间
      ################### 配置中心 #####################
      config:
        namespace: daba22a2-228e-4e62-9418-23ec9bab8eb9 #命名空间
        server-addr: ip:8848 # 对应nacos地址
        file-extension: yml
        extension-configs:
         - dataId: gateway-auth.yml
           group: DEFAULT_GROUP
           refresh: true # 动态刷新

启动配置

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class AuthorizationApplication {

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

nacos上添加配置文件

springcloud使用nacos做注册、配置中心_第1张图片

启动项目

注册成功
springcloud使用nacos做注册、配置中心_第2张图片

附加swagger UI文档配置

package com.xq.vitea.gateway.auth.config.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    /**
     * 创建API应用
     * apiInfo() 增加API相关信息
     * 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
     * 本例采用指定扫描的包路径来定义指定要建立API的目录。
     *
     * @return
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("包"))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 创建该API的基本信息(这些基本信息会展现在文档页面中)
     * 访问地址:http://项目实际地址/doc.html
     *
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("网关权限")
                .description("网关权限服务")
                .termsOfServiceUrl("")
                .version("2.0")
                .build();
    }
}

  • 打开 http://localhost:port/doc.html

springcloud使用nacos做注册、配置中心_第3张图片

源码地址:https://gitee.com/sharking_xq96/vitea-nacos-cloud.git

你可能感兴趣的:(springcloud)