SpringClound 微服务环境搭建

1.创建父工程
创建一个maven工程,然后在pom.xml文件中添加下面内容



    org.springframework.boot
    spring-boot-starter-parent
    2.1.4.RELEASE





    1.8
    UTF-8
    UTF- 8
    Greenwich.RELEASE
    2.1.0.RELEASE



    
        
            org.springframework.cloud
            spring-cloud-dependencies
            ${spring-cloud.version}
            pom
            import
        
        
            com.alibaba.cloud
            spring-cloud-alibaba-dependencies
            ${spring-cloud-alibaba.version}
            pom
            import
        
    


版本对应:
SpringClound 微服务环境搭建_第1张图片
2.搭建公共工程shop-common
2.1.引入依赖



    
    
        org.springframework.boot
        spring-boot-starter-data-jpa
    
    
        org.projectlombok
        lombok
    
    
        com.alibaba
        fastjson
        1.2.56
    
    
        mysql
        mysql-connector-java
    


2.2.写实体类
order:

package com.myl.entity;

import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity(name = "shop_order")
@Data
public class Order {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)//主键递增方式
    private Long oid;
    private Integer number;
    private Long pid;
    private Double pprice;
    private String pname;
    private Long uid;
    private String username;

}

product:

package com.myl.entity;

import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Data
@Entity(name = "shop_product")
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long pid;
    private String pname;
    private Double pprice;
    private Integer stock;
}

3.创建商品微服务
3.1创建一个名为 shop-product 的模块,并添加springboot依赖


        
            com.ykq
            shop-common
            1.0-SNAPSHOT
        
        
            org.springframework.boot
            spring-boot-starter-web
        
    

3.2.写主类

package com.myl;

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

@SpringBootApplication
public class Product {
    public static void main(String[] args) {
        SpringApplication.run(Product.class,args);
    }
}

3.3.创建配置文件application.yml

server:
  port: 8081
spring:
  application:
    name: shop-product

  datasource:
    url: jdbc:mysql://localhost:3306/schooldb?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
  jpa:
    properties:
      hibernate:
        hbm2ddl:
          auto: update
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect

3.4.创建ProductDao接口

package com.myl.dao;

import com.myl.entity.Product;
import org.springframework.data.jpa.repository.JpaRepository;
// Product:实体     Long:Id类型
public interface ProductDao extends JpaRepository {

}

3.5.创建ProductService接口

package com.myl.service;
import com.myl.entity.Product;

public interface ProductService {
    public Product findById(Long pid);
}

3.6.创建ProductServiceImpl实现类

package com.myl.service.impl;

import com.myl.dao.ProductDao;
import com.myl.entity.Product;
import com.myl.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductDao productDao;
    @Override
    public Product findById(Long pid) {
        return productDao.findById(pid).get();
    }
}

3.7. 创建ProductController类

package com.myl.controller;

import com.myl.entity.Product;
import com.myl.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProductController {
    @Autowired
    private ProductService productService;
    @RequestMapping("/findById/{pid}")
    public Product findById(@PathVariable(value = "pid") Long pid){
           Product product= productService.findById(pid);
           return product;
    }

}

3.8.启动工程,等到数据库表创建完毕之后,加入测试数据
SpringClound 微服务环境搭建_第2张图片
4.创建订单微服务
4.1创建一个名为 shop-order 的模块,并添加springboot依赖

 
        
            com.myl
            shop-common114
            1.0-SNAPSHOT
        
        
            org.springframework.boot
            spring-boot-starter-web
        
    

4.2创建启动类

package com.myl;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class OrderApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class,args);
    }
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

4.3创建配置文件application.yml

server:
  port: 8091
spring:
  application:
    name: shop-order

  datasource:
    url: jdbc:mysql://localhost:3306/schooldb?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
  jpa:
    properties:
      hibernate:
        hbm2ddl:
          auto: update
        dialect: org.hibernate.dialect.MySQL5InnoDBDialect

4.4创建OrderDao接口

package com.myl.dao;

import com.myl.entity.Order;
import org.springframework.data.jpa.repository.JpaRepository;

public interface OrderDao  extends JpaRepository {
}

4.5 创建OrderService接口

package com.myl.service;

import com.myl.entity.Order;

public interface OrderService {
    public void save(Order order);
}

4.6创建OrderServiceImpl实现类

package com.myl.service.impl;

import com.myl.dao.OrderDao;
import com.myl.entity.Order;
import com.myl.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Override
    public void save(Order order) {
        orderDao.save(order);

    }
}

4.7创建OrderController类

package com.myl.controller;

import com.alibaba.fastjson.JSON;
import com.myl.entity.Order;
import com.myl.entity.Product;
import com.myl.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private RestTemplate restTemplate;
    @RequestMapping("/save/{pid}")
    public Order save(@PathVariable(value = "pid") Long pid){
        //调用商品微服务,得到要购买的商品
        Product product = restTemplate.getForObject("http://127.0.0.1:8081/findById/"+pid, Product.class);
        System.out.println("查询商品微服务信息"+ JSON.toJSONString(product));
        Order order=new Order();
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setUsername("admin");
        order.setNumber(1);
        order.setPid(product.getPid());
        order.setUid(1L);
        orderService.save(order);
        System.out.println("下单成功");
        return order;
    }
}

4.8启动工程并在浏览器上测试
SpringClound 微服务环境搭建_第3张图片

你可能感兴趣的:(springclound,spring)