? ? ? ? 1.創(chuàng)建新的項目
? ? ? ? ?2.創(chuàng)建的項目類型為SpringBoot
?
?
?????????選擇創(chuàng)建后再選擇新建項目
?
? ? ? ?
????????3. 將新建的項目中不需要的東西都給刪了,只留下pom文件即可
? ? ? ? ?修改pom文件的版本
<version>2.1.6.RELEASE</version>
? ? ? ? ?導(dǎo)入所需要的jar包,將原來<dependencies>中的依賴覆蓋掉
<!-- SpringBoot啟動器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- SpringBoot測試啟動器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Web依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 日志依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency> <!-- Lombok工具 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.4</version> <scope>provided</scope> </dependency> <!-- Actuator可以幫助你監(jiān)控和管理Spring Boot應(yīng)用 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- 熱部署 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency>
? ? ? ? 如果你的java版本比8高,還需要導(dǎo)入以下依賴
<!-- 引入Jaxb開始 --> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version>2.2.11</version> </dependency> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.2.11</version> </dependency> <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> <version>2.2.10-b140310.1920</version> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1.1</version> </dependency> <!-- 引入Jaxb結(jié)束 -->
? ? ? ? 導(dǎo)入微服務(wù)依賴的組件依賴
<dependencyManagement> <dependencies> <!-- Spring Cloud Neflix 公司出品的微服務(wù)組件的依賴--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!--Spring Cloud Alibaba微服務(wù)組件的依賴--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.1.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
? ? ? ? 4.創(chuàng)建存在實(shí)體類的微服務(wù)
? ? ? ? 在這個微服務(wù)下的pom文件中導(dǎo)入所需要的依賴
<dependencies> <!-- mybatis--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.2</version> </dependency> <!-- pojo持久化使用 --> <dependency> <groupId>javax.persistence</groupId> <artifactId>javax.persistence-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> </dependencies>
? ? ? ? 創(chuàng)建實(shí)體類
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; @Data @NoArgsConstructor @AllArgsConstructor public class User implements Serializable { private Integer id; private String username; private String password; }
? ? ? ? ?5.創(chuàng)建用于編寫邏輯的微服務(wù),步驟和上面的一樣,將名字編寫為:?leq-server-product
? ? ? ?????????引入實(shí)體類微服務(wù)的地址
<dependencies> <dependency> <groupId>com.example</groupId> <artifactId>leq-server-common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies>
? ? ? ? 導(dǎo)入第二代微服務(wù)組件nacos所需要的依賴
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
? ? ? ? 創(chuàng)建啟動類
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class ProductApplication { public static void main(String[] args) { SpringApplication.run(ProductApplication.class,args); } }
? ? ? ? 編寫配置文件,在resources創(chuàng)建application.properties文件
server.port=3000 spring.application.name=leq-server-product spring.datasource.url=jdbc:mysql://localhost:3306/leq_pro?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=0216 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
? ? ? ? 6.創(chuàng)建業(yè)務(wù)訪問的微服務(wù),名字為:?leq-server-page
????????????????也是先導(dǎo)入實(shí)體類的項目的地址
<dependencies> <dependency> <groupId>com.example</groupId> <artifactId>leq-server-common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies>
? ? ? ? ? ? ?再導(dǎo)入以下依賴
<!--nacos的客戶端依賴--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- 引入alibaba-nacos-config依賴,可以從Nacos配置中心獲得配置信息 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!-- Sentinel核心環(huán)境依賴 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency>
? ? ? ? 創(chuàng)建page的啟動類
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class PageApplication { public static void main(String[] args) { SpringApplication.run(PageApplication.class,args); } }
? ? ? ? 編寫配置文件,
????????在resources創(chuàng)建application.properties文件
server.port=3100 spring.application.name=leq-server-page spring.datasource.url=jdbc:mysql://localhost:3306/leq_pro?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=0216 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
? ? ? ? 到這里我們的微服務(wù)項目就算是搭建完成了,接下來進(jìn)行測試
? ? ? ?沒有安裝nacos的小伙伴可以看這個鏈接安裝nacos : 快速安裝nacos
???????? 1.先啟動naco服務(wù)注冊中心
http://10.48.185.7:8848/nacos/index.html
? ? ? ? ?訪問該地址在瀏覽器可以看到該頁面就證明服務(wù)注冊中心啟動了
? ? ? ? 啟動page和product兩個微服務(wù)項目,然后刷新該地址,就可以看到我們的項目注冊到服務(wù)中心了文章來源:http://www.zghlxwxcb.cn/news/detail-708359.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-708359.html
到了這里,關(guān)于在idea中搭建微服務(wù)項目(22版),詳細(xì)教程的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!