1. 程式人生 > >SpringCloud專案整體架構搭建(一)

SpringCloud專案整體架構搭建(一)

springcloud學習總結

1、專案整體架構搭建

一、新建maven父工程,用來管理專案的jar
在這裡插入圖片描述
如圖所示,點選next
在這裡插入圖片描述
繼續next next 直到finish

編寫pom檔案,管理子模組jar依賴

	<properties>
		<!--配置方式專案編碼-->
        <project.build.sourceEncoding>UTF8</project.build.sourceEncoding>
        <!--配置專案編譯打包的java版本-->
        <maven.
compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <junit.version>4.12</junit.version> <log4j.version>1.2.17</log4j.version> <lombok.version>1.16.18</lombok.version> <
/properties> <dependencyManagement> <dependencies> <!--SpringCloud版本--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <
version>
Dalston.SR1</version> <type>pom</type> <scope>import</scope> </dependency> <!--SpringBoot版本--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.5.9.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-bootstarter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.1.11</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> </dependencies> </dependencyManagement> <build> <finalName>springcloud</finalName> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <plugins> <!--配置一個外掛,在resources路徑下的以$開頭並結尾之間的為變數,可直接取出來--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <configuration> <delimiters> <delimit>$</delimit> </delimiters> </configuration> </plugin> </plugins> </build>

二、新建服務例項api模組
在這裡插入圖片描述

修改pom檔案,增加依賴

<!-- 當前Module需要用到的jar包,按自己需求新增,如果父類已經包含了,可以不用寫版本號 -->
<dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

新增實體類Dept

package com.atguigu.springcloud.entities;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
@NoArgsConstructor
//@AllArgsConstructor
@Data
@Accessors(chain=true)
public class Dept implements Serializable// entity --orm--- db_table
{
	private Long 	deptno; // 主鍵
	private String 	dname; // 部門名稱
	private String 	db_source;// 來自那個資料庫,因為微服務架構可以一個服務對應一個數據庫,同一個資訊被儲存到不同資料庫
	public Dept(String dname)
	{
		super();
		this.dname = dname;
	}
}

三、新建服務註冊中心eureka7001模組

New Module

修改pom檔案

<dependencies>
        <!--eureka-server服務端    這裡依賴帶server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <!-- 修改後立即生效,熱部署 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

編寫application.yml檔案

server: 
  port: 7001
 
eureka: 
  instance:
    hostname: eureka7001.com #eureka服務端的例項名稱
    #注意:這裡要在hosts檔案裡面編寫配置對映  後面需要配置eureka叢集以及路由,索性一次性配置完成
    #127.0.0.1       eureka7001.com
	#127.0.0.1       eureka7002.com
	#127.0.0.1       eureka7003.com
	#127.0.0.1       zuul5555.com
  client: 
    register-with-eureka: false     #false表示不向註冊中心註冊自己。
    fetch-registry: false     #false表示自己端就是註冊中心,我的職責就是維護服務例項,並不需要去檢索服務
    service-url:
     
      #設定與Eureka Server互動的地址查詢服務和註冊服務都需要依賴這個地址(單機)。
      
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/      

增加主啟動類

package com.atguigu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer // EurekaServer伺服器端啟動類,接受其它微服務註冊進來
public class EurekaServer7001_App
{
 public static void main(String[] args)
 {
 	SpringApplication.run(EurekaServer7001_App.class, args);
 }
}

四、新建服務提供者provider8001模組

New Module

修改pom檔案

<dependencies>
        <!-- 引入自己定義的api通用包,可以使用Dept部門Entity -->
        <dependency>
            <groupId>springcloud</groupId>
            <artifactId>springcloud-api</artifactId>
            <version>${project.version}</version>
        </dependency>

        <!-- actuator監控資訊完善 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- 將微服務provider側註冊進eureka   這裡依賴不帶server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <!--使用jetty容器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <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>
        </dependency>
        <!-- 修改後立即生效,熱部署 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

編寫application.yml檔案

server:
  port: 8001
  
mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml        # mybatis配置檔案所在路徑
  type-aliases-package: com.atguigu.springcloud.entities    # 所有Entity別名類所在包
  mapper-locations:
  - classpath:mybatis/mapper/**/*.xml                       # mapper對映檔案
    
spring:
   application:
   #特別重要!!
    name: microservicecloud-dept 
   datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # 當前資料來源操作型別
    driver-class-name: com.mysql.jdbc.Driver              # mysql驅動包
    url: jdbc:mysql://localhost:3306/cloudDB01              # 資料庫名稱
    username: root
    password: 123321
    dbcp2:
      min-idle: 5                                           # 資料庫連線池的最小維持連線數
      initial-size: 5                                       # 初始化連線數
      max-total: 5                                          # 最大連線數
      max-wait-millis: 200                                  # 等待連接獲取的最大超時時間
      
eureka:
  client: #客戶端註冊進eureka服務列表內
    service-url:
      defaultZone: http://localhost:7001/eureka/
  instance:
    instance-id: microservicecloud-dept8001 #自定義服務名稱資訊
    prefer-ip-address: true     #訪問路徑可以顯示IP地址
#
#當在eureka裡面點選服務名字時,返回json而不是404
info:
  app.name: atguigu-microservicecloud
  company.name: www.atguigu.com
  
  #可能是我父pom裡面的外掛配置的有問題,以下兩個變數的值我沒取到
  build.artifactId: $project.artifactId$
  build.version: $project.version$

業務邏輯編寫
mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.atguigu.springcloud.dao.DeptDao">

	<select id="findById" resultType="com.atguigu.springcloud.entities.Dept" parameterType="java.lang.Long">
		select deptno,dname,db_source from dept where deptno=#{deptno};
	</select>
	<select id="findAll" resultType="com.atguigu.springcloud.entities.Dept">
		select deptno,dname,db_source from dept;
	</select>
	<insert id="addDept" parameterType="com.atguigu.springcloud.entities.Dept">
		INSERT INTO dept(dname,db_source) VALUES(#{dname},DATABASE());
	</insert>

</mapper>

dao

package com.atguigu.springcloud.dao;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import com.atguigu.springcloud.entities.Dept;

@Mapper
public interface DeptDao
{
	public boolean addDept(Dept dept);

	public Dept findById(Long id);

	public List<Dept> findAll();
}

service

package com.atguigu.springcloud.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.atguigu.springcloud.dao.DeptDao;
import com.atguigu