springcloud使用微服务的搭建

微服务的搭建

1.配置对应信息

  • Springboot 、springcloud、springcloud alibaba对应关系

https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E

在这里插入图片描述

在这里插入图片描述

2.pom.xml的配置

2.1 总项目pom.xml引入依赖

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
        <relativePath/> 
        <!-- lookup parent from repository -->
    </parent>

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Hoxton.SR9</spring-cloud.version>
    <spring-cloud-alibaba.version>2.2.6.RELEASE</spring-cloud-alibaba.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>${spring-cloud-alibaba.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

2.2工具类模块引入(非必需)

  <dependencies>
      <!-- lombok类 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!-- 阿里JSON解析器 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.79</version>
        </dependency>
    </dependencies>

2.3创建微服务

步骤:

  • 创建maven模块并导入依赖(ali-user)

  • 添加springboot的启动类

  • 入配置文件application.yml

  • 创建必要的接口和实现类(controller service dao)

1 .创建(ali-user)maven模块或springboot模块并导入依赖

<!--工具类引入-->
<dependency>
    <groupId>xxxx</groupId>
    <artifactId>ali-common</artifactId>
    <version>1.0.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2 添加springboot的启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class UserApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class);
    }
}     

3 .加入配置文件application.yml

server:
  port: 10022

spring:
  application:
    name: ali-user

4 .创建必要的接口和实现类(controller service dao)

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
搭建微服务项目一般需要以下步骤: 1. 创建父项目 在项目根目录下创建一个父项目,作为所有微服务子项目的父项目。在父项目的pom.xml文件中,配置Spring Cloud和其他依赖的版本号,如下所示: ``` <dependencyManagement> <dependencies> <!-- Spring Cloud dependencies --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>2020.0.2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> ``` 2. 创建微服务子项目 在父项目下创建多个微服务子项目,每个子项目都是一个独立的服务模块,可以有自己的数据模型、业务逻辑、数据库等。每个子项目都需要配置Spring BootSpring Cloud相关依赖,如下所示: ``` <dependencies> <!-- Spring Boot dependencies --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Cloud dependencies --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> </dependencies> ``` 3. 配置微服务 每个微服务都需要有自己的配置文件,例如application.yml或application.properties。在配置文件中,需要指定微服务的端口号、注册中心地址、数据源等信息。 ``` server: port: 8080 spring: application: name: user-service datasource: url: jdbc:mysql://localhost:3306/user_db username: root password: root eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ ``` 4. 注册微服务 微服务需要注册到注册中心,以便其他微服务可以发现它并调用它的接口。可以使用Eureka或Consul等开源组件作为注册中心。在微服务的配置文件中,需要指定注册中心的地址和端口号。 ``` eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ ``` 5. 调用微服务 微服务之间可以通过HTTP或RPC调用接口进行通信。可以使用Feign或Ribbon等Spring Cloud组件来实现微服务之间的调用。 例如,在一个微服务中调用另一个微服务的接口,可以使用以下代码: ``` @FeignClient(name = "user-service") public interface UserServiceClient { @GetMapping("/users/{id}") User getUserById(@PathVariable("id") Long id); } ``` 以上就是使用Spring Cloud搭建微服务项目的基本步骤。当然,在实际项目中还需要考虑一些其他因素,例如安全性、性能、可伸缩性等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宣布无人罪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值