谷粒商城学习日记(9)——使用spring cloud alibaba的nacos组件作注册中心和Feign(远程调用)

配置阿里云的公共配置(版本配置)

在公共的模块common中添加如下代码

 <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.5.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

来实现对整个spring cloud alibaba 中间件的版本控制

集成中间件

集成spring cloud alibaba 的中间件可以参考官方文档
里面包含中间件的集成demo
官方文档

使用NACOS做注册中心

1.首先,修改 pom.xml 文件,引入 Nacos Discovery Starter。

 <dependency>
     <groupId>com.alibaba.cloud</groupId>
     <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
 </dependency>

2.在应用的 /src/main/resources/application.yml文件

spring:
  application:
    name: gulimall-coupon
  datasource:
    url: jdbc:mysql://47.103.206.81:3306/gulimall_sms?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: 
    driver-class-name: com.mysql.jdbc.Driver
#配置 Nacos Server 地址
  cloud:
    nacos:
      discovery:
        server-addr: 123.57.234.28:8848

3.使用 @EnableDiscoveryClient 注解开启服务注册与发现功能

 @SpringBootApplication
 @EnableDiscoveryClient
 public class ProviderApplication {
    
    

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

下载nacos并启动

nacos下载地址

下载解压到服务器,并打开防火墙端口,启动nacos

sh startup.sh -m standalone

使用openFeign远程调用

1.首先需要导入依赖到需要调用的服务这里我使用的是customer

扫描二维码关注公众号,回复: 12882444 查看本文章
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2 编写一个接口,接口告诉springcloud这个接口需要调用远程服务
2.1 在接口里声明@FeignClient(“gulimall-coupon”)他是一个远程调用客户端且要调用coupon服务
2.2 要调用coupon服务的/coupon/coupon/info/${id}方法
3 开启远程调用功能 @EnableFeignClients,要指定远程调用功能放的基础包

customer yml

spring:
  datasource:
    url: jdbc:mysql://47.103.216.22:3306/gulimall_ums?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: 060988wxf
    driver-class-name: com.mysql.jdbc.Driver
  application:
    name: gulimall-customer
#配置 Nacos Server 地址
  cloud:
    nacos:
      discovery:
        server-addr: 123.57.224.38:8848

coupon控制层新增测试方法

    @RequestMapping("/test")
    public R id(){
    
    
        CouponEntity coupon = couponService.getById(1);
        return R.ok().put("coupon", coupon);
    }

customer 启动类


@MapperScan("com.xfwang.gulimall.customer.dao")
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients(basePackages="com.xfwang.gulimall.customer.feign")
public class GulimallCustomerApplication {
    
    

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

}

com.xfwang.gulimall.customer.feign接口类

@FeignClient("gulimall-coupon")
public interface CouponFeignService {
    
    

    /**
     * 远程服务的url
     * 注意写全优惠券类上还有映射//注意我们这个地方不是控制层,所以这个请求映射请求的不是我们服务器上的东西,而是nacos注册中心的
     */
    @RequestMapping("/coupon/coupon/test")
    public R membercoupons();
}

调用类

@RestController
@RequestMapping("customer/member")
public class MemberController {
    
    
    @Autowired
    private MemberService memberService;

    @Autowired
    CouponFeignService couponFeignService;

    /**
     * 测试远程调用
     */
    @RequestMapping("/info")
    public R test(){
    
    
        return couponFeignService.membercoupons();
    }
 }

重启两边服务

调用customer的接口即可

----------------------遇到的问题-------------------------------------

使用openFeign组件报错,查询资料是springboot和springcloud的版本太高了需要引入新包

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-loadbalancer</artifactId>
		</dependency>

启动服务访问还是报错,查询资料需要降低springboot和springcloud的版本

于是把需要用到的customer的springboot版本和springcould版本下调

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
	</properties>

springboot和springcloud的版本对应关系的在官网
官网

顺便把common的spring cloud alibaba的版本下调了

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
   </dependencyManagement>

猜你喜欢

转载自blog.csdn.net/menxinziwen/article/details/114581620