FeignClient+Nacos(单机测试及源码),微服务1调用微服务2的URL

问题背景

最近在做微服务 URL 调用,除了使用 okhttp 依赖以外,发现还可以使用一种常用的方式,使用spring-cloud-starter-openfeign依赖,可以根据本文的代码自己创建工程,也可以直接下载源码
注意事项:

startup.cmd -m standalone

或者打开脚本,直接更改模式,双击启动脚本即可

登录注册中心http://localhost:8848/nacos,默认账号和密码都为:nacos

  • 必须要有注册中心,可以用Eurake和Nacos,本文使用的Nacos
  • 版本号必须兼容,不然会报很多找不到类的错误,这个坑太大了,我搞了好久,springboot和springcloud的兼容性相当不好

代码步骤

1 service1微服务

1.1 先创建module工程,引入父类依赖,版本都是经过我的调试,可以编译通过

<?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>
    <groupId>com.yg</groupId>
    <artifactId>sleuthTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <modules>
        <module>service1</module>
        <module>service2</module>
    </modules>

    <name>sleuthTest</name>
    <description>sleuthTest</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </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-openfeign</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<!--            <version>2.1.0.RELEASE</version>-->
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <!--Spring Cloud-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

1.2 创建service1微服务,引入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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.yg</groupId>
    <artifactId>service1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>com.yg</groupId>
        <artifactId>sleuthTest</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <name>service1</name>
    <description>service1</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>

    </dependencies>
</project>

1.3 在service1项目的启动类中添加注解@EnableFeignClients、@EnableDiscoveryClient

package com.yg.service1;

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

@EnableFeignClients
@SpringBootApplication
@EnableDiscoveryClient
public class Service1Application {

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

}

1.4 在service1项目中创建调用的外部服务接口类

package com.yg.service1.controller;

import com.yg.service1.fallback.Service2ControllerFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(value = "service2",fallbackFactory = Service2ControllerFallbackFactory.class)
public interface Service2Controller {

    @GetMapping("/test2")
    String getTest2();
}

1.5 实现FallbackFactory

package com.yg.service1.fallback;

import com.yg.service1.controller.Service2Controller;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;

@Component
public class Service2ControllerFallbackFactory implements FallbackFactory<Service2Controller> {
    @Override
    public Service2Controller create(Throwable throwable) {
        return null;
    }
}

1.6 创建controller类,编写调用service2的方法

package com.yg.service1.controller;

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

@RestController
public class Service1Controller {

    @Autowired
    private Service2Controller service2Controller;

    @GetMapping("/test")
    public String test(){
        System.out.println("Test success");
        return "I am YuanGe";
    }

   @GetMapping("/test1")
   public String test1(){
       System.out.println("test11111");
       System.out.println(service2Controller.getTest2());
       return "I am test11111";
   }
}

1.7 更改配置文件,添加到注册中心,更改服务名,这样才能在注册中心找到

server:
  port: 3901

spring:
  application:
    name: service1
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

2 service2微服务

2.1 创建service2微服务,引入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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.yg</groupId>
    <artifactId>service2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>com.yg</groupId>
        <artifactId>sleuthTest</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <name>service2</name>
    <description>service2</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>

    </dependencies>
</project>

2.2 service2项目启动类添加注解

package com.yg.service2;

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

@SpringBootApplication
@EnableDiscoveryClient
public class Service2Application {

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

}

2.3 创建controller控制类,API和service1对应

package com.yg.service2.controller;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Service2Controller {

    @GetMapping("/test")
    public String test(){
        System.out.println("Received");
        return "I am YuanGe";
    }

    @GetMapping("/test2")
    public String test2(){
        System.out.println("test22222");
        return "I am test22222";
    }
}

2.4 更改配置文件,更改服务名,这样才能在注册中心找到

server:
  port: 3902

spring:
  application:
    name: service2
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

2.5 整体项目文件目录

测试流程

1 启动service1微服务

2 启动service2微服务

3 登录注册中心http://localhost:8848/nacos,输入账号 nacos,输入密码 nacos

4 打开空白网页调用service1微服务接口,然后service1再调用service2


心得

  • 版本兼容性问题,真的搞了我两天,非常蓝瘦,下次有时间要研究一下版本兼容的对策了




作为程序员第 16 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …

Lyric: 初恋的颜色

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