SpringCloud-OpenFeign(三)

目录

简介

Feign与OpenFeign

方式 一(工作中不怎么使用)

创建OpenFeign服务

pom文件

配置文件

AClient接口

OpenFeignDemoController

启动类

结果

方式二(工作中常用) 

创建OpenFeign服务

pom文件

配置文件

APlushClient接口

OpenFeignDemoController

启动类

api服务

Hello接口

Eureka-App-a服务

pom文件

配置文件

HelloController

启动类

结果


简介

在上篇中我们通过restTemplate跨进程调用接口,但是在实际工作中我们并不使用它来跨进程调用。在服务eureka-app-b中通过 

String ret = restTemplate.getForObject("http://a/echo?name=123", String.class);

调用,显然代码并不优雅,由此更优雅的跨进程调用方式就出现了。

Feign与OpenFeign

Feign是一个开源声明式WebService客户端,用于简化服务通信;Feign采用"接口+注解"方式开发,屏蔽了网络通信的细节;

OpenFeign是SpringCloud对Feign的增强,支持SpringMVC注解;

方式 一(工作中不怎么使用)

创建OpenFeign服务

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>
    <parent>
        <groupId>com.chensir</groupId>
        <artifactId>spring-cloud-root</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <artifactId>spring-cloud-openfeign-demo</artifactId>


    <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>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>

        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

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


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

配置文件

server.port = 9092
spring.application.name = opfei
eureka.client.fetch-registry=true
eureka.instance.ip-address = 172.17.204.119
eureka.instance.instance-id = ${eureka.instance.ip-address}:${server.port}
eureka.instance.hostname =  ${eureka.instance.ip-address}

eureka.client.service-url.defaultZone=http://ek1.cn:8001/eureka/,http://ek2.cn:8002/eureka/



AClient接口

//此注解类似 @service、@mapper 调用a服务
@FeignClient("a")
public interface AClient {

    @GetMapping("/echo")
    String  hello(@RequestParam String  name);


}

OpenFeignDemoController

@RestController
public class OpenFeignDemoController {

    @Autowired
    private AClient aClient;


    @GetMapping("/test")
    public  String  test() {
        String ret = aClient.hello("辣目洋子");

        return ret;
    }
}

 此示例调用的仍是a服务中/echo接口

启动类

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class App {

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

}

结果

方式二(工作中常用) 

在工作中api服务就是事先定义好的各个接口,a服务就是api接口的实现类(b服务、c服务均可去实现api中定义的接口)。而此OpenFeignDemo就是调用方,去调用api服务中的接口 并且在调用时可以指定调用哪个实现类服务。

创建OpenFeign服务

 

pom文件

比上方示例多个api包

<?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>com.chensir</groupId>
        <artifactId>spring-cloud-root</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <artifactId>spring-cloud-openfeign-demo</artifactId>


    <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>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>

        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.chensir</groupId>
            <artifactId>spring-cloud-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>


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

配置文件

内容没变


server.port = 9092
spring.application.name = opfei
eureka.client.fetch-registry=true
eureka.instance.ip-address = 172.17.204.119
eureka.instance.instance-id = ${eureka.instance.ip-address}:${server.port}
eureka.instance.hostname =  ${eureka.instance.ip-address}

eureka.client.service-url.defaultZone=http://ek1.cn:8001/eureka/,http://ek2.cn:8002/eureka/



APlushClient接口

@FeignClient("a")
public interface APlusClient extends Hello {
}

OpenFeignDemoController

@RestController
public class OpenFeignDemoController {

    @Autowired
    private APlusClient aPlusClient;

    @GetMapping("/test")
    public  String  test() {
        String ret = aPlusClient.userJDdou(100);

        return ret;
    }
}

启动类

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class App {

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

}

api服务

Hello接口

public interface Hello {

    @GetMapping("/hello")
    String  hello();


    @GetMapping("/jingdou/{userId}")
    String  userJDdou(@PathVariable Integer userId);

}

就这些,没其他的。

Eureka-App-a服务

pom文件

就多个api包

<?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>com.chensir</groupId>
        <artifactId>spring-cloud-root</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <artifactId>eureka-app-a</artifactId>

    <name>eureka-app-a</name>
    <description>eureka-app-a</description>

    <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.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</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>com.chensir</groupId>
            <artifactId>spring-cloud-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>


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

配置文件

没变

HelloController

@RestController
public class HelloController implements Hello {
    @Override
    public String hello() {
        return "这是通过外部接口定义的Controller实现方法";
    }

    @Override
    public String userJDdou(Integer userId) {
        return StrUtil.format("用户{}有250个京豆",userId) ;
    }
}

启动类

@SpringBootApplication
public class App {

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

}

结果

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值