唯读---项目基础搭建

唯读---项目基础搭建

一、建立父工程

  1. 第一步

    file-->new-->project

image
  1. 第二步

    选择maven,不选择模板,点击next

    image

输入 GroupId: com.wedo.parent ArtifactId:wedo 下一步

image

选择代码空间-->finish

  1. 修改pom.xml文件

    
    
        4.0.0
    
        com.wedo.parent
        wedo
        1.0.0-SNAPSHOT
    
        pom
    
        wedo
    
        
            org.springframework.boot
            spring-boot-starter-parent
            2.0.4.RELEASE
             
        
    
        
            UTF-8
            UTF-8
            1.8
            Finchley.SR2
            1.3.2
            2.0.2
            5.1.46
            1.2.3
            1.0.0-SNAPSHOT
            1.26.1-RELEASE
            1.0
            2.9.7
            5.0.6.RELEASE
            RELEASE
            2.1.0.M4
            3.3.1
            1.0.0
            2.1.0.M4
            5.1.0.RELEASE
            3.1.0
            0.9.1
            0.0.3
            2.8.0
            2.1.0.RELEASE
            2.0.0.RELEASE
            1.3.7
        
    
        
            
                
                
                    org.springframework.cloud
                    spring-cloud-dependencies
                    ${spring-cloud.version}
                    pom
                    import
                
                
                
                    org.mybatis.spring.boot
                    mybatis-spring-boot-starter
                    ${mybatis.starter.version}
                
                
                
                    tk.mybatis
                    mapper-spring-boot-starter
                    ${mapper.starter.version}
                
                
                
                    com.github.pagehelper
                    pagehelper-spring-boot-starter
                    ${pageHelper.starter.version}
                
                
                
                    mysql
                    mysql-connector-java
                    ${mysql.version}
                
                
                
                    com.github.tobato
                    fastdfs-client
                    ${fastDFS.client.version}
                
            
        
    
    
    
        
            
                
                    org.springframework.boot
                    spring-boot-maven-plugin
                
            
        
    
    

    ps:这个因为是以前写过的项目的依赖,现在又拉过来用,可能fastDFS暂时用不到,这个项目用的华为云OBS存储,前一阵子参加华为云的比赛,给了一张2000元代金券,然后买了一年的ECS和OBS,都是低配,现在不确定代码写完之后跑不跑的起来:smile:

  2. wedo的src文件夹可以删除了

二、搭建Eureka注册中心

  1. 右键点击wedo-->new-->module选择maven,不使用模板,下一步


    image
  1. 修改GroupId以及ArtifactId如图所示,修改完成后点击下一步

    image
  1. Module name记得加上-,如图所示,之后点击完成

    image
  1. 修改pom.xml

    
    
        
            wedo
            com.wedo.parent
            1.0.0-SNAPSHOT
        
        4.0.0
    
        com.wedo.registry
        wedo-registry
        1.0.0-SNAPSHOT
    
        
            
                org.springframework.cloud
                spring-cloud-starter-netflix-eureka-server
            
        
    
    
    
    
  1. 然后配置springboot启动类和application.yml文件

    WedoRegistryApplication.java

    package com.wedo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @SpringBootApplication
    @EnableEurekaServer
    public class WedoRegistryApplication {
        public static void main(String[] args) {
            SpringApplication.run(WedoRegistryApplication.class);
        }
    }
    
    

    application.yml

    server:
      port: 10086
    spring:
      application:
        name: wedo-registry
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:10086/eureka
        register-with-eureka: false
        fetch-registry: false
      server:
        enable-self-preservation: false
        eviction-interval-timer-in-ms: 10000
    
image

三、搭建zuul网关微服务

  1. 前三步和搭建Eureka(wedo-registry)一致,不过注意GroupId改成com.wedo.gateway,ArtifactId改为wedo-gateway,同时finish之前注意“-”问题

  2. 配置pom.xml

    
    
        
            wedo
            com.wedo.parent
            1.0.0-SNAPSHOT
        
        4.0.0
    
        com.wedo.gateway
        wedo-gateway
        1.0.0-SNAPSHOT
    
        
            
                org.springframework.cloud
                spring-cloud-starter-netflix-eureka-client
            
            
                org.springframework.cloud
                spring-cloud-starter-netflix-zuul
            
        
    
    
    
  3. 配置启动类和application.yml配置文件

    image

WedoGatewayApplication.java

package com.wedo;


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

@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class WedoGatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(WedoGatewayApplication.class);
    }
}

application.yml

server:
  port: 10010
spring:
  application:
    name: wedo-gateway
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka
    registry-fetch-interval-seconds: 5
zuul:
  prefix: /api
  routes:
    # book-service: /book/**
    # search-service: /search/**
  1. 在如图目录下新建WedoCorsConfiguration(解决跨域)

[图片上传失败...(image-9046d5-1601977147698)]

package com.wedo.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class WedoCorsConfiguration {
    @Bean
    public CorsFilter corsFilter() {

        // 初始cors配置对象
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 允许的域,不要写*,否则cookie就无法使用了
        corsConfiguration.addAllowedOrigin("http://manage.wedo.com");
        corsConfiguration.addAllowedOrigin("http://www.wedo.com");
        corsConfiguration.addAllowedOrigin("http://localhost:8080");
        // 允许携带Cookie信息
        corsConfiguration.setAllowCredentials(true);
        // 允许的请求方式, * 代表允许所有请求方式s
        corsConfiguration.addAllowedMethod("*");
        // 允许的头信息, * 代表所有的请求方法
        corsConfiguration.addAllowedHeader("*");

        // 初始化cors配置源对象
        UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
        configurationSource.registerCorsConfiguration("/**",corsConfiguration);

        // 返回CorsFilter实例, 参数: cors配置源对象
        return new CorsFilter(configurationSource);
    }
}

注意:我的电脑上修改了host文件,cros这里面几个域名都是指向127.0.0.1的:smile:

四、项目启动

调出services界面

image

点击刚刚写好的两个微服务,运行,启动成功后,浏览器访问localhost:10086,可以看到网关微服务已经注册到注册中心了

image

看到这个即为成功

上一篇:唯读---图书微服务接口设计
下一篇:唯读---图书微服务

你可能感兴趣的:(唯读---项目基础搭建)