Spring cloud学习之路(四,服务消费者Feign )

10 篇文章 0 订阅
10 篇文章 0 订阅

一,创建服务消费者模块

1.1 创建步骤和上一篇类似-》新建一个spring-boot子模块,取名为feign-consumer,可以在选择dependencies时勾选Spring Web,Eureka Discovery Client,OpenFeign;也可以直接走默认,然后在它的pom文件引入Feign的起步依赖spring-cloud-starter-feign、Eureka的起步依赖spring-cloud-starter-netflix-eureka-client、Web的起步依赖spring-boot-starter-web,代码如下:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zhangghq</groupId>
    <artifactId>feign_consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>feign_consumer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>com.zhanghq</groupId>
            <artifactId>common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--引入feign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

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

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

1.2 在工程的配置文件application.yml文件,指定程序名为feign-consumer,端口号为8764,服务注册地址为http://localhost:8761/eureka/ ,代码如下:

eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8764
spring:
  application:
    name: consumer-feign
feign:
  hystrix:
    enabled: true #开启断路器(熔断器) 这个注解不提示 但是不影响它的使用

1.3 在程序的启动类FeignConsumerApplication ,加上@EnableFeignClients注解开启Feign的功能:

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class FeignConsumerApplication {

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

}

1.4 定义一个feign接口,通过@ FeignClient(“服务名”),来指定调用哪个服务。比如在代码中调用了producer服务的“/hi”接口,代码如下:

@FeignClient(value = "producer")
public interface FeignBookService {

    @RequestMapping(value = "/hi",method = RequestMethod.GET) 
    String sayHiFromClientOne(@RequestParam(value = "name") String name);

}

1.5 在Web层的controller层,对外暴露一个"/hi"的API接口,通过上面定义的Feign客户端FeignBookService 来消费服务。代码如下:

@RestController
public class TestController {

     //编译器报错,无视。 因为这个Bean是在程序启动的时候注入的,编译器感知不到,所以报错。 
     @Autowired
     FeignBookService feignBookService 

    @RequestMapping("/hi")
    public String sayHi(@RequestParam String name)
    {
        return feignBookService.sayHiFromClientOne(name);
    }

}

1.6 启动程序,多次访问http://localhost:8764/hi?name=jackson 就可以了

 

上一篇:Spring cloud学习之路(三,服务提供者 )https://blog.csdn.net/Zhang_Jackson/article/details/103289720

下一篇:Spring cloud学习之路(五,断路器 Hystrix)https://blog.csdn.net/Zhang_Jackson/article/details/103293354

代码已经放在了GitHub上https://github.com/JacksonZhangHuaQuan/SpringCloudDemo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值