03--springCloudAlibaba 集成 dubbo及springcloud(feign) RPC框架

上一章 创建一个工程并将其注册至nacos,并使用nacos配置
上一章 我们创建了一个生产者服务(producer-example) 并将其注册至了nacos和使用可nacos配置。 本章我们将创建一个消费者服务。并使用dubbo和feign进行rpc通信
  1. 在spring-cloud-alibaba项目中像创建producer-example 一样 创建 consumer-example模块
  2. 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>spring-cloud-alibaba</artifactId>
        <groupId>spring-cloud-alibaba</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>consumer-example</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--nacos作为注册-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--nacos作为配置-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.cloud.alibaba.ConsumerApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
  1. ConsumerApplication.java
package com.cloud.alibaba;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @author 刘志强
 * @date 2020/11/17 09:29
 * @EnableDiscoveryClient 启动客户端
 */
@EnableDiscoveryClient
@SpringBootApplication
@Slf4j
public class ConsumerApplication implements CommandLineRunner {


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

    @Override
    public void run(String... args) {
        log.info("command已启动");

    }
}
  1. nacos配置如下
Data ID: consumer-example-dev.yml
Group:DEFAULT_GROUP
配置格式:YAML
配置内容:
author:
    userName: 刘志强
    mailbox: 2425358736@qq.com
    

Data ID: consumer-example.yml
Group:DEFAULT_GROUP
配置格式:YAML
配置内容:
server:
  port: 10001
  1. bootstrap.yml
spring:
  application:
    name: consumer-example
  profiles:
    active: dev

  main:
    allow-bean-definition-overriding: true
  cloud:
    nacos:
      discovery:
        # 服务注册地址
        server-addr: 101.37.152.195:8848
      config:
        server-addr: 101.37.152.195:8848
        # 配置文件格式
        file-extension: yml
        # 共享配置
        shared-dataids: application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
    refresh:
      enabled: true
  1. application-dev.yml 为consumer-example 和 producer-example 服务共享。在上一章已经创建过了

DUBBO

  1. consumer-example 引入dubbo开发包
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-dubbo</artifactId>
    </dependency>
  1. consumer-example 的bootstrap.yml 增加dubbo配置。也可以将此配置写在nacos中的consumer-example.yml里。 订阅producer-example服务
dubbo:
  registry:
    # 注册地址 nacos服务
    address: spring-cloud://101.37.152.195
  cloud:
    # 订阅
    subscribed-services: producer-example
  application:
    ## 启用服务质量
    qos-enable: true
    qos-port: 22222
  1. producer-example 模块引入dubbo开发包
  2. producer-example 的 bootstrap.yml加入dubbo配置
dubbo:
  registry:
    ## 注册地址 nacos
    address: spring-cloud://52.83.239.30
  scan:
    ## 暴露的服务
    base-packages: com.cloud.alibaba.rpc.dubbo.**
  ## 协议
  protocol:
    name: dubbo
    port: -1
  application:
    qos-enable: true
    qos-port: 22223
  cloud:
    # 不订阅任何服务 默认订阅所由服务
    subscribed-services: ""
  1. 新建dubbo-api模块。存放 生产者 和 消费者 的通用接口与通用实体类
  2. dubbo-api 中 创建 DubboApiService 接口
package com.cloud.alibaba;

/**
 * @author 刘志强
 * @date 2020/11/17 14:48
 */
public interface DubboApiService {

    /**
     * 获取名称
     * @return
     */
    String getUserName();
}

  1. producer-example 和 consumer-example 模块分别引入dubbo-api模块
    <!--dubbo通用接口等信息-->
    <dependency>
        <groupId>spring-cloud-alibaba</groupId>
        <artifactId>dubbo-api</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
  1. producer-example(生产者) 服务中实现DubboApiService 接口。并暴露此服务
package com.cloud.alibaba.rpc.dubbo;

import com.cloud.alibaba.DubboApiService;
import com.cloud.alibaba.domain.Author;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author 刘志强
 * @date 2020/11/17 14:55
 */
@Service(version = "1.0.0")
public class DubboApiImpl implements DubboApiService {

    @Autowired
    private Author author;

    @Override
    public String getUserName() {
        return author.toString();
    }
}


  1. @Service 注解是dubbo 包下的注解,别弄错了。
  2. consumer-example(消费者) 服务 消费此接口
package com.cloud.alibaba.controller;

import com.cloud.alibaba.DubboApiService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 刘志强
 * @date 2020/11/17 14:58
 */
@RestController
public class ConsumerController {

    @Reference(version = "1.0.0", check = false)
    private DubboApiService dubboApiService;

    @GetMapping("getUserName")
    public String getUserName() {
        return dubboApiService.getUserName();
    }

}

  1. 启动生产者 在启动消费者。浏览器访问 http://127.0.0.1:10001/getUserName

Feign

  1. consumer-example 引入 openfeign 开发包
        <!--openfeign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
  1. consumer-example 的bootstrap.yml 配置 feign 和 hystrix
# 开启feign熔断
feign:
  hystrix:
    enabled: true

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            # 超时毫秒
            timeoutInMilliseconds: 10000
  1. consumer-example 启动类增加 @EnableFeignClients 注解 开启Feign 客户端
  2. consumer-example 增加@FeignClient 接口
package com.cloud.alibaba.rpc.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author 刘志强
 * @date 2020/11/17 15:23
 * producer-example 生产者服务的 spring.application.name
 * fallback 当下游服务异常时,本服务的回滚处理类
 */
@FeignClient(value = "producer-example", fallback = FeignServiceImpl.class)
public interface FeignService {

    /**
     * 获取用户名
     * @param authorization
     * @param userName
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/getUserName")
    String getUserName(@RequestHeader(value = "Authorization") String authorization, @RequestParam("userName") String userName);

}

package com.cloud.alibaba.rpc.feign;

import org.springframework.stereotype.Component;

/**
 * @author 刘志强
 * @date 2020/11/17 15:24
 */
@Component
public class FeignServiceImpl implements FeignService {
    @Override
    public String getUserName(String authorization, String userName) {
        return "下游服务调用失败";
    }
}

  1. producer-example 服务增加 ProducerController 并编写getUserName 接口
package com.cloud.alibaba.controller;

import com.cloud.alibaba.domain.Author;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 刘志强
 * @date 2020/11/17 15:31
 */
@RestController
@Slf4j
public class ProducerController {

    @Autowired
    private Author author;
    @Autowired
    private HttpServletRequest httpServletRequest;

    @GetMapping("/getUserName")
    public String getUserName(String userName) {
        log.info(userName);
        String authorization = httpServletRequest.getHeader("Authorization");
        log.info(authorization);
        return author.toString();
    }
}

  1. 分别启动 producer-example 与 consumer-example 服务
  2. 访问 http://127.0.0.1:10001/getUserNameTwo?userName=%E5%88%98%E5%BF%97%E5%BC%BA
源码地址
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值