您好,登錄后才能下訂單哦!
這篇文章給大家介紹spring cloud 中怎么搭建alibaba微服務,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
1.創建父工程cloud2020
2.引入相關jar包
<modelVersion>4.0.0</modelVersion><groupId>com.yxw.springcloud</groupId><artifactId>cloud2020</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><modules> <module>cloud-provider-payment8001</module></modules><name>springcloud</name><!-- FIXME change it to the project's website --><url>http://maven.apache.org/</url><inceptionYear>2001</inceptionYear><distributionManagement> <site> <id>website</id> <url>scp://webhost.company.com/www/website</url> </site></distributionManagement><!--統一管理jar包版本--><properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <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> <mysql.version>8.0.24</mysql.version> <druid.version>1.1.16</druid.version> <mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version></properties><!--子模塊繼承之后,提供作用:鎖定版本+子module不用groupId和version--><dependencyManagement> <dependencies> <!--spring boot 2.2.2--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.2.2.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!--spring cloud Hoxton.SR1--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR1</version> <type>pom</type> <scope>import</scope> </dependency> <!--spring cloud alibaba 2.1.0.RELEASE--> <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> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> <optional>true</optional> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <exclusions> <exclusion> <groupId>com.github.jsqlparser</groupId> <artifactId>jsqlparser</artifactId> </exclusion> </exclusions> </dependency> </dependencies></dependencyManagement><build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.1.6.RELEASE</version> <configuration> <fork>true</fork> <addResources>true</addResources> </configuration> </plugin> </plugins></build>
3.創建子工程
cloud-provider-payment8001
4.引入相關jar包
<parent> <artifactId>cloud2020</artifactId> <groupId>com.yxw.springcloud</groupId> <version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloud-provider-payment8001</artifactId><dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <!--mysql-connector-java--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- 熱部署 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.0</version> </dependency> <!-- 代碼生成器 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.4.0</version> </dependency> <!-- 模板引擎 --> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-site-plugin</artifactId> <version>3.3</version> </dependency></dependencies>
5.創建啟動類
@SpringBootApplication@MapperScan(basePackages = "com.yxw.**.mapper")public class PaymentApplication {public static void main(String[] args) { SpringApplication.run(PaymentApplication.class, args); } }
6.編寫yml文件
server: port: 8001spring: application:name: cloud-provider-service datasource:type: com.alibaba.druid.pool.DruidDataSource #當前數據源操作類型 driver-class-name: com.mysql.cj.jdbc.Driver #mysql驅動包 url: jdbc:mysql://localhost:3306/cloud2020?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=trueusername: rootpassword: 123456mybatis-plus: #實體掃描,多個package用逗號或者分號分隔 typeAliasesPackage: com.yxw.**.model mapper-locations: classpath:mapper/*.xml
7.編寫業務代碼測試我們之前搭建的環境是否可用,代碼這里就不貼出來了,獲取完整代碼:https://gitee.com/xiaowei_328/cloud2020.git
8.在創建個子工程:cloud-consumer-order80
注:相關配置和代碼這里可以參考我上傳的代碼這里不做過多解釋
9.用RestTemplate調用我們8001端口服務
10.controller代碼如下:
配置好以后分別啟動80和8001端口的服務
瀏覽器訪問如下:http://localhost/consumer/payment/get/1388397492484583426
簡單說明下因為我訪問的時候沒有加80端口,因為瀏覽器默認就是80端口所以這里可以省略。
工程重構
1.上面我們創建的80和8001兩個服務都有共同的實體類,我們要給抽出來一個公共的模塊
(1)接著在父工程下創建子工程:cloud-api-commons
(2)修改pom文件
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.1.0</version> </dependency></dependencies>
(3)將我們之前創建的實體類復制到commons工程下
2.修改cloud-consumer-order80工程pom文件,引入cloud-api-commons公共服務jar包
<!--引入自己定義的api通用包,可以使用Payment支付Entity--><dependency> <groupId>com.yxw.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version></dependency>
3.修改cloud-provider-payment8001工程pom文件,引入cloud-api-commons公共服務jar包
<!--引入自己定義的api通用包,可以使用Payment支付Entity--><dependency> <groupId>com.yxw.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version></dependency>
關于spring cloud 中怎么搭建alibaba微服務就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。