谷粒商城-day02-服务调用发现nacos与远程调用openfeign

Nacos 服务注册与发现

SpringBoot版本统一为 2.1.9.RELEASE

引入 SpringCloud Alibaba

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-alibaba-dependenciesartifactId>
            <version>2.1.0.RELEASEversion>
            <type>pomtype>
            <scope>importscope>
        dependency>
    dependencies>
dependencyManagement>

服务注册与发现


<dependency>
    <groupId>com.alibaba.cloudgroupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>

参考网址 https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme.md

下载 nacos,这里使用 1.1.3 版本

解压,运行 bin 里面的 startup.cmd

以 coupon 模块为例

spring:
#    nacos 服务注册中心地址
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
#     服务的名字
  application:
    name: gulimall-coupon

主启动类上加上 @EnableDiscoveryClient 注解

@EnableDiscoveryClient
@SpringBootApplication
public class GulimallCouponApplication {

    public static void main(String[] args) {
        SpringApplication.run(GulimallCouponApplication.class, args);
    }
}

访问 http://127.0.0.1:8848/nacos/#/login 进入管理界面 账号密码都是 nacos

谷粒商城-day02-服务调用发现nacos与远程调用openfeign_第1张图片

就可以看到我们的服务被注册进来了

把会员服务也注册进去

openfeign 远程调用

 <dependency>
     <groupId>org.springframework.cloudgroupId>
     <artifactId>spring-cloud-starter-openfeignartifactId>
 dependency>

引入依赖

我们完成一个在会员服务里面调用优惠卷的远程调用

// MemberController.java
@RequestMapping("/member/list")
public R membercoupons(){
    CouponEntity couponEntity = new CouponEntity();
    couponEntity.setCouponName("满100减10");
    return R.ok().put("coupons", Arrays.asList(couponEntity));
}

现在 优惠卷服务里面新建一个controller 方法

/**
 *1. 想要远程调用别的服务
 * 1) 引入open-feign
 * 2) 编写一个接口,告诉 SpringCloud 这个接口需要调用远程服务
 *   1.声明接口的每一个方法都是调用哪个远程服务的哪个接口
 *3) 开启远程调用功能
 */
@EnableFeignClients(basePackages = "com.zhouzhou.gulimall.member.feign")
@EnableDiscoveryClient
@SpringBootApplication
public class GulimallMemberApplication {

    public static void main(String[] args) {
        SpringApplication.run(GulimallMemberApplication.class, args);
    }

}

在会员服务里面配置

package com.zhouzhou.gulimall.member.feign;

import com.zhouzhou.common.utils.R;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient("gulimall-coupon")
public interface CouponFeignService {

    @RequestMapping("/coupon/coupon/member/list")
    public R membercoupons();
}

新建 feign 接口声明

@Autowired
CouponFeignService couponFeignService;

@RequestMapping("/coupons")
public R test() {
    MemberEntity memberEntity = new MemberEntity();
    memberEntity.setNickname("张三");

    R membercoupons = couponFeignService.membercoupons();

    return R.ok().put("member", memberEntity).put("coupons", membercoupons.get("coupons"));
}

测试方法

GET localhost:8085/member/member/coupons

{
  "msg": "success",
  "code": 0,
  "coupons": [
    {
      "id": null,
      "couponType": null,
      "couponImg": null,
      "couponName": "满100减10",
      "num": null,
      "amount": null,
      "perLimit": null,
      "minPoint": null,
      "startTime": null,
      "endTime": null,
      "useType": null,
      "note": null,
      "publishCount": null,
      "useCount": null,
      "receiveCount": null,
      "enableStartTime": null,
      "enableEndTime": null,
      "code": null,
      "memberLevel": null,
      "publish": null
    }
  ],
  "member": {
    "id": null,
    "levelId": null,
    "username": null,
    "password": null,
    "nickname": "张三",
    "mobile": null,
    "email": null,
    "header": null,
    "gender": null,
    "birth": null,
    "city": null,
    "job": null,
    "sign": null,
    "sourceType": null,
    "integration": null,
    "growth": null,
    "status": null,
    "createTime": null
  }
}

两个服务调用时必须在注册中心上线

新年快乐❤

你可能感兴趣的:(谷粒商城)