《果然新鲜》电商项目(04)- 注册中心及Feign远程调用

1.引言

在上一节《果然新鲜电商项目(03)- 父工程配置》主要描述了「果然新鲜」项目的父类公共内容配置。
本文将开始讲解Eureka注册中心的配置以及微服务间的Feign相互调用。

2.Eureka注册中心配置

模块定位:配置基础设施下(guoranshengxian-shop-basics)的注册中心模块(guoranshengxian-shop-basics-eureka)。

2.1.配置maven

<?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>
        <groupId>com.guoranxinxian</groupId>
        <artifactId>guoranxinxian-shop-basics</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>guoranxinxian-shop-basics-eureka</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

</project>

2.2.application.yml配置

###服务端口号
server:
  port: 8100
###eureka 基本信息配置
eureka:
  instance:
    ###注册到eurekaip地址
    hostname: 127.0.0.1
  client:
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    ###因为自己是为注册中心,不需要自己注册自己
    register-with-eureka: false
    ###因为自己是为注册中心,不需要检索服务
    fetch-registry: false

2.3.启动类

package com.guoranshengxian;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class AppEureka {

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

2.4.启动Eureka

在这里插入图片描述

3.微信服务配置

模块定位:接口实现层下(guoranshengxian-shop-service)的微信服务模块(guoranshengxian-shop-service-weixin)。

3.1 配置maven

<?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>guoranxinxian-shop-service</artifactId>
        <groupId>com.guoranxinxian</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>guoranxinxian-shop-service-weixin</artifactId>


</project>

3.2 application.yml配置

###服务启动端口号
server:
  port: 8200
###服务名称(服务注册到eureka名称)
spring:
  application:
    name: guoranxinxian-shop-service-weixin
###服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8100/eureka


4.会员服务配置

模块定位:接口实现层下(guoranxinxian-shop-service)的会员服务模块(guoranxinxian-shop-service-member)。

4.1.配置maven

<?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>guoranxinxian-shop-service</artifactId>
        <groupId>com.guoranxinxian</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>guoranxinxian-shop-service-member</artifactId>

</project>

4.2.application.yml配置

###服务启动端口号
server:
  port: 8300
###服务名称(服务注册到eureka名称)
spring:
  application:
    name: guoranxinxian-shop-service-member
###服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8100/eureka

4.3. 启动类

package com.guoranxinxian;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class AppMember {

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

5.会员服务Feign远程调用微信服务

5.1.微信服务接口配置

模块定位:接口层下(guoranxinxian-shop-service-api)的微信服务模块(guoranxinxian-shop-service-api-weixin)。

<?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>guoranxinxian-shop-service-api</artifactId>
        <groupId>com.guoranxinxian</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>guoranxinxian-shop-service-api-weixin</artifactId>

</project>

5.1.1 定义实体类

模块定位:接口层下(guoranxinxian-shop-service-api)的微信服务模块(guoranxinxian-shop-service-api-weixin)。

package com.guoranxinxian.entity;

import lombok.Data;

@Data
public class AppEntity {

    /**
     * 应用id
     */
    private String appId;
    /**
     * 应用密钥
     */
    private String appSecret;

    public AppEntity() {

    }

    public AppEntity(String appId, String appSecret) {
        super();
        this.appId = appId;
        this.appSecret = appSecret;
    }
}

5.1.2 定义接口

模块定位:接口层下(guoranxinxian-shop-service-api)的微信服务模块(guoranxinxian-shop-service-api-weixin)。

package com.guoranxinxian.service;

import com.guoranxinxian.entity.AppEntity;
import org.springframework.web.bind.annotation.GetMapping;

public interface AppService {

    /**
     * 获取app应用信息
     *
     * @return
     */
    @GetMapping("/getApp")
    public AppEntity getApp();

}

5.2 会员服务接口配置

5.2.1.定义会员调用微信接口

1.添加微信接口模块依赖:

<?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>guoranxinxian-shop-service</artifactId>
        <groupId>com.guoranxinxian</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>guoranxinxian-shop-service-member</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.guoranxinxian</groupId>
            <artifactId>guoranxinxian-shop-service-api-weixin</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

2.调用微信服务接口:guoranxinxian-shop-service-api-member

package com.guoranxinxian.service;

import com.guoranxinxian.entity.AppEntity;
import org.springframework.web.bind.annotation.GetMapping;

public interface MemberService {

    @GetMapping("/memberInvokWeixin")
    public AppEntity memberInvokWeixin();
}

5.3 会员服务调用微信服务

模块定位:接口实现层下(guoranxinxian-shop-service)的微信服务模块(guoranxinxian-shop-service-weixin)和会员服务(guoranxinxian-shop-service-member)。

5.3.1 Service父模块添加Feign依赖

接口实现层(guoranxinxian-shop-service)添加maven依赖:

   <dependencies>
        <!-- springcloud feign组件 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

5.3.2 微信被调用接口实现

package com.guoranxinxian.impl;

import com.guoranxinxian.entity.AppEntity;
import com.guoranxinxian.service.AppService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AppServiceImpl implements AppService {

    @GetMapping("/getApp")
    public AppEntity getApp() {
        return new AppEntity("1962264482", "ylw666");
    }
}

5.3.3 会员调用微信接口实现

模块定位:接口实现层下(guoranxinxian-shop-service)的会员服务(guoranxinxian-shop-service-member)。

1.定义feign接口:

package com.guoranxinxian.feign;

import com.guoranxinxian.service.AppService;
import org.springframework.cloud.openfeign.FeignClient;

@FeignClient(name = "guoranxinxian-shop-service-weixin")
public interface AppServiceFeign extends AppService {

}

2.调用微信接口:

import com.guoranxinxian.entity.AppEntity;
import com.guoranxinxian.feign.AppServiceFeign;
import com.guoranxinxian.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MemberServiceImpl implements MemberService {

    @Autowired
    private AppServiceFeign appServiceFeign;

    @GetMapping("/memberInvokWeixin")
    public AppEntity memberInvokWeixin() {
        return appServiceFeign.getApp();
    }
}

5.4.创建DTO层:guoranxinxian-shop-api-dto

<?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>guoranxinxian-shop</artifactId>
        <groupId>com.guoranxinxian</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>guoranxinxian-shop-api-dto</artifactId>

</project>

5.4.1.创建DTO实体层:guoranxinxian-shop-api-weixin-dto

<?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>guoranxinxian-shop</artifactId>
        <groupId>com.guoranxinxian</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>guoranxinxian-shop-api-dto</artifactId>
    <packaging>pom</packaging>
    <modules>
        <module>guoranxinxian-shop-api-weixin-dto</module>
    </modules>


</project>

5.4.2.创建DTO实体层:com.guoranxinxian.weixin.dto

package com.guoranxinxian.weixin.dto;


import lombok.Data;

@Data
public class AppEntity {

    /**
     * 应用id
     */
    private String appId;
    /**
     * 应用密钥
     */
    private String appSecret;

    public AppEntity() {

    }

    public AppEntity(String appId, String appSecret) {
        super();
        this.appId = appId;
        this.appSecret = appSecret;
    }

}

6.启动

注意:会员启动类AppMember要加上@EnableFeignClients注解

在这里插入图片描述
1.会员服务调用微信服务,浏览器输入:http://localhost:8300/memberInvokWeixin,可以看到远程调用Feign服务成功。
在这里插入图片描述

6.总结

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值