SpringCloud

  • 一.概念
            SpringCloud同SpringBoot配合實現集羣構架,SpringCloud實現架構思想,SoringBoot屬於模塊微服務實現,結合實現集羣微服務
  • 2、SpringCloud機制:
    • 1.Api網關
    • 2.http,rp模塊微服務之間的restful通訊
    • 3.註冊和發現
    • 4.熔斷機制
  • 3、
    • 實戰:
      • 建立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/
相關文章
相關標籤/搜索