微服务——创建首页微服务

如何创建一个父级SpringBoot工程

链接:https://blog.csdn.net/weixin_45528987/article/details/105302869

如何在window下安装SpringCloud alibaba的nacos中心

链接:https://blog.csdn.net/weixin_45528987/article/details/105303100

项目如何注册到nacos中心

链接:https://blog.csdn.net/weixin_45528987/article/details/105303245

第一步:创建一个Maven项目

在这里插入图片描述

第二步:添加需要的pom依赖

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

        <!-- 前端模板 thymeleaf 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>
        <!--自定义工具包-->
        <dependency>
            <groupId>com.zxf</groupId>
            <artifactId>common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

第三步:创建静态目录和首页html目录,并添加文件

在这里插入图片描述

第四步:新建application.yml配置文件,编写配置信息

server:
  port: 9000 #端口号

spring:
  application:
    name: home #服务名
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848 #nacos注册地址
  thymeleaf:
    prefix: classpath:templates/
    suffix: .html
    mode: HTML
    encoding: UTF-8
    servlet:
      content-type: text/html
    cache: false #不开启页面缓存

第五步:创建SpringBoot启动类,首页访问Controller类

package com.zxf;

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

@SpringBootApplication
@EnableDiscoveryClient
public class HomeApplication {

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

}
@Controller
public class IndexController {

    @GetMapping("index")
    public String index(){
        return "index";
    }

}

第六步:MVC静态资源路径映射配置

第一种:配置文件配置

  mvc:
    static-path-pattern: /static/**
  resources:
    static-locations: classpath:/static/

第二种:配置类配置

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    /**
     * 静态资源加载配置 没有此配置静态资源将会被视为请求向后端发送GET请求
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }

}

访问成功

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值