依赖,因为要书写mvc和feign注解
org.springframework.boot spring-boot-starter-weborg.springframework.cloud spring-cloud-starter-feign1.4.7.RELEASE
添加一个接口,封装了生产者的对应路由和传参
@FeignClient("provider") public interface ProviderA { @RequestMapping("/hello") public Student fun1(@RequestParam("count") Integer count); }改造生产者(可选)
让生产者继承这个接口
@RestController public class HelloController implements ProviderA { @Override @RequestMapping("/hello") public Student fun1(Integer count) { System.out.println(count); return new Student("张三", 23); } }改造消费者
依赖
org.springframework.cloud spring-cloud-starter-feign1.4.7.RELEASE
启动类,启用Feign扫描,指向api包
@SpringBootApplication @EnableEurekaClient @EnableFeignClients(basePackages = {"org.example.api.controller"}) public class App { public static void main(String[] args) { SpringApplication.run(App.class); } @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } }
控制器,用Feign替换RestTemplate
@RestController public class HelloController { @Autowired private ProviderA providerA; private static Integer count = 1; @RequestMapping("/") public Student fun1() { return providerA.fun1(count++); } }测试
全部启动,打开URL:http://localhost:9101/
成功!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)