其他分享
首页 > 其他分享> > springcloud-alibaba dubbo/feign 20220905

springcloud-alibaba dubbo/feign 20220905

作者:互联网

Feign组件为内部服务通信(声明式HTTP客户端) 简洁、方便、优雅 微服务之间的通信 REST API HTTP并不会开启KeepAlive功能,当前连接为短连接,每次请求都需TCP连接,效率低下   外部服务REST API/内部调用Feign组件(声明式HTTP调用)                   外使用REST API/内服务使用RPC===>Dubbo Spring Cloud                   外/内服务 性能对比   RESTAPI/Dubbo RPC>RESTAPI/Feign            

一、dubboapi接口层

1、pom.xml

<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>
    <groupId>com.sc</groupId>
    <artifactId>dubboapi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubboapi</name>
    <description>Demo project for Spring Boot</description>

2、dubboapi
package com.sc.dubboapi.dubboapi;

public interface IHelloService {
        String sayHello(String name);
}

 

 

 

二、providerdubbo2031生产者

引用dubboapi 0.0.1-SNAPSHOT接口包

1、pom.xml

   <modelVersion>4.0.0</modelVersion>
    <groupId>com.sc</groupId>
    <artifactId>provider-dubbo2031</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>provider-dubbo2031</name>
    <description>Demo project for Spring Boot</description>

  <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
        <spring-cloud-alibaba.version>2.2.2.RELEASE</spring-cloud-alibaba.version>
    </properties>
<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
  <dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  </dependency>
  <dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-dubbo</artifactId>
  </dependency>
  <dependency>
    <groupId>com.sc</groupId>
    <artifactId>dubboapi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </dependency>
</dependencies>

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-dependencies</artifactId>
      <version>${spring-boot.version}</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-alibaba-dependencies</artifactId>
      <version>${spring-cloud-alibaba.version}</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

 


2、application.properties


# 应用名称
spring.application.name=providerdubbo2031
server.port=2031
##dubbo
dubbo.scan.base-packages=com.sc.providerdubbo2031.service
dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
dubbo.registry.address=spring-cloud://localhost
##nacos
spring.cloud.nacos.discovery.username=nacos
spring.cloud.nacos.discovery.password=nacos
# Nacos 服务发现与注册配置,其中子属性 server-addr 指定 Nacos 服务器主机和端口
spring.cloud.nacos.discovery.server-addr=localhost:8848
# 注册到 nacos 的指定 namespace,默认为 public
spring.cloud.nacos.discovery.namespace=public

 


3、HelloServiceImpl


package com.sc.providerdubbo2031.service;

import com.sc.dubboapi.dubboapi.IHelloService;
import org.apache.dubbo.config.annotation.DubboService;

  @DubboService
   public class HelloServiceImpl implements IHelloService {
      public String sayHello(String name) {
         return "Hello World"+name;
     }
 }

 

4、ProviderDubbo2031Application
package com.sc.providerdubbo2031;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

   @EnableDiscoveryClient
   @EnableAutoConfiguration
   @SpringBootApplication
   public class ProviderDubbo2031Application {

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

  }

 

\

 

 

 

 

 

三、customerdubbo2032消费者

引用dubboapi 0.0.1-SNAPSHOT接口包
1、pom.xml
<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
  <dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  </dependency>
  <dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-dubbo</artifactId>
  </dependency>
  <dependency>
    <groupId>com.sc</groupId>
    <artifactId>dubboapi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </dependency>
</dependencies>

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-dependencies</artifactId>
      <version>${spring-boot.version}</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-alibaba-dependencies</artifactId>
      <version>${spring-cloud-alibaba.version}</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
   </dependencies>
</dependencyManagement>


2、application.properties


# 应用名称
spring.application.name=customerdubbo2032
server.port=2032
##dubbo####与nacos的提供者服务一致providerdubbo2031
dubbo.cloud.subscribed-services=providerdubbo2031
dubbo.protocol.port=20881
##nacos
spring.cloud.nacos.discovery.username=nacos
spring.cloud.nacos.discovery.password=nacos
# Nacos 服务发现与注册配置,其中子属性 server-addr 指定 Nacos 服务器主机和端口
spring.cloud.nacos.discovery.server-addr=localhost:8848
# 禁此注册到 nacos
spring.cloud.nacos.discovery.register-enabled=false

 

 

 

3、HelloController
   package com.sc.customerdubbo2032.control;

  import com.sc.dubboapi.dubboapi.IHelloService;
   import org.apache.dubbo.config.annotation.DubboReference;
   import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;

  @RestController
  public class HelloController {

        @DubboReference(check = false)
        private IHelloService helloService;

       @GetMapping("/say")
       public String sayHello(){
              return helloService.sayHello("Lily2022");
       }
}

 

4、Customerdubbo2032Application
     package com.sc.customerdubbo2032;

     import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

     @EnableDiscoveryClient
     @EnableAutoConfiguration
     @SpringBootApplication
     public class Customerdubbo2032Application {

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

      }

 

 

 

 

 

标签:dubbo,feign,spring,boot,springcloud,org,import,com,cloud
来源: https://www.cnblogs.com/smallfa/p/16659126.html