说明:本人书写该篇博客原因主要有两个:一、方便本人查阅,二、为全小白且想学微服务的朋友进行查阅。以下内容主要来源于余胜军,本人在他基础上将步骤进行细化,使小白也能看懂,请大家在转载的时候也引入余胜军的链接
1.1. 构建parent项目
同理,删除one-shop-basics项目中得src文件
同理创建剩余项目
Eclipse安装yaml插件如果已安装可以跳过该步骤
###服务端口号 建议多个服务间设定的端口号要有一定的规律,比如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
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); } }
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; } }
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();
###微信服务启动端口号 server: port: 8200 ###服务名称(服务注册到eureka名称) spring: application: name: app-one-weixin ###服务注册到eureka地址 eureka: client: service-url: defaultZone: http://localhost:8100/eureka
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; } }
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); } }
package com.one.member; import com.one.weixin.entity.AppEntity; public interface MemberService { /* * 会员服务调用微信接口 */ public AppEntity memberInvokeWeixin(); }
###会员服务启动端口号 server: port: 8300 ###服务名称(服务注册到eureka名称) spring: application: name: app-one-member ###服务注册到eureka地址 eureka: client: service-url: defaultZone: http://localhost:8100/eureka
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{ }
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(); } }
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