01-创建工程

创建工程

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>top.bigking</groupId>
    <artifactId>aispringclouddemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.7.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- JDK 9 缺失 jar -->
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

注册中心

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        <version>2.0.2.RELEASE</version>
    </dependency>
</dependencies>

application.yml:

server:
  port: 8761
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
      register-with-eureka: false
      fetch-registry: false

启动类

package top.bigking;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * @Author ABKing
 * @since 2020/3/25 下午9:51
 **/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

配置中心

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
        <version>2.0.2.RELEASE</version>
    </dependency>
</dependencies>

application.yml

server:
  port: 8762
spring:
  application:
    name: configserver
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/shared
  • 在shared目录下创建各个微服务对应的配置文件
  • 启动类
package top.bigking;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

/**
 * @Author ABKing
 * @since 2020/3/25 下午10:10
 **/
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

服务提供者

pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        <version>2.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
        <version>2.0.2.RELEASE</version>
    </dependency>
</dependencies>

bootstrap.yml

作用是指定要读取的配置中心的配置文件

spring:
  application:
    name: order
  profiles:
    active: dev
  cloud:
    config:
      uri: http://localhost:8762
      fail-fast: true

Handler

package top.bigking.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @Author ABKing
 * @since 2020/3/25 下午10:50
 **/
@RestController
@RequestMapping("order")
public class OrderHandler {
    @Value("${server.port}")
    private String port;
    @GetMapping("index")
    public String index(){
        return "order的端口:" + this.port;
    }
}

启动类

package top.bigking;

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

/**
 * @Author ABKing
 * @since 2020/3/25 下午10:56
 **/
@SpringBootApplication
public class OrderApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }
}

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>aispringclouddemo</artifactId>
        <groupId>top.bigking</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>menu</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>
    </dependencies>

</project>

bootstrap.yml

spring:
  application:
    name: menu
  profiles:
    active: dev
  cloud:
    config:
      uri: http://localhost:8762
      fail-fast: true

启动类:

package top.bigking;

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

/**
 * @Author ABKing
 * @since 2020/3/26 下午10:31
 **/
@SpringBootApplication
public class MenuApplication {
    public static void main(String[] args) {
        SpringApplication.run(MenuApplication.class, args);
    }
}

在配置中心添加数据库连接信息

server:
  port: 8020
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/spring_dem01
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver

Handler

package top.bigking.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author ABKing
 * @since 2020/3/26 下午10:33
 **/
@RestController
@RequestMapping("menu")
public class MenuHandler {
    @Value("${server.port}")
    private String port;
    @GetMapping("index")
    public String index(){
        return this.port;
    }
}

Entity

package top.bigking.entity;

import lombok.Data;

/**
 * @Author ABKing
 * @since 2020/3/26 下午10:47
 **/
@Data
public class Menu {
    private Integer id;
    private String name;
    private Double price;
    private String flavor;
}

Repository

package top.bigking.repository;

import top.bigking.entity.Menu;

import java.util.List;

/**
 * @Author ABKing
 * @since 2020/3/26 下午10:51
 **/
public interface MenuRepository {
    List<Menu> findAll(Integer index, Integer limit);
    Integer count();
    Menu findById(Integer id);
    Integer save(Menu menu);
    Integer update(Menu menu);
    Integer deleteById(Integer id);
}
<?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="top.bigking.repository.MenuRepository">
    <select id="findAll" resultType="top.bigking.entity.Menu">
        select id, name, price, flavor from t_menu limit #{param1}, #{param2}
    </select>

    <select id="count" resultType="int">
        select count(id) from t_menu
    </select>

    <select id="findById" resultType="top.bigking.entity.Menu">
        select id, name, price, flavor from t_menu where id = #{id}
    </select>

    <insert id="save" useGeneratedKeys="true" keyProperty="id">
        insert into t_menu(name, price, flavor) values (#{name}, #{price}, #{flavor})
    </insert>

    <update id="update" parameterType="top.bigking.entity.Menu">
        update t_menu set name = #{name}, price = #{price}, flavor = #{flavor} where id = #{id}
    </update>

    <delete id="deleteById" parameterType="int">
        delete from t_menu where id = #{id}
    </delete>
</mapper>

启动类

package top.bigking;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @Author ABKing
 * @since 2020/3/26 下午10:31
 **/
@SpringBootApplication
@MapperScan("top.bigking.repository")
public class MenuApplication {
    public static void main(String[] args) {
        SpringApplication.run(MenuApplication.class, args);
    }
}
posted @ 2020-03-27 17:27  ABKing  阅读(240)  评论(0编辑  收藏  举报