1、给需要远程调用的微服务上加maven加依赖(被调用的微服务不需要)
2、在需要远程调用的启动类上加@EnableFeignClients(basePackages = “com.xxx.xxx.service”)(被调用的微服务不需要)
3、写service
package com.my.server1.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
@Service
@FeignClient("server2")
public interface TestService {
@RequestMapping("/s2/get")
public String get();
}
4、调用
package com.my.server1.controller;
import com.my.server1.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("s1")
public class CustomerController {
@Autowired
private TestService testService;
@RequestMapping("get")
public String get(){
String rs = testService.get();
return "I am server2"+rs;
}
}
5、结果