SpringCloud

  • 一.概念
            SpringCloud同SpringBoot配合实现集群构架,SpringCloud实现架构思想,SoringBoot属于模块微服务实现,结合实现集群微服务
  • 二、SpringCloud机制:
    • 1.Api网关
    • 2.http,rp模块微服务之间的restful通信
    • 3.注册和发现
    • 4.熔断机制
  • 三、
    • 实战:
      • 创建mavn项目
      • 此处创建cloud maven项目
      • 配置pom.xml文件,内容中配置依赖管理器dependencyManagement,配置依赖包
<packaging>pom</packaging>
<properties>
    <junit.version>4.13.2</junit.version>
    <lombok.version>1.18.18</lombok.version>
</properties>
<dependencyManagement>
        <dependencies>
<!--            springCloud依赖-->
            <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
<!--            springboot-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
                <version>2.1.4.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.4.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
<!--            数据库-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.46
                </version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.10</version>
            </dependency>
<!--            启动器-->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.1.4</version>
            </dependency>
<!--            junit-->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
            </dependency>
<!--            lombok-->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </dependency>
<!--            logj4-->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-core</artifactId>
                <version>1.2.3</version>
            </dependency>
<!--                eureka-->
        </dependencies>
    </dependencyManagement>

 

      • 注意这里的SpringCloud和SpringBoot拥有版本兼容性
      • 创建springcloud-apiModule,用来管理entity实体
@Data
@NoArgsConstructor
@Accessors(chain = true)    //链式编程开启
public class Dept implements Serializable {
    private int dep;
    private String dnname;
    private String db_source;
}

 

      • 创建SpringCloud-provider-8001  Module,用来实现生产层,既dao层、Service层、和RESTful风格的Controller层。
pom.xml文件
<dependencies>
    <dependency>
        <groupId>springcloud</groupId>
        <artifactId>springcloud-api</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>springcloud</groupId>
        <artifactId>springcloud-api</artifactId>
        <version>1.0-SNAPSHOT</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
        <version>1.4.6.RELEASE</version>
    </dependency>
//info依赖
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

 

      • 创建编辑SpringCloud-provider-8001=>application.yml
server:
  port: 8001
mybatis:
  mapper-locations: classpath:mybatis/mapper/*.xml
  type-aliases-package: com.wjsm.springcloud.entity
spring:
  application:
    name: springcloud-provider
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEnconding=utf-8
    username: root
    password: 123
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7002/eureka/,http://localhost:7002/eureka/,http://localhost:7003/eureka
  instance:
    instance-id: springcloud-provider-8001
info:
  app.name: wjsmc
  company.name: wwwww

 

      • Create mapper层接口类,创建SQL的query接口,Create mapper的静态.xml文件,编写Sql操作语句
###    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.wjsm.springcloud.dao.deptdao">
    <insert id="addDept" parameterType="Dept">
        insert into db01.dept (dnname, db_source)
        values (#{daname},#{db_source});
    </insert>
    <select id="queryById" resultType="Dept" parameterType="Long">
        select * from db01.dept where dep = #{dep}
    </select>
    <select id="queryAll" resultType="Dept">
        select * from db01.dept;
    </select>
</mapper>
      • Create service接口类,定义dao层里接口函数,Create serviceImpl类,实现service接口函数。
      • Create controllet类,编写RESTful风格控制层代码,实现数据库数据的JSON输出
      • 创建SpringCloud-consumer-8080  Module,用来实现消费层
      • 消费模块需要执行请求操作,既Get,Post,Insert等操作,需配置RestTemplate bean,Create config类,声明RestTemplate bean
@Configuration
public class ConfigBean {
    @Bean
    public RestTemplate getRestTemplate(){
        return  new RestTemplate();
    }
}

 

      • 在控制层实现controller的数据消费
@RestController
public class ConsumerController {
    @Autowired
    private  RestTemplate restTemplate;
    private static final String REST_URL_PREFIX = "http://localhost:8001";
    @RequestMapping("/consumer/getAll")
    public List<Dept> getALl(){
        System.out.println(REST_URL_PREFIX+"/dept/getAll");
        System.out.println();
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/getAll",List.class);
    }
}

 

      • 开始实现SpringCloud服务注册========================================
      • 创建springcloud-eureka-7001 Module,实现服务注册功能
      • 编写springcloud-eureka-7001  pom.xml文件,需注意,spring-cloud-starter-eureka-server依赖需要和springboot版本兼容,上文有写
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
        <version>1.4.6.RELEASE</version>
    </dependency>
</dependencies>

 

      • 注意,eureka Module的配置文件中需要注意!!!!!
        需配置eureka的client注册功能来区分自己是注册服务。如下
server:
  port: 7001
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:7002/eureka/,http://localhost:7003/eureka/
posted @ 2021-03-14 23:04  王居三木  阅读(84)  评论(0编辑  收藏  举报