從0手寫springCloud項目(組件搭建)

寫在前面

一直在寫springCloud項目,每次都是新建項目然後從零開始寫配置,現在寫一個儘量通用的項目,方便後續搭建框架的時候直接拿過去使用。

  1. 需要搭建的組件(模塊)有:
    eureka(認證),zuul(網關),auth(認證),config(配置中心),user(用戶),order(訂單),pay(支付),feign...
  2. 這邊主要想涉及到的框架技術有:springcloud,springboot2,oauth2,springSecurity,liquibase,lcn(5.0.2),mybatisplus,logback,redis,mysql,swagger2,poi
  3. 需要搭建、支持的技術
    github,jenkins(自動發佈),maven私服,nginx,redis,mysql5.7,jdk1.8,swagger2,rabbitmq
一 需要搭建的組件

需要搭建的組件主要有7個模塊(feign會集成到具體模塊),這邊我回詳細記錄eureka,zuul,auth,config,user.因爲前四者是springCloud的配置。需要詳細介紹,而具體的業務邏輯代碼會在具體模塊,這裏我將以user模塊爲例子詳細介紹.

  • eureka

我們知道,在爲服務裏面,所有模塊需要被註冊到一個註冊中心,持續的向註冊中心發送心跳以保證連接的存活。而springcloud的註冊中心有consul和eureka,這裏我選用的是eureka.
eureka的代碼很簡單,只需要在配置文件裏面配置好註冊地址與密碼(可不設置,生產上強烈建議設置),並標識好自己不向自己註冊,不被自己發現即可。

maven座標:

 <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--我是用的springboot2.1.3如果是springboot1.5.x請不用這個-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

主類,不用做任何配置

@SpringBootApplication
@EnableEurekaServer
public class CrawlerEurekaApplication {

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


yml配置文件:

spring:
  application:
    name: crawler-eureka

server:
  host: http://localhost
  port: 9990
eureka:
  client:
    fetch-registry: false
    register-with-eureka: false
    service-url:
      defaultZone: ${server.host}:${server.port}/eureka/
  instance:
    prefer-ip-address: true
  • zuul

上面我們把註冊中心搭建好了,訪問localhost:9990就可以看到eureka的控制檯。但是我們看不到一個服務註冊上去了。現在我們搭建一個網關,因爲在實際項目中,我們會有很多個微服務模塊,而服務器只會向外暴露一個端口,其他的通過相對路徑轉發。這樣也是爲了安全和方便管理,有點nginx的感覺。
網關的配置也不復雜:pom座標:

<dependencies>
        <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>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

主類除了標識爲eureka-client,還標識是網關

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

yml配置

server:
  port: 9996
spring:
  application:
    name: crawler-zuul
  redis:
    host: localhost
    port: 6379
    password: 123456

zuul:
  routes:
    feign-auth:
      path: /auth/**
      serviceId: crawler-auth
      strip-prefix: true
      custom-sensitive-headers: true
    feign-user:
      path: /user/**
      serviceId: crawler-goddess
      sensitiveHeaders:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:9990/eureka/
  instance:
    prefer-ip-address: true

logging:
  level:
    ROOT: info
    org.springframework.web: info

ribbon:
  ReadTimeout: 6000000
  SocketTimeout: 6000000
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 600000

啓動項目,再次打開localhost:9990可以發現多了一個crawler-zuul

  • auth

待寫

  • config

待寫
(這兩個模塊等我把後續文章寫完了,回過頭來補起來。下一篇文章主要介紹user模塊框架涉及以及涉及的主要技術點)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章