您的位置:首页 - 文章 - Spring Cloud - 正文

SpringCould-product_server


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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.sxpcwlkj</groupId>
    <artifactId>product_server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>product_server</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <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>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

配置文件application.yml

server:
  port: 8771
#指定注册中心地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
#服务的名称
spring:
  application:
    name: product-service

启动类没变化

package com.sxpcwlkj.product_server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ProductServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProductServerApplication.class, args);
    }
}

启动项目  查看Eureka 控制网页是否监听拉去到服务

一、实现商品服务的网接口

(常规controller api 访问查询数据库)

实体类:Product

package com.sxpcwlkj.product_server.entity;
import java.math.BigDecimal;
public class Product {
    //id
    private int  id;
    //商品名称
    private String name;
    //商品价格
    private BigDecimal pice;
    //库存
    private int  num;
    public Product() {
    }
    public Product(int id, String name, BigDecimal pice, int num) {
        this.id = id;
        this.name = name;
        this.pice = pice;
        this.num = num;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public BigDecimal getPice() {
        return pice;
    }
    public void setPice(BigDecimal pice) {
        this.pice = pice;
    }
    public int getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }
}

ProductService类

package com.sxpcwlkj.product_server.service;
import com.sxpcwlkj.product_server.entity.Product;
import java.util.List;
public interface ProductService {
    List<Product> lestProductAll();
    int addPrduct(Product product);
    Product selectBuyId(int id);
}

ProductServiceImpl类

package com.sxpcwlkj.product_server.service.impl;
import com.sxpcwlkj.product_server.entity.Product;
import com.sxpcwlkj.product_server.service.ProductService;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
@Service
public class ProductServiceImpl implements ProductService {
    static List<Product>  list=new ArrayList<>();
    static {
        Product  product1=new Product(1,"铅笔",new BigDecimal(2),100);
        list.add(product1);
        Product  product2=new Product(2,"钢笔",new BigDecimal(12),100);
        list.add(product2);
        Product  product3=new Product(3,"毛笔",new BigDecimal(5),100);
        list.add(product3);
        Product  product4=new Product(4,"彩笔",new BigDecimal(6),100);
        list.add(product4);
        Product  product5=new Product(5,"眉笔",new BigDecimal(12),100);
        list.add(product5);
    }
    @Override
    public List<Product> lestProductAll() {
        return list;
    }
    @Override
    public int addPrduct(Product product) {
        list.add(product);
        return 0;
    }
    @Override
    public Product selectBuyId(int id) {
        return list.get(id+1);
    }
}

ProduceController类

package com.sxpcwlkj.product_server.controller;
import com.sxpcwlkj.product_server.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/v1/product")
public class ProduceController {
    @Autowired
    private ProductService productService;
    /**
     * 查询所有
     * @return
     */
    @RequestMapping("listAll")
    public Object selectList(){
        return productService.lestProductAll();
    }
    /**
     * 查询单品
     * @param id
     * @return
     */
    @RequestMapping("selectById")
    public Object selectById(int  id){
        return productService.selectBuyId(id);
    }
}

http://localhost:8771/api/v1/product/listAll

本文原创,作者:西决,其版权均为品创网络所有。如需转载,请注明出处:https://www.sxpcwlkj.com/springcould-product_server/

发表评论