Spring Cloud Alibaba-微服务搭建(二)

4. 创建用户微服务

步骤:

  1.  创建模块 导入依赖
  2. 创建SpringBoot主类
  3. 加入配置文件
  4. 创建必要的接口和实现类(controller service dao)

新建一个 shop-user 模块,然后进行下面操作
1 创建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>springcloud-alibaba</artifactId>
    <groupId>com.itheima</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>
  <artifactId>shop-user</artifactId>
  <dependencies>
    <dependency>
      <groupId>com.itheima</groupId>
      <artifactId>shop-common</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>

2 编写主类

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

3 创建配置文件

server:
port: 8071
spring:
application:
 name: service-product
datasource:
 driver-class-name: com.mysql.jdbc.Driver
 url: jdbc:mysql:///shop?
serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
 username: root
 password: root
jpa:
 properties:
  hibernate:
   hbm2ddl:
    auto: update
   dialect: org.hibernate.dialect.MySQL5InnoDBDialect

5 创建商品微服务

1 创建一个名为 shop_product 的模块,并添加springboot依赖

<?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>springcloud-alibaba</artifactId>
    <groupId>com.itheima</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>
  <artifactId>shop-product</artifactId>
  <dependencies>
    <dependency>
            <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>com.itheima</groupId>
      <artifactId>shop-common</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>

2 创建工程的主类

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

3 创建配置文件application.yml

server:
port: 8081
spring:
application:
 name: service-product
datasource:
 driver-class-name: com.mysql.jdbc.Driver
 url: jdbc:mysql:///shop?
serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
 username: root
 password: root
jpa:
 properties:
  hibernate:
   hbm2ddl:
    auto: update
   dialect: org.hibernate.dialect.MySQL5InnoDBDialect

4 创建ProductDao接口

package com.itheima.dao;
public interface ProductDao extends JpaRepository<Product,Integer> {
}

5 创建ProductService接口和实现类

package com.itheima.service.impl;
@Service
public class ProductServiceImpl implements ProductService {
  @Autowired
  private ProductDao productDao;
  @Override
  public Product findByPid(Integer pid) {
    return productDao.findById(pid).get();
 }
}

6 创建Controller

@RestController
@Slf4j
public class ProductController {
  @Autowired
  private ProductService productService;
  @GetMapping("/product/{pid}")
  public Product product(@PathVariable("pid") Integer pid) {
    Product product = productService.findByPid(pid);
    log.info("查询到商品:" + JSON.toJSONString(product));
    return product;
 }
}

7 启动工程,等到数据库表创建完毕之后,加入测试数据

INSERT INTO shop_product VALUE(NULL,'小米','1000','5000');
INSERT INTO shop_product VALUE(NULL,'华为','2000','5000');
INSERT INTO shop_product VALUE(NULL,'苹果','3000','5000');
INSERT INTO shop_product VALUE(NULL,'OPPO','4000','5000');

8 通过浏览器访问服务

 6 创建订单微服务

1 创建一个名为 shop-order 的模块,并添加springboot依赖

<?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>springcloud-alibaba</artifactId>
    <groupId>com.itheima</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>
  <artifactId>shop-order</artifactId>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>com.itheima</groupId>
      <artifactId>shop-common</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>

2 创建工程的主类

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

3 创建配置文件application.yml

server:
port: 8091
spring:
application:
 name: service-product
datasource:
 driver-class-name: com.mysql.jdbc.Driver
 url: jdbc:mysql:///shop?
serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
 username: root
 password: root
jpa:
 properties:
  hibernate:
   hbm2ddl:
    auto: update
   dialect: org.hibernate.dialect.MySQL5InnoDBDialect

4 创建OrderDao接口

package com.itheima.dao;
public interface OrderDao extends JpaRepository<Order,Long> { 
}

5 创建OrderService接口和实现类

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

6 创建RestTemplate

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

7 创建Controller

package com.itheima.controller;
@RestController
@Slf4j
public class OrderController {
  @Autowired
  private RestTemplate restTemplate;
  @Autowired
  private OrderService orderService;
  //准备买1件商品
  @GetMapping("/order/prod/{pid}")
  public Order order(@PathVariable("pid") Integer pid) {
    log.info(">>客户下单,这时候要调用商品微服务查询商品信息");
    //通过restTemplate调用商品微服务
    Product product = restTemplate.getForObject(
     "http://localhost:8081/product/" + pid, Product.class);

    log.info(">>商品信息,查询结果:" + JSON.toJSONString(product));
    Order order = new Order();
    order.setUid(1);
    order.setUsername("测试用户");
    order.setPid(product.getPid());
    order.setPname(product.getPname());
    order.setPprice(product.getPprice());
    order.setNumber(1);
    orderService.save(order);
    return order;
 }
}

7 启动工程,通过浏览器访问服务进行测试

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chuxuezhe_987

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值