serice-feign
01-eureka-server
01-serice-feign
Spring Cloud Feign是基于Netflix feign实现,整合了Spring Cloud Ribbon和Spring Cloud Hystrix
pom.xml
<parent>
<groupId>com.sky</groupId>
<artifactId>chapter1</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>chapter3-serice-feign</artifactId>
<dependencies>
<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-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
application.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8765
spring:
application:
name: service-feign
feign.hystrix.enabled: true
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
@RestController
public class ServiceFeignApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceFeignApplication.class, args);
}
@Autowired
ServiceHi serviceHi;
@GetMapping(value = "/hi")
public String sayHi(@RequestParam String name) {
return serviceHi.sayHiFromClientOne(name);
}
}
@FeignClient(value = "service-hi", fallback = ServiceHiHystric.class)
public interface ServiceHi {
@RequestMapping(value = "/hi", method = RequestMethod.GET)
String sayHiFromClientOne(@RequestParam(value = "name") String name);
}
@Component
public class ServiceHiHystric implements ServiceHi {
@Override
public String sayHiFromClientOne(String name) {
return "hi," + name + ",sorry,error!";
}
}
测试1
先启动chapter1-eureka-server
再启动chapter1-service-hi(端口8762)修改端口后再启动一次(8763)
再启动serice-feign(8765)
访问:http://localhost:8761/
看看eureka的服务注册情况
访问:http://ip:8765/hi?name=hello
交替访问8762和8763端口
测试2
在测试1的基础上停掉8762端口和8763端口