Spring-Cloud学习(二) Eureka-Client端,以商品服务为例

一、使用Idea自带的Spring initializr创建一个Eureka客户端

  1.1) 创建maven项目

1.2) 修改pom文件,保证spring-boot和spring-cloud-dependencies 的版本和eureka服务端的版本一致

1.3)修改配置文件

spring.application.name=product
server.port=8081

#注册中心eureka服务ip地址
eureka.instance.hostname=localhost
#注册中心eureka服务端口号
eureka.client.eureka-server-port=8761
#注册中心eureka服务地址
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${eureka.client.eureka-server-port}/eureka/

 1.4) 因为需要开发rest服务,故需要引入spring-web的相关jar包

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

1.5)开发一个rest服务

package com.roger.product.controller;

import com.roger.product.entity.ProductInfo;
import com.roger.product.service.ProductInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/product")
@Slf4j
public class ProductController {

    @Autowired
    private ProductInfoService infoService;


    @GetMapping("/list")
    public void list() {
        log.info("1.查询所有在架的商品");
        List<ProductInfo> productInfoList = infoService.findUpAll();
        log.info("2. 获取类目type列表");
        List<Integer> categoryTypeList = productInfoList.stream()
                .map(ProductInfo::getCategoryType)
                .collect(Collectors.toList());
        log.info("3. 查询类目");

        log.info("4. 构造api接口返回数据");
    }
}

1.6) 启动项目,查看结果

猜你喜欢

转载自blog.csdn.net/lihongtai/article/details/88012654