springcloud Micro Services demo

springcloud relying on springBoot, springBoot can leave springcloud,

But springcloud not leave springBoot

 

  Spring Boot is Spring of a fast configuration Scaffolding, based on Spring Boot rapid development of single

 

  Micro-service, the Spring Cloud is based on Spring Boot cloud application development tools to achieve; Spring Boot professionals

 

  Note to quickly and easily integrated single individual micro-services, the Spring Cloud attention of global service governance framework;

 

 

The following figure such as the establishment of the project

01 in pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.offcn</groupId>
    <artifactId>microservice_cloud_01</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>microservice_cloud_02_api</module>
        <module>microservice_cloud_03_provider_product_8001</ Module> 
    <parent>
    <-! Manual developed POM ->
    </ modules>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.7.RELEASE</version>
    </parent>
    <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>
        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>
    <!--依赖声明-->
    <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>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.12</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.13</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
View Code

 Create an entity class in the microservice_cloud_02_api

 

package com.yuan.springcloud.entities;

public class Product { //跨服调用必须实现序列化
    private long id;
    private String productName;
    private String dbSource;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getDbSource() {
        return dbSource;
    }

    public void setDbSource(String dbSource) {
        this.dbSource = dbSource;
    }

    @Override
    public String toString() {
        return "Product{" +
                "id=" + id +
                ", productName='" + productName + '\'' +
                ", dbSource='" + dbSource + '\'' +
                '}';
    }
}
View Code

 

microservice_cloud_02_api中的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>microservice_cloud_01</artifactId>
        <groupId>com.offcn</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>microservice_cloud_02_api</artifactId>
</project>
View Code

 

03中的建立controller、service、mapper

 

productmapper应放入resources-》mapper中否则会报错

 

application.yml配置文件

 

server:
  port: 8001
mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml
  type-aliases-package: com.yuan.springcloud.entities
  mapper-locations: classpath:mybatis/mapper/**/*.xml
spring:
  application:
  name: microservice-product
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/springcloud_db01?serverTimezone=GMT%2B8
    username: root
    password: root
    dbcp2:
      min-idle: 5
      initial-size: 5
      max-total: 5
      max-wait-millis: 150

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/proyuan/p/11813180.html