SpringCloud–提供者和调用者

服务提供者

<!--pom.xml-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
# bootstrap.yml
spring:
 application:
   name: aosp-cskt-teacher-service
eureka:
 client:
   serviceUrl:
     defaultZone: http://localhost:8080/eureka
// 启动类
@SpringBootApplication
@EnableDiscoveryClient
public class Application {
   public static void main(String[] args) {
       SpringApplication.run(Application.class, args);
   }
}
@RestController
@RequestMapping("/customer")
@Slf4j
public class TestController {
   /**
    * 通过ID查询
    *
    * @param ids ID
    * @return CompanyCustomer
    */
   @GetMapping("/{ids}")
   public String get(@PathVariable("ids") String ids) {
       System.out.println("进入服务提供者");
       return "ok service";
   }
}

服务消费者

配置和上面的一样

调用代码:

@RequestMapping("/index")
@RestController
public class IndexController {
@Resource
   private CustomerProviderClient client;
   @GetMapping("/test/{ids}")
   public String gets(@PathVariable("ids") String ids){
       System.out.println("ids : " + ids);
       return client.getTest(ids);
   }
}
@FeignClient(name = "aosp-cskt-teacher-service", 
fallbackFactory = CustomerProviderClientFallBackFactory.class // 调用失败执行这里
)
public interface CustomerProviderClient {
   // 调用远程服务
   @RequestMapping(value = "/customer/{ids}", method = RequestMethod.GET)
   String getTest(@PathVariable("ids") String ids);
}

发表评论