SpringClound 微服务环境搭建
程序员文章站
2022-06-14 14:46:37
...
1.创建父工程
创建一个maven工程,然后在pom.xml文件中添加下面内容
<!--引入父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<!--引入相关的jar包管理-->
<!--引入springcloud和alibaba的版本管理-->
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF- 8</project.reporting.outputEncoding>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
<spring-cloud-alibaba.version>2.1.0.RELEASE</spring-cloud-alibaba.version>
</properties>
<!--dependencyManagement: 只对jar的版本进行管理,如果子工程想使用,还得引入。只是不需要引入版本号。-->
<dependencyManagement>
<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>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
版本对应:
2.搭建公共工程shop-common
2.1.引入依赖
<!--加入依赖-->
<dependencies>
<!--mybatis dao(方法)===mapper映射-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
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依赖
<dependencies>
<dependency>
<groupId>com.ykq</groupId>
<artifactId>shop-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
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> Product:实体 Long:Id类型
public interface ProductDao extends JpaRepository<Product,Long> {
}
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.启动工程,等到数据库表创建完毕之后,加入测试数据
4.创建订单微服务
4.1创建一个名为 shop-order 的模块,并添加springboot依赖
<dependencies>
<dependency>
<groupId>com.myl</groupId>
<artifactId>shop-common114</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
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<Order,Long> {
}
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启动工程并在浏览器上测试
上一篇: 康熙皇帝晚年的一个预言,百年后竟然成真
下一篇: 搭建LAMP服务器环境流程