nacos作为注册中心使用实例

  1. 实现效果
    member会员服务调用coupon优惠券服务,查询用户所拥有的优惠券

  2. 导入依赖包

    <!-- 导入服务发现包,这样能获取注册中心中的微服务-->
    <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>
    
  3. 编写接口,告诉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();
    }
    
  4. 在启动类中开启远程调用服务

    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);
    	}
    }
    
  5. 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));
        }
    }
    
  6. 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"));
        }
    }
    
  7. 由于可能出现版本不兼容导致运行失败问题,所有依赖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>
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值