SpringBoot_Feign


SpringBoot_Feign

对应服务以Nacos 为注册中心

服务提供者

第一步

  • pom.xml 文件中引入对应的配置
<properties>
    <spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
    <spring-boot-dependencies.version>2.5.6</spring-boot-dependencies.version>
</properties>

<dependencies>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        <!-- 使用spring loadbalancer,弃用ribbon -->
        <exclusions>
            <exclusion>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.alibaba.nacos</groupId>
                <artifactId>nacos-client</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>com.alibaba.nacos</groupId>
        <artifactId>nacos-client</artifactId>
        <version>2.0.3</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.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>

第二步

  • 配置文件添加配置
spring:
  application:
    name: springboot-nacos-provide

  cloud:
    loadbalancer:
      retry:
        enabled: true
      enabled: true

    nacos:
      discovery:
        server-addr: 172.16.0.127:8848

第三步

  • Controller 层正常定义接口
@RestController
public class ProvideController {
    /**
     * 定义一个方法,对外提供
     * @return
     */
    @PostMapping("/getProvide")
    public String getProvide() {
        return "success";
    }
}

第四步

  • Application 添加注解
@SpringBootApplication
// 这个注解在 SpringBoot 2.5.0 以上可以省略
//@EnableDiscoveryClient
public class SpringbootNacosProvideApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootNacosProvideApplication.class, args);
    }
}

服务消费者

第一步

  • pom.xml 文件里添加对应的配置
<properties>
    <spring-cloud.version>2020.0.4</spring-cloud.version>
    <spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
    <spring-boot-dependencies.version>2.5.6</spring-boot-dependencies.version>
</properties>

<dependencies>
    <!-- SpringCloud -->
    <dependency>
        <groupId>com.alibaba.csp</groupId>
        <artifactId>sentinel-datasource-nacos</artifactId>
        <exclusions>
            <exclusion>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            </exclusion>

            <exclusion>
                <groupId>com.alibaba.nacos</groupId>
                <artifactId>nacos-client</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <!--配置中心客户端-->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        <exclusions>
            <exclusion>
                <groupId>com.alibaba.nacos</groupId>
                <artifactId>nacos-client</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

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

    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        <!-- 使用spring loadbalancer,弃用ribbon -->
        <exclusions>
            <exclusion>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.alibaba.nacos</groupId>
                <artifactId>nacos-client</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>com.alibaba.nacos</groupId>
        <artifactId>nacos-client</artifactId>
        <version>2.0.3</version>
    </dependency>

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

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

    <dependency>
        <groupId>io.github.openfeign</groupId>
        <artifactId>feign-okhttp</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.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>

第二步

  • 添加对应的配置文件
spring:
  application:
    name: springboot-nacos-consumer

  cloud:
    loadbalancer:
      retry:
        enabled: true
      enabled: true

    nacos:
      discovery:
        server-addr: 172.16.0.127:8848

  provide:
    url: springboot-nacos-provide

  main:
    allow-bean-definition-overriding: true

logging:
    level:
      com.example.springbootnacosconsumer.client: debug

第三步

  • 定义消费者接口
@FeignClient(name = "${spring.provide.url}")
@Component
public interface IProvideService {
    @PostMapping("/getProvide")
    String getProvide();
}

第四步

  • Controller 层直接调用
@RestController
public class ConsumerController {
    @Resource
    private IProvideService provideService;
    /**
     * 远程接口的方法就可以在这里使用了
     * @return
     */
    @PostMapping("/getConsumer")
    public String getConsumer() {
        return provideService.getProvide();
    }
}

第五步

  • Application 添加注解
@SpringBootApplication
// @EnableFeignClients 这个注解必须要有
@EnableFeignClients
// 这个注解在 SpringBoot 2.5.0 以上可以省略
//@EnableDiscoveryClient
public class SpringbootNacosConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootNacosConsumerApplication.class, args);
    }
}

参考

SpringCloud-Alibaba之Nacos服务注册发现与多种消费方式


文章作者: L Q
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 L Q !
  目录