• 0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例


      这里的feign依然是原来的feign,只不过将注册中心由eureka换成了nacos。服务提供方参见0.9.0.RELEASE版本的spring cloud alibaba nacos实例,消费方跟提供方一样,只需加入feign的相关内容即可。抡出三板斧:

      1、pom加入feign:

    <?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.wlf</groupId>
        <artifactId>spring-cloud-alibaba-consumer</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.7.RELEASE</version>
        </parent>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Greenwich.SR2</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                    <version>0.9.0.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <dependencies>
            <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>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>

      2、application同服务提供方,只需注册到nacos即可:

    #端口
    server.port=8383
    #应用名
    spring.application.name=lxytrans-consumer
    #注册中心
    spring.cloud.nacos.discovery.server-addr=localhost:8848

      3、启动类中加入注解@EnableFeignClients,引入服务提供方接口,并通过注解指向服务提供方服务实例@FeignClient("lxytrans-provider"):

    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @EnableFeignClients
    @EnableDiscoveryClient
    @SpringBootApplication
    public class TransConsumerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(TransConsumerApplication.class, args);
        }
    
        @Slf4j
        @RestController
        static class TestController {
    
            @Autowired
            private ApplicationApi applicationApi;
    
            @GetMapping("/sayhello")
            public String sayhello() {
                return "say: " + applicationApi.hello();
            }
    
        }
    
        @FeignClient("lxytrans-provider")
        interface ApplicationApi {
    
            @GetMapping("/hello")
            String hello();
    
        }
    
    }

      消费者跑起来后可在nacos中看到:

      通过消费方调用提供方:

  • 相关阅读:
    经常让程序员恼火的一些事情你是否也遇到过一些?
    CRC文件解压缩问题
    你在淘宝买件东西背后的复杂技术 技术普及帖
    程序员需要戒骄戒躁
    IT路上的应该注意自我规划 学习规划与自我修炼
    腾讯,我最恨别人用枪顶着我的头(转)
    软件管理,软件生命周期,软件过程名词解释
    程序员需要掌握的最终技术是什么? “终极技术”:应对困境的方法和信念
    程序员如何缓解“电脑病”
    新浪微博XSS攻击事件
  • 原文地址:https://www.cnblogs.com/wuxun1997/p/11392772.html
Copyright © 2020-2023  润新知