4.Feign

2017-12-27  本文已影响0人  元代码

Feign是一个声明式的Web服务客户端。它使Web服务客户端更容易实现。声明一个接口并且加入注解来使用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

可以看到实现了负载均衡效果

上一篇 下一篇

猜你喜欢

热点阅读