SpringCloud学习笔记(四、注册数据微服务

<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”>

springcloud

edu.hpu.springcloud

1.0-SNAPSHOT

4.0.0

product-data-service

product-data-service

http://www.example.com

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<maven.compiler.source>1.8</maven.compiler.source>

<maven.compiler.target>1.8</maven.compiler.target>

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

org.springframework.boot

spring-boot-starter-web

实体类Product:

package edu.hpu.springcloud.pojo;

public class Product {

private int id;

private String name;

private int price;

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 int getPrice() {

return price;

}

public void setPrice(int price) {

this.price = price;

}

public Product(int id, String name, int price) {

this.id = id;

this.name = name;

this.price = price;

}

public Product() {

}

}

服务层ProductService:

这里把 端口号 放进了产品信息里。 这个数据服务会做成集群,那么访问者为了分辨到底是从哪个数据微服务取的数据,就需要提供个端口号,才能意识到是从不同的微服务得到的数据。

package edu.hpu.springcloud.pojo;

public class Product {

private int id;

private String name;

private int price;

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 int getPrice() {

return price;

}

public void setPrice(int price) {

this.price = price;

}

public Product(int id, String name, int price) {

this.id = id;

this.name = name;

this.price = price;

}

public Product() {

}

}

控制层ProductController:

把 Product 集合转换成 json 数组。

package edu.hpu.springcloud.controller;

import edu.hpu.springcloud.pojo.Product;

import edu.hpu.springcloud.service.ProductService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController

public class ProductController {

@Autowired

private ProductService productService;

@RequestMapping("/products")

public Object products() {

List ps = productService.listProducts();

return ps;

}

}

启动类ProductDataServiceApplication:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值