說明:本人書寫該篇博客原因主要有兩個:一、方便本人查閱,二、為全小白且想學微服務的朋友進行查閱。以下內容主要來源於余勝軍,本人在他基礎上將步驟進行細化,使小白也能看懂,請大家在轉載的時候也引入余勝軍的鏈接
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/