新建Project
這裏使用的開發工具是IDEA,JDK版本1.8。
打開IDEA開發工具,File -> New -> Project
然後一步步往下設置,然後到這一步,選擇Spring Cloud
OK,繼續一路往下,點finish,Spring Cloud項目創建完成。
設置編譯器版本,選擇jdk版本等
這裏我使用的是JDK 1.8,當前主流的版本。具體操作不再贅述,網上一搜一大把,照着做就行了。
新建微服務啓動類Application.class
注意這2個註解,一定要加上:@EnableDiscoveryClient和@SpringBootApplication
package com.yangcq.learning.hantang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class HantangApplication {
public static void main(String[] args) {
SpringApplication.run(HantangApplication.class, args);
}
}
配置文件Application.properties
#服務名稱
spring.application.name=hantang
#設置日誌級別
logging.level.root=info
#對外提供服務端口號
server.port=8088
#eureka-client註冊地址
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
#是否向eureka-server註冊
eureka.client.registerWithEureka=false
新建Controller
這裏的註解和入參的定義很關鍵,請看仔細了,直接上代碼。
爲了方便跟蹤,打印一行日誌。
package com.yangcq.learning.hantang.controller;
import com.yangcq.learning.hantang.HantangApplication;
import com.yangcq.learning.hantang.service.CommonService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping(value = "/interface/hantang/common")
public class CommonController {
private static final Log log = LogFactory.getLog(HantangApplication.class);
@Autowired
private CommonService commonService;
@RequestMapping(value = "/list/{var}", method = RequestMethod.GET)
public String getInventoryList(@PathVariable String var) {
log.info("請求入參 var:" + var);
commonService.commonTest();
return "漢唐氣象";
}
}
新建service
CommonService接口
package com.yangcq.learning.hantang.service;
public interface CommonService {
/**
* service方法
*/
void commonTest();
}
CommonServiceImpl實現類,注意,一定要加上@Service註解,注入Spring IOC容器
package com.yangcq.learning.hantang.service.impl;
import com.yangcq.learning.hantang.service.CommonService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
@Service
public class CommonServiceImpl implements CommonService {
private static final Log log = LogFactory.getLog(CommonServiceImpl.class);
@Override
public void commonTest() {
// 業務邏輯一般放在service中
log.info("進入service commonTest");
}
}
項目整體文件結構
OK,看一下項目整體結構
至此,一個Spring Cloud項目就算搭建完成了。接下來我們啓動項目,訪問看是否正常。
注意,這裏我對banner進行了個性化定製,使用漢字“中”取代了“SPRING”。
這個挺簡單,大家可以一試,只需要在resource下,新建一個banner.txt文件,然後把你自定義的banner設計,複製進去即可。
項目啓動階段,會去讀取這個banner.txt文件,這個名字是固定的,不能更改,詳情可以參考SpringApplication.class源碼。
控制檯完整啓動日誌如下:
D:\JDK64\jdk1.8\bin\java.exe -XX:TieredStopAtLevel=1
___
|||
|||
|||
|||
+++++++|||+++++++
+ ||| +
+ ||| +
+++++++|||+++++++
|||
|||
|||
|||
vvv
2020-04-02 16:23:58.530 INFO 68876 --- [ main] c.y.learning.hantang.HantangApplication : No active profile set, falling back to default profiles: default
2020-04-02 16:23:58.962 INFO 68876 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=fe5609f5-cb29-3873-8849-003765dbeeef
2020-04-02 16:23:59.226 INFO 68876 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8088 (http)
2020-04-02 16:23:59.236 INFO 68876 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-04-02 16:23:59.236 INFO 68876 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.33]
2020-04-02 16:23:59.538 INFO 68876 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-04-02 16:23:59.538 INFO 68876 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 992 ms
2020-04-02 16:23:59.716 INFO 68876 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-04-02 16:24:00.298 INFO 68876 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8088 (http) with context path ''
2020-04-02 16:24:00.702 INFO 68876 --- [ main] c.y.learning.hantang.HantangApplication : Started HantangApplication in 3.422 seconds (JVM running for 4.388)
項目啓動完成後,訪問:http://localhost:8088/interface/hantang/common/list/%E6%B1%89
注意,我這裏入參是中文:漢,所以顯示的是對應的unicode編碼
來看最終的效果:
控制檯日誌如下:
2020-04-02 17:01:18.938 INFO 68876 --- [nio-8088-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-04-02 17:01:18.938 INFO 68876 --- [nio-8088-exec-2] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-04-02 17:01:18.945 INFO 68876 --- [nio-8088-exec-2] o.s.web.servlet.DispatcherServlet : Completed initialization in 7 ms
2020-04-02 17:01:18.973 INFO 68876 --- [nio-8088-exec-2] c.y.learning.hantang.HantangApplication : 請求入參 var:漢
2020-04-02 17:01:18.973 INFO 68876 --- [nio-8088-exec-2] c.y.l.h.service.impl.CommonServiceImpl : 進入service commonTest
使用Spring Initializr從零開始搭建Spring Cloud項目,就這麼愉快的結束了。後續,我會基於這個項目,進行Spring Cloud項目的
擴展,逐漸豐富這個項目,敬請期待。