一 。搭建以springboot為基礎的微服務專案
注:本文為自己記錄,防止忘記
1.建立父pom工程並匯入依賴
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--eureka server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<!-- 整合lombok 框架 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- 整合redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 整合aop -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- 整合web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot整合activemq -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<!-- 整合傳送郵件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- 整合mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.3.7</version>
</dependency>
<!-- 阿里巴巴資料來源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.14</version>
</dependency>
<!-- httpclient -->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.30</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.3</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
版本可能會出現衝突,主要是springcloud導致,自己嘗試幾個版本之後發現這麼不報錯,具體也不知道為什麼
2.在父pom工程下建立api工程,裡面都是介面和介面地址,用於給外部訪問(jar型別),同時建立一個相應的具體工程(jar型別,實現具體的業務邏輯,這裡我們先建立一個member工程,即會員服務),並將api工程引入,那麼具體工程中就建立類的時候就直接繼承api工程就行了,然後實現具體的業務邏輯(如,資料庫操作,連線mq,連線redis,具體的配置檔案就不貼了),另外所有的服務都要連線註冊中心(eureka),eureka工程就是建立一個jar工程然後配置一下,啟動就行了
配置檔案:
server:
port: 8761 ##這裡是埠號可以自己進行改
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
這時候基本的搭建已將完事了,然後把上面說到的member的配置檔案貼一下
server:
port: 8762
# context-path: /member
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
spring:
redis:
host: 127.0.0.1
password: 123456
port: 6379
pool:
max-idle: 100
min-idle: 1
max-active: 1000
max-wait: -1
application:
name: member
#資料庫連線資訊
datasource:
name: test
url: jdbc:mysql://127.0.0.1:3306/itmayiedu-member
username: root
password: 12345678
# 使用druid資料來源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
##activemq連線資訊
activemq:
broker-url: tcp://localhost:61616
in-memory: true
pool:
enabled: false
然後可以進行測試一下連線Redis,資料庫,mq什麼的,然後將工具類貼一下 注:這些類都寫在單獨的工程中(commom)
Redis:
package com.itmayiedu.base;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
@SuppressWarnings({ "rawtypes", "unchecked" })
@Component
public class BaseRedisService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
public void setString(String key, Object data, Long timeout) {
if (data instanceof String) {
String value = (String) data;
stringRedisTemplate.opsForValue().set(key, value);
}
if (timeout != null) {
stringRedisTemplate.expire(key, timeout, TimeUnit.SECONDS);
}
}
public Object getString(String key) {
return stringRedisTemplate.opsForValue().get(key);
}
public void delKey(String key) {
stringRedisTemplate.delete(key);
}
}
----------------------------用於返回資料的兩個工具類-------------
package com.itmayiedu.base;
public class ResponseBase {
// 響應code
private Integer code;
// 訊息內容
private String msg;
// 返回data
private Object data;
public ResponseBase() {
}
public ResponseBase(Integer code, String msg, Object data) {
super();
this.code = code;
this.msg = msg;
this.data = data;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
package com.itmayiedu.base;
public class BaseController {
// 返回成功 ,data值為null
public ResponseBase setResultSuccess() {
return setResult(Constants.HTTP_RES_CODE_200, Constants.HTTP_RES_CODE_200_VALUE, null);
}
// 返回成功 ,data可傳
public ResponseBase setResultSuccess(Object data) {
return setResult(Constants.HTTP_RES_CODE_200, Constants.HTTP_RES_CODE_200_VALUE, data);
}
// 返回失敗
public ResponseBase setResultError(String msg){
return setResult(Constants.HTTP_RES_CODE_500,msg, null);
}
// 自定義返回結果
public ResponseBase setResult(Integer code, String msg, Object data) {
ResponseBase responseBase = new ResponseBase();
responseBase.setCode(code);
responseBase.setMsg(msg);
if (data != null)
responseBase.setData(data);
return responseBase;
}
}
-----------------------mq的,這個是我自己封裝的,可能不太好,註釋寫的很詳細----這個可以暫時複製進去不用管,後面用到了再說
package com.itmayiedu.base;
import com.alibaba.fastjson.JSONObject;
/*
* 根據傳入引數不同,製作成不用的mq介面型別
*/
public class MqInterfaceType {
//mq中有特定的介面型別,sendUrl(比如發郵件,這個就傳郵箱號,簡訊就傳手機號,interfaceType,這個是自己定義的,舉例:郵箱就是email,反正自己能看懂是發什麼就行)
public static String mqInterfaceType(String sendUrl,String interfaceType) {
JSONObject root = new JSONObject();
JSONObject header = new JSONObject();
//型別,就是發郵件還是簡訊等等。。
header.put("interfaceType", interfaceType);
JSONObject content = new JSONObject();
//sendUrl發簡訊就是手機號,郵箱就是郵箱地址
content.put("sendUrl", sendUrl);
root.put("header", header);
root.put("content", content);
//最後封裝成一個json的字串
return root.toJSONString();
}
}