栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Nacos(三):Nacos集成OpenFegin

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Nacos(三):Nacos集成OpenFegin

Nacos集成OpenFegin
  • 前言
  • 创建项目
    • pom.xml
    • appliaction.properties
    • 启动类
    • Cotrolller 接口类
  • 启动测试
  • 总结& 使用注意事项

前言

上一章Spring Cloud项目中Nacos作为注册中心 在使用Naocs作为注册中心的时候我们使用 RestTemplate+Ribbo 的方式访问到了procider发布的服务, 但是在实际开发中不可能使用这种方式进行服务调用,所以这章我们讲一下:使用 Naocs集成OpenFegin 来完成调用服务发布的接口。

OpenFegin 自身整合了Ribbon合Hystrix, 为服务调用提供了更优雅的方式
Ribbon:负载均衡
Hystrix: 断路器,Hystrix能够保证在一个依赖出现问题的情况下,不会导致整体服务失败,避免级联故障,以提高分布式系统的弹性。

上一章 创建了 服务发布者 provider 服务消费者 consumer
这一章 我们重新创建一个新的 集成openFegin的消费者

  • open-fegin

demo项目git地址: https://gitee.com/DianHaiShiYuDeMing/nacos-demo

创建项目 pom.xml


    
        com.example
        nacos-demo
        1.0.0
    
    4.0.0

    open-fegin
    com.example.consumer
    0.0.1-SNAPSHOT
    open-fegin
    Demo project for Spring Boot

    
        1.8
    
    
     
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
            2.2.5.RELEASE
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


appliaction.properties
server.port=8030

###nacos
spring.application.name=open-fegin
spring.cloud.nacos.discovery.namespace=evone
spring.cloud.nacos.username=evone
spring.cloud.nacos.password=evone123456
spring.cloud.nacos.server-addr=127.0.0.1:16848
启动类
package com.example.fegin;

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

@SpringBootApplication
@EnableDiscoveryClient// 开启nacos 服务发现
@EnableFeignClients(basePackages = "com.example.fegin")
public class OpenFeginApplication {

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

}

Cotrolller 接口类
package com.example.fegin.controller;

import com.example.fegin.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FeginContoller {

    @Autowired
    private DemoService demoService;

    @GetMapping("/test/{id}")
    public Object test(@PathVariable String id) {

        String forObject = demoService.test(id);

        return forObject;
    }
}
启动测试

总结& 使用注意事项

Spring Cloud Openfegin 自动集成了 Ribbon 与 Hystrix;
在pom坐标使用过程中 OpenFegin 要使用与 nacos-discovery 的版本不一样的话 可能造成依赖jar包坐标版本冲突,导致依赖jar 下不下来 出现各种各样的问题 例如

java.lang.ClassNotFoundException: com.netflix.config.CachedDynamicIntProperty

需要单独引入引入


    com.netflix.archaius
    archaius-core
    0.7.6
    
        
            com.google.guava
            guava
        
    

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/678749.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号