Redis项目(一)项目搭建

Redis项目(一)项目搭建

项目架构

基于微服务进行开发,微服务是目前比较热门的架构方式,具有以下特点:

  • 职责单一:理论上一个微服务只解决一件事(小)
  • 隔离性强:服务单独部署,服务之间互相隔离,互不影响,因此一个服务宕机并不影响其他服务运行。

Redis项目(一)项目搭建_第1张图片

项目基础搭建

基于Spring Cloud Hoxton.SR8搭建。

第一步:创建Maven的父级工程,添加对应依赖


<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.0modelVersion>

<groupId>com.fengzigroupId>
<artifactId>food-social-contact-parentartifactId>
<packaging>pompackaging>
    <modules>
        <module>ms-registrymodule>
    modules>
    <version>1.0-SNAPSHOTversion>




<properties>
    <spring-boot-version>2.3.5.RELEASEspring-boot-version>
    <spring-cloud-version>Hoxton.SR8spring-cloud-version>
    <lombok-version>1.18.16lombok-version>
    <commons-lang-version>3.11commons-lang-version>
    <mybatis-starter-version>2.1.3mybatis-starter-version>
    <mysql-version>8.0.22mysql-version>
    <swagger-starter-version>2.1.5-RELEASEswagger-starter-version>
    <hutool-version>5.4.7hutool-version>
    <guava-version>20.0guava-version>
    <maven.compiler.source>1.8maven.compiler.source>
    <maven.compiler.target>1.8maven.compiler.target>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <springfox.version>3.0.0springfox.version>

properties>


<dependencyManagement>
    <dependencies>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-dependenciesartifactId>
            <version>${spring-boot-version}version>
            <type>pomtype>
            <scope>importscope>
        dependency>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-dependenciesartifactId>
            <version>${spring-cloud-version}version>
            <type>pomtype>
            <scope>importscope>
        dependency>
        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>${lombok-version}version>
        dependency>
        
        <dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-lang3artifactId>
            <version>${commons-lang-version}version>
        dependency>
        
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>${mybatis-starter-version}version>
        dependency>
        

        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-boot-starterartifactId>
            <version>${springfox.version}version>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>${mysql-version}version>
        dependency>
        
        <dependency>
            <groupId>cn.hutoolgroupId>
            <artifactId>hutool-allartifactId>
            <version>${hutool-version}version>
        dependency>
        
        <dependency>
            <groupId>com.google.guavagroupId>
            <artifactId>guavaartifactId>
            <version>${guava-version}version>
        dependency>
    dependencies>
dependencyManagement>

<build>
    <pluginManagement>
        <plugins>
            
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    pluginManagement>
build>
project>

第二步:创建基于Eureka的注册中心微服务

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
        dependency>
    dependencies>
server:
  port: 8080

spring:
  application:
    name: ms-registry

# 配置 Eureka Server 注册中心
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:8080/eureka/

第三步:添加网关微服务

    <dependencies>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-gatewayartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>
    dependencies>
server:
  port: 80

spring:
  application:
    name: ms-gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true # 开启配置注册中心进行路由功能
          lower-case-service-id: true # 将服务名称转小写
      routes:
        - id: ms-diners # 这里的id是标识这个路由规则的唯一标识符
          uri: lb://ms-diners #lb:负载均衡
          predicates:
            - Path=/hello/**  #路径为 /hello/** 的请求转发到名为 ms-diners 的微服务
# 配置 Eureka Server 注册中心
eureka:
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
  client:
    service-url:
      defaultZone: http://localhost:8080/eureka/

第四步:添加用户食客微服务

        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
server:
  port: 8081 # 端口

spring:
  application:
    name: ms-diners # 应用名
@RestController
@RequestMapping("hello")
public class HelloController {

    @GetMapping
    public String hello(String name) {
        return "hello " + name;
    }

}

第五步:启动测试

访问localhost/hello?name=fengzi

因为网关端口80,80端口是浏览器默认端口,所以可以省略端口号

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-M2lWyR1H-1692857461427)(image-20230824114757524.png)]

public String hello(String name) {
    return "hello " + name;
}

}


### 第五步:启动测试

访问[localhost/hello?name=fengzi](http://localhost/hello?name=fengzi)

> 因为网关端口80,80端口是浏览器默认端口,所以可以省略端口号

[外链图片转存中...(img-M2lWyR1H-1692857461427)]



你可能感兴趣的:(Redis,redis,数据库,缓存)