温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

spring cloud 中怎么搭建alibaba微服务

发布时间:2021-08-05 16:33:10 来源:亿速云 阅读:218 作者:Leah 栏目:编程语言

这篇文章给大家介绍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端口服务

spring cloud 中怎么搭建alibaba微服务

10.controller代码如下:

spring cloud 中怎么搭建alibaba微服务

配置好以后分别启动80和8001端口的服务

浏览器访问如下:http://localhost/consumer/payment/get/1388397492484583426

spring cloud 中怎么搭建alibaba微服务

简单说明下因为我访问的时候没有加80端口,因为浏览器默认就是80端口所以这里可以省略。

                                                    工程重构

1.上面我们创建的80和8001两个服务都有共同的实体类,我们要给抽出来一个公共的模块

    (1)接着在父工程下创建子工程:cloud-api-commons

spring cloud 中怎么搭建alibaba微服务

    (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工程下

spring cloud 中怎么搭建alibaba微服务

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微服务就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI