微服务Springcloud开弓之框架搭建《一》


微服务体系

  • 服务注册与发现
  • 配置中心管理
  • 服务调用
  • 服务网关
  • 服务熔断
  • 服务监控
  • 负载均衡
  • 全链路跟踪
  • 服务降级
  • 自动化构建部署
  • 服务消息队列
  • 服务定时任务调度操作

对微服务的实现

  • Eureka : 服务注册与发现
  • Netelix Ribbon: 服务负载与调用
  • Netelix Feign: 服务负载与调用
  • Hystrix : 服务熔断与降级
  • Netelix Zuul : 服务网关
  • Spring Cloud config :服务分布式配置
  • SpringBoot : 服务开发

版本依赖关系

Cload Release TrainBoot Version
Hoxton2.2.x
Greenwich2.1.x
Finchley2.0.x
Edgware1.5.x
Dalston1.5.xz

技术选型

技术版本
cloudHoxton.SR1
boot2.2.2.RELEASE
cloud alibaba2.1.0.RELEASE
java1.8
Maven3.5.4
Mysql5.7及以上

微服务技术现在状况

在这里插入图片描述

父工程的搭建

1、New Project 勾选site简单

2、聚合总父工程名字

3、Maven选版本

4、工程名字

5、字符编码

6、 注解生效激活 File | Settings | Build, Execution, Deployment | Compiler | Annotation Processors

7、. java编译版本选8

父工程依赖

 <groupId>com.atguigu.springcloud</groupId>
  <artifactId>cloud2020</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

