Fegin使用

2018-10-17  本文已影响0人  夜寻

Feign使用

集成

加入Eureka客户端和Feign

pom.xml

    <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-netflix-ribbon</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>

启用Eureka和Feign

/**
* @EnableDiscoveryClient 启用Eureka
* @EnableFeignClients 启用Feign
* @author HouZm
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class Client2Application {

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

配置

#向erueka注册
eureka.instance.leaseRenewalIntervalInSeconds= 30
eureka.instance.prefer-ip-address=true
eureka.client.registryFetchIntervalSeconds= 15
eureka.client.serviceUrl.defaultZone=http://10.60.110.8:8199/eureka-server/eureka/,http://10.60.110.9:8199/eureka-server/eureka/,http://10.60.110.10:8199/eureka-server/eureka/

## 配置向Eureka注册的信息
server.servlet.context-path=
eureka.instance.home-page-url-path=${server.servlet.context-path}
eureka.instance.health-check-url-path=${server.servlet.context-path}/actuator/health
eureka.instance.status-page-url-path=${server.servlet.context-path}/actuator/info
eureka.instance.metadata-map.management.context-path=${server.servlet.context-path}/actuator

server.port=8183
spring.application.name=DEV-CLIENT-2
## 启用actuator 端口,默认只公开了/health 和 /info 端点
## 公开除 env 端点之外的所有(已启用的) web 端点:
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

使用

client1 TestController

@RestController
@RequestMapping("/test")
public class TestController {

    @Resource
    private TestService testService;

    @Resource
    private FeignService feignService;

    @RequestMapping("/test01")
    public ResponseEntity test01(){
        return new ResponseEntity(feignService.test01(),HttpStatus.OK);
    }

    @RequestMapping("/test02")
    public ResponseEntity test02(){
        return new ResponseEntity(feignService.test02(),HttpStatus.OK);
    }

}

client1 FeignService

/**
* fegin 客户端 如果是集成了Eureka的话,name就是调用的Service的application name 或者说是ServieID 
* 另外 name中不能使用下划线否则会出现Service id not legal hostname
*
* @author HouZm
*/
@FeignClient(name = "DEV-CLIENT-2",path = "/test")
public interface FeignService {

    @RequestMapping(value = "/test01", method = RequestMethod.GET)
    String test01();

    @RequestMapping(value = "/test02", method = RequestMethod.GET)
    String test02();
}

clinet2 TestController

@RestController
@RequestMapping("/test")
public class TestController {

    @Resource
    private FeignService feignService;

    @RequestMapping("/test01")
    public ResponseEntity test01(){
        return new ResponseEntity(feignService.test02(),HttpStatus.OK);
    }
    @RequestMapping("/test02")
    public ResponseEntity test02(){
        return new ResponseEntity("测试",HttpStatus.OK);
    }
}

client2 FeignService

/**
* fegin 客户端 如果是集成了Eureka的话,name就是调用的Service的application name 或者说是ServieID
* 另外 name中不能使用下划线否则会出现Service id not legal hostname
*
*
* @author HouZm
*/
@FeignClient(name = "DEV-CLIENT-1",path = "/test")
public interface FeignService {

    @RequestMapping(value = "/test01", method = RequestMethod.GET)
    String test01();

    @RequestMapping(value = "/test02", method = RequestMethod.GET)
    String test02();
}

测试

请求Client1 服务的test02接口会通过Feign调用Client2 的test02接口
http://localhost:8182/test/test02
请求Client2 服务的test01接口会通过Feign调用Client2的test
01接口 而client2的test01接口同样会通过Feign调用client1的test02接口。
http://localhost:8182/test/test01
两次请的结果都是Clinet2 test02接口返回 测试 这两个字符

上一篇下一篇

猜你喜欢

热点阅读