搭建微服务电商项目框架--创建微信及会员服务

说明:本人书写该篇博客原因主要有两个:一、方便本人查阅,二、为全小白且想学微服务的朋友进行查阅。以下内容主要来源于余胜军,本人在他基础上将步骤进行细化,使小白也能看懂,请大家在转载的时候也引入余胜军的链接

1.1. 创建项目工作集

1.1. 电商项目功能结构图

1.1. 构建项目

1.1. 构建parent项目

1.1. 构建一级子类

同理,删除one-shop-basics项目中得src文件

1.1. 建立one-shop-basics下得子项目

同理创建剩余项目

1.1. 2.搭建项目

1.1.1. 搭建eureka

1.1.1.1. 安装yaml插件

Eclipse安装yaml插件如果已安装可以跳过该步骤

1.1.1.1. 创建eureka 配置文件application.yml

###服务端口号  建议多个服务间设定的端口号要有一定的规律,比如eureka用8100  订单服务用8200等规则
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

  

1.1.1.1. Eureka启动类:AppEureka

package com.cyb.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/*
 * Eureka 启动类
 */
@SpringBootApplication
@EnableEurekaServer
public class AppEureka {

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

1.1.1. 构建微信接口服务

1.1.1.1. 建立实体

package com.one.weixin.entity;

import lombok.Data;

@Data
public class AppEntity {
    
    private String appId;
    private String appName;
    

    public AppEntity() {
        super();
    }

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

1.1.1.1. 建立接口WeiXinAppService

package com.one.weixin.service;

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

import com.one.weixin.entity.AppEntity;

/**
 * 微信服务接口
 * @author 陈远波
 *
 */
public interface WeiXinAppService {

    /**
     * 应用服务接口
     * @return
     */
    @GetMapping("/getApp")
    public AppEntity getApp();

1.1.1. 构建微信实现服务

1.1.1.1. 微信服务配置文件application.yml

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

1.1.1.1. 构建微信实现服务类WeiXinAppServiceImpl

package com.one.weixin.service.impl;

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

import com.one.weixin.entity.AppEntity;
import com.one.weixin.service.WeiXinAppService;

/**
 * 微信服务接口的实现
 * @author 陈远波
 *
 */
@RestController
public class WeiXinAppServiceImpl implements WeiXinAppService {

    @GetMapping("/getApp")
    public AppEntity getApp() {
        // TODO Auto-generated method stub
        AppEntity app= new AppEntity("appId:","appName");
        return app;
    }

}

1.1.1.1. Springboot启动类

package com.one;

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


/*
 * Eureka 启动类
 */
@SpringBootApplication
@EnableEurekaClient
public class AppWeiXin {

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

1.1.1. 构建会员接口服务

1.1.1.1. 会员接口类

package com.one.member;
import com.one.weixin.entity.AppEntity;

public interface MemberService {

    /*
     * 会员服务调用微信接口
     */
    public AppEntity memberInvokeWeixin();
}

1.1.1. 构建会员实现服务

1.1.1.1. 会员服务配置文件application.yml

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

1.1.1.1. 会员调用微信接口的feign客户端接口

package com.one.member.feign;

import org.springframework.cloud.openfeign.FeignClient;

import com.one.weixin.service.WeiXinAppService;

@FeignClient(name="app-one-weixin")
public interface WeixinAppServiceFeign extends WeiXinAppService{

}

1.1.1.1. 会员实现类

package com.one.member.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.one.member.MemberService;
import com.one.member.feign.WeixinAppServiceFeign;
import com.one.weixin.entity.AppEntity;
import com.one.weixin.service.WeiXinAppService;

@RestController
public class MemberServiceImpl implements MemberService{

    @Autowired
    private WeixinAppServiceFeign weixinAppServiceFeign;
    
    

    @GetMapping("/memberInvokeWeixin")
    @Override
    public AppEntity memberInvokeWeixin() {
        // TODO Auto-generated method stub
        return weixinAppServiceFeign.getApp();
    }

}

1.1.1.1. 启动类

package com.one;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class AppMember {

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

同理,微信服务按同样步骤进行搭建
以上内容仅供参考,需要该篇博客源码的可以私聊我,本人博客园地址为:https://www.cnblogs.com/chenyuanbo/

原文地址:https://www.cnblogs.com/chenyuanbo/p/12105228.html