OpenFeign 4.0.1+SpringCloud3.0.x+Consul集群

介绍OpenFeign

openFeign 是工作在客户端 可与其他注册发现管理服务器整合(eureka,zookeeper,consul,nacos等)功能上替代了restTemplate,本身集成Ribbon有负载均衡功能,在形式上比restTemplate更简单,风格上更好的面向接口编程。

在此重点记录openFeign在客户端的配置和编码,至于consul 集群配置参考之前的文章,

集群环境:

client:192.168.1.133

server0--server3: 192.168.1.136--138

服务微应用

provider服务:port:8021  和 port :8022

完成consul 接入 client (192.168.1.133)

order Consumer: 91

微服务目录结构

 

pom

<?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>springcloud19</artifactId>
        <groupId>com.hztech</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>consumer-openfeign-order80</artifactId>

    <properties>
        <maven.compiler.source>19</maven.compiler.source>
        <maven.compiler.target>19</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>


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


        <!-- consul -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

        <!-- 引入公用模块-->
        <dependency>
            <groupId>com.hztech</groupId>
            <artifactId>common-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <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>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>



        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

 

 

yaml

server:
  port: 91

spring:
  application:
    name: order-consul-payment

  cloud:
    consul:
      port: 8500
      host: 192.168.1.133 #machine136
      discovery:
        health-check-timeout: 10s #检测超时时间
        health-check-critical-timeout: 30s #设置超时30秒之后,注销服务,这里数字要大于心跳检测的数字
        prefer-ip-address: true
        instance-group: default #设置实例所在组
        heartbeat:
          enabled: true
          ttl: 10 #这里开启心跳检测,设置10s提交一次心跳,用于consul与服务不在同一个网段的情况,支持存活检测,由于是启动10秒之后提交心跳,同时我这里consul和服务不在同一个网段,所以在项目启动成功之后,在consul上面需要等待十秒之后才能看到检测成功
        register: true #是否在consul 集群中注册自己

 

接口Service

这是重点:

@FeignClient(value="provider-consul-payment")
1、完成了根据微服务名称 --》 consul 客户端--》consul 服务集群--》返回provider服务的真实的ip 和 port-->请求为服务--》得到结果
2、public interface PymentOrderService 是接口声明,方法的功能 与 8021 provider 的 服务controller 中的方法对应,
@FeignClient(value="provider-consul-payment")
@Service
public interface PymentOrderService {


    @PostMapping("/payment/create")
    public CommonResult<Payment> create(Payment payment);
    @GetMapping("/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable( "id")  Long id);

}

 

Controller

 

@RestController
@RequestMapping("/feiconsumer")
public class OrderOpenFeignController {

    @Autowired
    private PymentOrderService pymentOrderService;

    @PostMapping("/payment/create")
    public CommonResult<Payment> create(Payment payment)
    {
        return pymentOrderService.create(payment);

    }

    @GetMapping("/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable( "id")  Long id)
    {
        return pymentOrderService.getPaymentById(id);

    }


}

 

Main方法 

@EnableFeignClients 开启openFeign
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableFeignClients
public class OrderOpenFeignConsulMain80 {

    public static void main(String[] args) {

        SpringApplication.run( OrderOpenFeignConsulMain80.class, args);

    }

}

 

 

启动服务

8021,8022,91

 

 

 

 

测试效果

 

posted on 2023-02-15 11:19  hztech  阅读(154)  评论(0编辑  收藏  举报

导航