美文网首页
Spring Cloud(3) Feign - Feign调用

Spring Cloud(3) Feign - Feign调用

作者: 挨板砖的码农 | 来源:发表于2018-01-31 15:18 被阅读0次

目标

  • 增加wallet模块,提供接口 /getWallet,并注册到eureka-server
  • account模块增加接口 /userInfo,通过调用wallet接口 /getWallet 获取数据后返回

增加wallet模块

引入依赖库

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Edgware.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

配置文件 application.properties

# 端口号
# 端口号
server.port=8001

# 服务名
spring.application.name=wallet

# eureka服务注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:5000/eureka/

启动类 MainApplication

@EnableEurekaClient
@SpringBootApplication
@RestController
@RequestMapping
public class MainApplication {

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


    @GetMapping("getWallet")
    public String getWallet() {
        return "this is wallet info";
    }

}

启动服务

访问 http://localhost:8001/

image.png

图中可以看出,wallet接口可以正常访问

修改account

增加Feign依赖

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>

增加Feign支持与userInfo接口

@EnableEurekaClient
@SpringBootApplication
@RestController
@RequestMapping
@EnableFeignClients
public class MainApplication {

    @Autowired
    private WalletRemoteService walletRemoteService;

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

    @GetMapping("userInfo")
    public Map userInfo() {
        String walletInfo = walletRemoteService.getWallet();
        return new HashMap() {{
            put("walletInfo", walletInfo);
        }};
    }

}

测试

访问 http://localhost:5000/

image.png

account、wallet已成功注册到eureka-server中

访问 http://localhost:8000/userInfo

image.png

account调用wallet成功

相关文章

网友评论

      本文标题:Spring Cloud(3) Feign - Feign调用

      本文链接:https://www.haomeiwen.com/subject/cyzkzxtx.html