前言:Nacos配置了权限控制,Nginx代理了Nacos,Nginx配置了Https,之后需要在SpringBoot配置Nacos,跟内网环境配置的有点不一样,下面具体介绍下如何配置。

一、Nginx配置

本地服务

192.168.8.245:8848

代理地址

https://www.xiaoqiangzai.xyz/nacos

image-20210815150705262

代理配置

# 注册中心
location /nacos/ {
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $http_host:$server_port;
    proxy_pass http://192.168.8.245:8848/nacos/;
}

image-20210815150616777

二、SpringBoot配置

相关依赖

<dependencyManagement>
    <dependencies>
        <!-- SpringCloudAlibaba依赖管理 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>2.2.2.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!-- SpringCloudNetflix依赖管理 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.SR8</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!-- SpringBoot依赖管理 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.3.2.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

注册发现依赖

<!-- 连接nacos客户端 -->
<dependency>
    <groupId>com.alibaba.nacos</groupId>
    <artifactId>nacos-client</artifactId>
    <version>1.2.1</version>
</dependency>
<!-- 服务注册与发现 -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    <exclusions>
        <exclusion>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!-- 分布式配置中心 -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    <exclusions>
        <exclusion>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
        </exclusion>
    </exclusions>
</dependency>

SpringBoot配置

bootstrap.yml

server:
  port: 8080

spring:
  application:
    name: gateway-service
  cloud:
    nacos:
      config:
        server-addr: https://www.xiaoqiangzai.xyz/
        namespace: 41b65de4-83fa-4445-aba2-de081d18e789
        username: pavilion_dev
        password: 123456
        file-extension: yml
        context-path: /nacos
        refresh-enabled: true
      discovery:
        server-addr: https://www.xiaoqiangzai.xyz/
        namespace: 41b65de4-83fa-4445-aba2-de081d18e789
        username: pavilion_dev
        password: 123456

image-20210815161003048

启动成功

image-20210815161505725

注册成功

image-20210815161528506

当账号密码错误时或者改账号没有权限时会报403权限不足

image-20210815161950476