Spring Cloud(3) Feign - Feign調(diào)用

目標(biāo)

  • 增加wallet模塊,提供接口 /getWallet,并注冊(cè)到eureka-server
  • account模塊增加接口 /userInfo,通過(guò)調(diào)用wallet接口 /getWallet 獲取數(shù)據(jù)后返回

增加wallet模塊

引入依賴庫(kù)

<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

# 端口號(hào)
# 端口號(hào)
server.port=8001

# 服務(wù)名
spring.application.name=wallet

# eureka服務(wù)注冊(cè)中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:5000/eureka/

啟動(dòng)類 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";
    }

}

啟動(dòng)服務(wù)

訪問(wèn) http://localhost:8001/

image.png

圖中可以看出,wallet接口可以正常訪問(wèn)

修改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);
        }};
    }

}

測(cè)試

訪問(wèn) http://localhost:5000/

image.png

account、wallet已成功注冊(cè)到eureka-server中

訪問(wèn) http://localhost:8000/userInfo

image.png

account調(diào)用wallet成功

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容