springcloud中使用openfeign进行通信

介绍

 openfeign解决了在微服务架构下服务之间提供了解决方案。通过openfeign可以在服务消费端以接口的形式调用其它服务。

如下图:

image.png

创建服务提供端

  本片文章是在我前面几篇几篇文章示例代码基础上进行编码。文章末尾会提供仓库地址。

创建demo-open-feign-provider服务

这里创建的是一个maven项目

image.png

修该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>springcloud-demo</artifactId>
        <groupId>com.springboot3</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>demo-open-feign-provider</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </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.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

创建启动类

image.png

代码如下:

package com.springboot3.openfeign;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
public class ApplicationOpenFeignProvider {

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

修改application.yml配置文件

 这里以Eureka作为服务注册中心,下示例代码中会有EurekaServer服务,你也可以浏览我前面的文章创建一个注册中心服务。

 这里会将该服务注册进Eureka服务中心
application.yml代码如下:

server:
  port: 8001

spring:
  application:
    name: demo-open-feign-provide # 服务的名称

# 配置Eureka
eureka:
  client:
    # 表示是否将自己注册到EurekaService
    register-with-eureka: true
    # 是否从EurekaService抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合Ribbon使用负责均衡
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka # 注册到集训EurekaServer,如果是在真实环境中,应配置正确的EurekaServer所在服务器的域名
  instance:
    instance-id: demo-open-feign-provide  # 修改服务的主机名称
    prefer-ip-address: true # 访问路径可以显示ip
    lease-renewal-interval-in-seconds: 5 # 客户端向服务端发送心跳间隔时间,默认30s
    lease-expiration-duration-in-seconds: 90 # 客户端在最后一次收到心跳后的等待时间,默认90s,超时会剔除

注意:

defaultZone: http://localhost:7001/eureka

这里是需要填写Eureka注册中心的地址

创建一个InfoController类

 这里简单的写个接口,当通过/info路径访问时返回该服务的端口

image.png

代码如下

package com.springboot3.openfeign.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class InfoController {

    @Value("${server.port}")
    private String port;

    @GetMapping("/info")
    public String getInfo(){
        return "This is "+port;
    }
}

启动

  • 启动Eureka服务注册中心
  • 启动demo-open-feign-provide服务

现在两个服务都启动完毕

image.png

访问Eureka服务注册中心,查看demo-open-feign-provide是否注册

image.png

测试

 通过访问demo-open-feign-provide服务中的接口观察是否正常

http://localhost:8001/info

image.png

 现在我们的服务提供者已一切正常,并且注册进了Eureka服务注册中心。下面创建服务消费者,通过openfeign已接口的形式调用demo-open-feign-provide提供的服务

创建服务消费者

创建demo-open-feign-consumer服务

同样创建的是一个mavne项目

image.png

修改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>springcloud-demo</artifactId>
        <groupId>com.springboot3</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>demo-open-feign-consumer</artifactId>


    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </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.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

创建启动类

image.png

代码如下

package com.springboot3.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class ApplicationOpenFeignConsumer {

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

修改application.yml文件

server:
  port: 80

spring:
  application:
    name: demo-open-feign-consumer

# 配置Eureka
eureka:
  client:
    # 表示是否将自己注册到EurekaService
    register-with-eureka: false
    # 是否从EurekaService抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合Ribbon使用负责均衡
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka # 注册到集训EurekaServer,如果是在真实环境中,应配置正确的EurekaServer所在服务器的域名

logging:
  level:
    # Feign日志以什么级别监控接口
    com.springboot3.springcloud.service.PaymentFeignService: debug


创建DemoOpenFeignProviderService

image.png

代码如下:

package com.springboot3.consumer.server;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;

@Component
@FeignClient(value = "DEMO-OPEN-FEIGN-PROVIDE")
public interface DemoOpenFeignProviderService {

    @GetMapping("/info")
    String getInfo();
}

  • @FeignClient(value = "DEMO-OPEN-FEIGN-PROVIDE"):在Eureka注册中心显示的服务名称
  • 这就时已接口的形式去调用服务

创建DemoController

image.png

代码如下:

package com.springboot3.consumer.controller;

import com.springboot3.consumer.server.DemoOpenFeignProviderService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

    @Autowired
    DemoOpenFeignProviderService demoOpenFeignProviderService;

    @GetMapping("/server/info")
    public String demo(){
       return demoOpenFeignProviderService.getInfo();
    }
}

启动服务

  • 启动Eureka服务注册中心
  • 启动demo-open-feign-provide服务
  • 启动demo-open-feign-consumer服务

这里你会发现在Eureka注册中心面板中没有demo-open-feign-consumer服务,这是因为我在配置其application.yml文件时

 # 表示是否将自己注册到EurekaService
    register-with-eureka: false

测试

通过80端口访问

/server/info

image.png

如图显示,成功的通过80端口调用了8001的服务

示例代码

https://gitee.com/cwljxf/springcloud-demo

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×