声明式调用Feign
使用feign远程调度其他服务
pom
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.eureka</groupId> <artifactId>eureka-fegion-client</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>com.eureka</groupId> <artifactId>tr</artifactId> <version>1.0-SNAPSHOT</version> <relativePath>../</relativePath> </parent>
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> <version>1.4.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.4.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
</project>
|
application.yml
1 2 3 4 5 6 7 8 9 10 11 12
| eureka: client: service-url: defaultZone: http://localhost:8761/eureka/
spring: application: name: eureka-fegion-client
server: port: 8765
|
EurekaFegionClientApplication.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.tr;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication @EnableEurekaClient @EnableFeignClients public class EurekaFegionClientApplication { public static void main(String[] args) { SpringApplication.run(EurekaFegionClientApplication.class, args); } }
|
开启feign 下面使用
- FeignConfig.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.tr;
import feign.Retryer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;
@Configuration public class FeignConfig { @Bean public Retryer feignRetryer() { return new Retryer.Default(100, 10, 5); } }
|
- EurekaClientFeign.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.tr;
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "eureka-client",configuration = FeignConfig.class) public interface EurekaClientFeign {
@GetMapping(value = "/hello") String sayHiFromEurekaClientFeign(@RequestParam(value = "name")String name); }
|
- hi.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.tr.controller;
import com.netflix.discovery.converters.Auto; import com.tr.service.HiService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;
@RestController public class hi { @Autowired private HiService hiService;
@GetMapping("/hi") public String sayHi(@RequestParam(value = "name",required = false) String name) { return this.hiService.sayHi(name); } }
|
Welcome to my other publishing channels