springcloud项目搭建(二):feign组件使用

搭建服务提供者

1. 在pom.xml文件中导入依赖

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zkane</groupId>
    <artifactId>producer-server</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <name>producer-server</name>
    <description>Spring Cloud Producer Server</description>

    <parent>
        <groupId>com.zkane</groupId>
        <artifactId>spring-cloud-demo</artifactId>
        <version>1.0.0</version>
        <relativePath/>
    </parent>

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

</project>

2. 在application.yml配置文件中配置

server:
  port: 8100
spring:
  application:
    name: producer-server
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8000/eureka/

3. 在启动类上添加注解:@EnableDiscoveryClient

@EnableDiscoveryClient
@SpringBootApplication
public class ProducerServerApplication {

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

4. 编写controller代码,提供restful风格的API访问

@RestController
public class ProducerController {

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

    @GetMapping("/")
    public String get() {
        return "Producer Server port: " + port;
    }

}

5.ribbon负载均衡

5.1.修改端口为8101进行启动

idea启动多个服务需要设置run/debug configurations,去掉勾选Single instance only

搭建服务消费者

1. 编写pom.xml文件

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zkane</groupId>
    <artifactId>consumer-server</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <name>consumer-server</name>
    <description>Spring Cloud Consumer Server</description>

    <parent>
        <groupId>com.zkane</groupId>
        <artifactId>spring-cloud-demo</artifactId>
        <version>1.0.0</version>
        <relativePath/>
    </parent>

    <dependencies>
        <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>

        <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>

</project>

2. 编写application.yml配置文件

server:
  port: 8200
spring:
  application:
    name: consumer-server
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8000/eureka/

3. 在启动类上添加注解

  • @EnableFeignClients注解是使用feign来调用服务提供者的API接口
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class ConsumerServerApplication {

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

4. 使用feign调用服务提供者的API接口

@RestController
public class ConsumerController {

    @Autowired
    private ProducerRemote producerRemote;

    @GetMapping("/")
    public String get() {
        return "Consumer: " + producerRemote.get();
    }

}

5. 使用hystrix实现服务熔断机制

  • 引入依赖
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
  • 需要在application.yml中配置feign启用hystrix
feign:
  hystrix:
    enabled: true
  • 在启动类添加启动hystrix的注解
@EnableHystrix
public class ConsumerServerApplication{

    public static void main(String[] args) {
        SpringApplication.run(ConsumerServerApplication.class, args);
    }
}
  • 在@FeignClient注解上添加fallback属性,指定熔断后调用的本地方法
@Component
@FeignClient(value = "producer-server", fallback = ProducerRemoteHystrix.class)
public interface ProducerRemote {
    @GetMapping("/")
    String get();

}
  • 熔断后调用的本地方法
@Component
public class ProducerRemoteHystrix implements ProducerRemote {
    @Override
    public String get() {
        return "Producer Server 的服务调用失败";
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值