Spring Cloud(3) Feign - Feign调用

目标

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

增加wallet模块

引入依赖库


        org.springframework.boot
        spring-boot-starter-parent
        1.5.10.RELEASE
        
    

    
        UTF-8
        UTF-8
        1.8
        Edgware.SR1
    

    
        
            org.springframework.cloud
            spring-cloud-starter-eureka
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

配置文件 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依赖


            org.springframework.cloud
            spring-cloud-starter-feign
        

增加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/

Spring Cloud(3) Feign - Feign调用_第1张图片
image.png

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

访问 http://localhost:8000/userInfo

image.png

account调用wallet成功

你可能感兴趣的:(Spring Cloud(3) Feign - Feign调用)