Feign是一個聲明式的Web服務(wù)客戶端妓美。它使Web服務(wù)客戶端更容易實(shí)現(xiàn)。聲明一個接口并且加入注解來使用Feign玄帕。它具有可插拔的注解特性部脚,可使用Feign 注解和JAX-RS注解。Feign也支持可插拔的編碼器和解碼器裤纹。使用Feign時,Spring Cloud 集成了Ribbon和Eureka來提供一個附在均衡客戶端丧没。
1.建路由選擇Feign
2.pom文件加入配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
3.配置文件
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/
server:
port: 8008
spring:
application:
name: service-feign
4.修改啟動application中加上
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServicefeignApplication {
public static void main(String[] args) {
SpringApplication.run(ServicefeignApplication.class, args);
}
}
加入interface
@FeignClient(value = "service-yuan")
public interface SchedualServiceHi {
@RequestMapping(value = "/hi", method = RequestMethod.GET)
String sayHiFromClientOne(@RequestParam(value = "name") String name);
}
加入controller并注入接口
@RestController
public class HiController {
@Autowired
SchedualServiceHi schedualServiceHi;
@RequestMapping(value = "/hi",method = RequestMethod.GET)
public String sayHi(@RequestParam String name){
return schedualServiceHi.sayHiFromClientOne(name);
}
}
5.啟動工程鹰椒,并查看Eureka
6.訪問 http://localhost:8008/hi?name=yuan
可以看到實(shí)現(xiàn)了負(fù)載均衡效果