-
实现效果
member会员服务调用coupon优惠券服务,查询用户所拥有的优惠券 -
导入依赖包
<!-- 导入服务发现包,这样能获取注册中心中的微服务--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- openfeign实现远程调用和负载均衡--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
-
编写接口,告诉springcloud这个接口需要调用远程服务(声明接口中的每个方法分别调用了哪个远程服务的哪个请求)
package com.kenai.gulimall.member.feign; import com.kenai.common.utils.R; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; //表明自己是一个会调用coupon服务的Feign客户端 @FeignClient("gulimall-coupon") public interface CouponFeignService { // 声明要调用哪个服务的哪个功能,并完整复制该方法的路径名及方法名(签名) @RequestMapping("/coupon/coupon/member/list") //声明要调用远程服务的哪个方法 public R memberCoupons(); }
-
在启动类中开启远程调用服务
package com.kenai.gulimall.member; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @EnableDiscoveryClient @SpringBootApplication // 开启feign远程调用功能,所有远程调用接口放在basePackages指定包下 @EnableFeignClients(basePackages = "com.kenai.gulimall.member.feign") public class GulimallMemberApplication { public static void main(String[] args) { SpringApplication.run(GulimallMemberApplication.class, args); } }
-
coupon优惠券服务查询优惠券代码
@RestController @RequestMapping("coupon/coupon") public class CouponController { @Autowired private CouponService couponService; @RequestMapping("/member/list") public R membercoupons(){ CouponEntity couponEntity = new CouponEntity(); couponEntity.setCouponName("满100减10"); return R.ok().put("coupons", Arrays.asList(couponEntity)); } }
-
member会员服务远程调用coupon服务,查询用户优惠券代码
@RestController @RequestMapping("member/member") public class MemberController { @Autowired private MemberService memberService; @Autowired private 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")); } }
-
由于可能出现版本不兼容导致运行失败问题,所有依赖jar包文件如下
<!-- member会员服务pom.xml文件,与coupon类似--> <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.kenai.gulimall</groupId> <artifactId>gulimall-member</artifactId> <version>0.0.1-SNAPSHOT</version> <name>gulimall-member</name> <description>谷粒商城会员服务</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR8</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>com.kenai.gulimall</groupId> <artifactId>gulimall-common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
公共微服务common的pom.xml文件 <?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>gulimall</artifactId> <groupId>com.kenai.gulimall</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>gulimall-common</artifactId> <description>每一个微服务公共的依赖,bean,工具类等</description> <dependencies> <!-- mybatisplus与springboot整合,这样在application.yml中会有mybatis-plus提示,当然有其他作用--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.8</version> </dependency> <!-- httpcomponents:java代码发送http请求的工具类--> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4.12</version> </dependency> <!-- 提供一些基础的、通用的操作和处理,如自动生成toString()的结果、自动实现hashCode()和equals()方法、数组操作、枚举、日期和时间的处理等--> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <!-- mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.17</version> </dependency> <!-- servelet-api在tomcat中自带,所以设置scope为provided,表示编译期有效,运行期不需要提供,不会打入包中--> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.1.1.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
nacos作为注册中心使用实例
猜你喜欢
转载自blog.csdn.net/qq_26496077/article/details/113922493
今日推荐
周排行