<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.17</mysql.version>
    <druid.version>1.1.16</druid.version>
    <spring.boot.version>2.2.2.RELEASE</spring.boot.version>
    <spring.cloud.version>Hoxton.SR1</spring.cloud.version>
    <spring.cloud.alibaba.version>2.1.0.RELEASE</spring.cloud.alibaba.version>
    <mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
  </properties>

  <!--子模块继承后,提供作用:锁定版本+子module不用groupId和version-->
  <dependencyManagement>
    <dependencies>
      <!--springboot 2.2.2-->
      <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>${mybatis.spring.boot.version}</version>
      </dependency>
      <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>${druid.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>${spring.boot.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!--Spring cloud Hoxton.SR1-->
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>${spring.cloud.version}</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>${spring.cloud.alibaba.version}</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>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>${lombok.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <fork>true</fork>
          <addResources>true</addResources>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <!--第三方maven私服-->
  <repositories>
    <repository>
      <id>nexus-aliyun</id>
      <name>Nexus aliyun</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>

注意

父工程创建完成执行mvn:insall将父工程发布到仓库方便子工程继承

Rest微服务工程搭建

构建步骤

1. Cloud-provider-payment8001 微服务提供者Module模块
  • 建module

  • 改POM

      <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>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
            </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>
    
  • 写YML

    server:
      port: 8001
    
    spring:
      application:
        name: cloud-provider-service
      datasource:
        username: root
        password: 123
        url: jdbc:mysql://localhost:3306/db2019?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
        driver-class-name: com.mysql.cj.jdbc.Driver
        type: com.alibaba.druid.pool.DruidDataSource
    
    mybatis:
      mapper-locations: classpath:mapper/*.xml
      type-aliases-package: com.atguigu.springcloud.entities
    
  • 主启动

    @SpringBootApplication
    public class PaymentMain8001 {
        public static void main(String[] args) {
            SpringApplication.run(PaymentMain8001.class);
        }
    }
    
  • 业务类

    1.建库

    CREATE TABLE `payment`(
    	`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
    	`serial` varchar(200) DEFAULT '',
    	PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
    

    2.emtities

    • 主实体Payment

      @Data
      @AllArgsConstructor
      @NoArgsConstructor
      public class Payment  implements Serializable {
      
          private  Long id;
          private String serial;
      }
      
    • Json封装体CommonResult (用于给前端传送json数据)

      @Data
      @AllArgsConstructor
      @NoArgsConstructor
      public class CommonResult<T> {
          private Integer code;
          private String message;
          private  T      data;
      
          public CommonResult(Integer code,String message){
              this(code,message,null);
          }
      
      }
      

    3.dao

    • 接口PaymentDao

      @Mapper
      public interface PaymentDao {
      
          public  int  create(Payment payment);
      
          public  Payment  getPaymentById(@PathParam("id") Long id);
      }
      
    • mybatis的映射文件PaymentMapper.xml 对应放到resource下的mapper,与接口名对应

      • src\main\resources\mapper\PaymentMapper.xml
      <?xml version="1.0" encoding="UTF-8" ?>
      <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
              "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
      <mapper namespace="com.atguigu.springcloud.dao.PaymentDao">
          <insert id="create" parameterType="payment" useGeneratedKeys="true" keyProperty="id">
              insert  into payment(serial) values (#{serial});
          </insert>
      
          <resultMap id="BaseResultMap" type="com.atguigu.springcloud.entities.Payment">
              <id column="id" property="id" jdbcType="BIGINT"/>
              <id column="serial" property="serial" jdbcType="VARCHAR"/>
          </resultMap>
          <select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap">
              select  * from payment where  id=#{id};
          </select>
      </mapper>
      

      mapper.xml的头部结构

      <?xml version="1.0" encoding="UTF-8" ?>
      <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
              "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
      <mapper namespace="com.atguigu.springcloud.dao.PaymentDao">
      </mapper>
      

    4.service

    • 接口PaymentService

      public interface PaymentService {
          public  int  create(Payment payment);
      
          public  Payment  getPaymentById(@PathParam("id") Long id);
      }
      
    • 实现类

      @Service
      public class PaymentServiceImpl implements  PaymentService {
      
          @Resource
          private PaymentDao paymentDao;
          @Override
          public int create(Payment payment) {
              return  paymentDao.create(payment);
      
          }
      
          @Override
          public Payment getPaymentById(Long id) {
              return paymentDao.getPaymentById(id);
          }
      }
      

5.controller

@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @PostMapping(value = "/payment/create")
    public  CommonResult create(Payment payment){
        int result = paymentService.create(payment);
        log.info("###########插入结果"+result);
        if(result>0){
            return new CommonResult(200,"插入数据库成功",result);
        }else{
            return new CommonResult(444,"插入数据库失败",null);
        }
    }

    @GetMapping(value = "/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id) {
        Payment paymentById = paymentService.getPaymentById(id);
        log.info("*********查找结果为: " + paymentById);
        if (paymentById != null) {
            return new CommonResult(200, "查找成功", paymentById);
        } else {
            return new CommonResult(444, "查找为空,查找ID为: " + id, null);
        }
    }
}
  • 测试

访问地址: http://localhost:8001/payment/get/31

结果

{"code":200,"message":"查找成功","data":{"id":31,"serial":"尚硅谷01"}}

postman模拟: http://localhost:8001/payment/create?serial=atguigu002

结果

{
    "code": 200,
    "message": "插入数据库成功",
    "data": 1
}

Dashboard窗口

在这里插入图片描述

<component name="RunDashboard">
    <option name="configurationTypes">
      <set>
        <option value="SpringBootApplicationConfigurationType" />
      </set>
    </option>
  </component> 

在这里插入图片描述
在这里插入图片描述
Dashboard窗口方法二
在这里插入图片描述

  • 小总结
  1. 建module
  2. 改POM
  3. 写YML
  4. 主启动
  5. 业务类
2.热部署
  • Adding devtools to your project

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    
  • Adding plugin to your pom.xml

    下面配置我们粘贴进聚合父类总工程的pom.xml里
    <build>
        <fileName>你自己的工程名字<fileName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <addResources>true</addResources>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
  • Enabling automatic build

在这里插入图片描述

  • Update the value of
    在这里插入图片描述
  • 重启idea
3.cloud-consumer-order80 微服务消费者订单Module模块
  • 建cloud-consumer-order80

  • 改POM

        <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>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>
        </dependencies>
    
  • 写YML

    server:
      port: 80
    
  • 主启动

    @SpringBootApplication
    public class OrderMain80 {
        public static void main(String[] args) {
            SpringApplication.run(OrderMain80.class,args);
        }
    }
    
  • 业务类

    • entites 和 Cloud-provider-payment8001 一样

    • 首说Resttemplate

在这里插入图片描述

  • config对其配置
```xml
@Configuration
public class ApplicationContextConfig {

    @Bean
    public RestTemplate getRestTemplate(){
        return  new RestTemplate();
    }
}
```
  • controller

    @RestController
    @Slf4j
    public class OrderController {
    
        public static  final  String  PAYMENT_URL="http://localhost:8001";
    
        @Resource
        private RestTemplate restTemplate;
    
        @GetMapping("/consumer/payment/create")
        public CommonResult<Payment> create(Payment payment){
            return  restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class);
        }
    
    
        @GetMapping("/consumer/payment/{id}")
        public  CommonResult<Payment> getPayment(@PathVariable("id") Long id){
            return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
        }
    }
    
  • 测试

    http://localhost/consumer/payment/32

    http://localhost/consumer/payment/create?serial=222

注意

测试一下插入发现成功了,但是数据库没值,只有自增主键,这里插入没成功的原因是,8001的创建参数没加@RequestBody,

4. 工程重构
  • 系统中有重复部分,重构

在这里插入图片描述

  • 新建

    cloud-api-common

  • 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>
    
  • entties

    Payment实体、CommonResult通用封装类 进行打包

  • maven命令clean install

  • 订单80和支付8001分别改造

    1、删除各自的原先的entities文件夹

    2、分别在pom文件中进行配置

    <dependency>
        <groupId>com.atguigu.springcloud</groupId>
        <artifactId>cloud-api-common</artifactId>
        <version>${project.version}</version>
    </dependency>
    
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值