当前位置: 首页 > article >正文

配置nacos

解压资料中的nacos-server-1.2.0.zip 

进入bin目录双击  startup.cmd 运行文件 

访问http://localhost:8848/nacos

注册admin服务

1<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-alibaba-nacos-config</artifactId>
</dependency>

创建heima-leadnews-admin-gateway微服务 pom.xml

 <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.heima</groupId>
            <artifactId>heima-leadnews-common</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

heima-leadnews-user pom.xml

 <dependencies>
        <!-- 引入依赖模块 -->
        <dependency>
            <groupId>com.heima</groupId>
            <artifactId>heima-leadnews-common</artifactId>
        </dependency>
        <dependency>
            <groupId>com.heima</groupId>
            <artifactId>heima-leadnews-model</artifactId>
        </dependency>
        <!-- Spring boot starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

2application.yml  同时把application.yml改名application.yml.bak

 server:
  port: 9001
spring:
  application:
    name: leadnews-admin
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.137.136:8848
      config:
        server-addr: 192.168.137.136:8848
        file-extension: yml

gateway-application.yml

server:
  port: 6001
spring:
  application:
    name: leadnews-admin-gateway
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.137.136:8848
    gateway:
      globalcors:
        cors-configurations:
          '[/**]': # 匹配所有请求
            allowedOrigins: "*" #跨域处理 允许所有的域
            allowedHeaders: "*"
            allowedMethods: # 支持的方法
            - GET
            - POST
            - PUT
            - DELETE
            - OPTIONS
      routes:
      # 平台管理
      - id: admin
        uri: lb://leadnews-admin
        predicates:
        - Path=/admin/**
        filters:
        - StripPrefix= 1

userapplication.yml

server:
  port: 9002
spring:
  application:
    name: leadnews-user
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/leadnews_user?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false
    username: root
    password: 123456
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.137.136:8848
# 设置Mapper接口所对应的XML文件位置,如果你在Mapper接口中有自定义方法,需要进行该配置
mybatis-plus:
  #mapper-locations: classpath*:mapper/*.xml
  # 设置别名包扫描路径,通过该属性可以给包中的类注册别名
  #type-aliases-package: com.heima.model.user.pojos
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #输出sql日志

引导类AdminApplication中加上注解@EnableDiscoveryClient可以让该服务注册到nacos注册中心上去

启动nacos,启动admin微服务,可以查看到admin服务已经在服务列表中了

把application.yml.bak中的部分内容复制到Nacos中

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/leadnews_admin?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false
    username: root
    password: 123456
mybatis-plus:
  # 设置Mapper接口所对应的XML文件位置,如果你在Mapper接口中有自定义方法,需要进行该配置
  #mapper-locations: classpath*:mapper/*.xml
  # 设置别名包扫描路径,通过该属性可以给包中的类注册别名
  #type-aliases-package: com.heima.model.admin.pojos
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #输出sql日志

搭建admin前端工程

  • 通过nginx的反向代理功能访问后台的网关资源

  • 通过nginx的静态服务器功能访问前端静态页面

配置nginx

①:解压资料文件夹中的压缩包nginx-1.18.0.zip

②:解压资料文件夹中的前端项目admin-web.zip

③:配置nginx.conf文件

在nginx安装的conf目录下新建一个文件夹vhost,在当vhost文件夹中新建heima-leadnews-admin.conf文件

heima-leadnews-admin.conf配置如下:

  • listen 8803 -- 监听 8803端口,前端工程的端口

  • upstream 配置负载均衡

  • location ~/service_admin/(.*) 匹配 路径中包含service_6001的请求

  • proxy_pass 反向代理 到 http://heima-admin-gateway ,这是我们配置的负载均衡,前端先把请求发给nginx,由nginx把请求转发给网关 localhost:6001

  • upstream  heima-admin-gateway{
        server localhost:6001;
    }

    server {
        listen 8803;
        location / {
            root D:/workspace/admin-web/;
            index index.html;
        }
        
        location ~/service_admin/(.*) {
            proxy_pass http://heima-admin-gateway/$1;
            proxy_set_header HOST $host;  # 不改变源请求头的值
            proxy_pass_request_body on;  #开启获取请求体
            proxy_pass_request_headers on;  #开启获取请求头
            proxy_set_header X-Real-IP $remote_addr;   # 记录真实发出请求的客户端IP
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  #记录代理信息
        }
    }

  • 打开nginx.conf 引入heima-leadnews-admin.conf文件加载

  • # 引入自定义配置文件
    include vhost/*.conf;

启动nginx

在nginx安装包中使用命令提示符打开,输入命令 start nginx.exe 。启动项目

重启nginx:nginx -s reload

关闭nginx nginx -s stop

打开前端项目进行测试 -- > http://localhost:8803


http://www.kler.cn/a/579201.html

相关文章:

  • Maven 中 SNAPSHOT 版本与 RELEASE 版本的区别
  • 爬虫案例十一js逆向数位观察网
  • ALG(Alloy+Loki+Grafana)轻量级日志系统
  • deepseek为什么要开源
  • OINH系列(自编)---> <Page 2-[1]>题库原题(少)
  • 机器人匹诺曹机制,真话假话平衡机制
  • 蓝桥杯备考:图论初解
  • 2025年渗透测试面试题总结-小某鹏汽车-安全工程师(题目+回答)
  • K8S单机部署
  • 【HarmonyOS Next】鸿蒙加固方案调研和分析
  • INFINI Labs 产品更新 | Easysearch 增加异步搜索等新特性
  • 在Ubuntu上搭建Samba服务,实现与windows之间的文件共享
  • 软件测试の概念之测试分类质量模型测试用例
  • 【哇! C++】类和对象(五) - 赋值运算符重载
  • 文生图 图生视频 文生视频人工智能AI工具节选
  • 【django初学者项目】
  • 算法每日一练 (9)
  • Flutter 学习之旅 之 flutter 不使用插件,简单实现一个 Toast 功能
  • Web3 的未来:去中心化如何重塑互联网
  • 03.05 QT事件