服务发现(Feign)

  1. 【服务中心】集群,Eureka Server

  2. 【服务提供者】集群,Eureka Client

  3. 【服务消费者】,Eureka Discovery Client(Feign)
    3.1 pom文件:

     
         org.springframework.boot
         spring-boot-starter-web
     
     
         org.springframework.cloud
         spring-cloud-starter-openfeign
     
     
         org.springframework.cloud
         spring-cloud-starter-netflix-eureka-client
     
    

    3.2 启动类&&Service层&&控制层

    启动类:

     @SpringBootApplication
     @EnableFeignClients         //启动Feign
     @EnableDiscoveryClient      //启动Eureka服务发现
     public class EurekaFeignApplication {
         public static void main(String[] args) {
             SpringApplication.run(EurekaFeignApplication.class, args);
         }
     }
    

    Service层:

     @FeignClient(value = "eureka-client")   //调用的服务名称
     public interface EurekaFeignService {
         @RequestMapping("/info")
         String getInfo();
     }
    

    控制层:

     @RestController
     public class EurekaFeignController {
         @Resource
         private EurekaFeignService eurekaFeignService;
         @RequestMapping("/feignInfo")
         public String feignInfo() {
             String message = eurekaFeignService.getInfo();
             return "获取的信息:  " + message;
         }
     }
    

    3.3 配置文件yml

     server:
       port: 52620
     spring:
       application:
         name: eureka-discovery-feign
     eureka:
       instance:
         hostname: localhost
         lease-renewal-interval-in-seconds: 5
         lease-expiration-duration-in-seconds: 10
       client:
         service-url:
           defaultZone:  http://localhost:8081/eureka,http://localhost:8082/eureka,http://localhost:803/eureka
    
  4. 启动【服务消费者】工程

  5. 访问服务中心,查看注册实例

  6. 访问地址http://localhost:52620/feignInfo,多刷新几次,查看变化
    【服务消费者】完成!!!

你可能感兴趣的:(服务发现(Feign))