public interface MyService { String sayHello(String name); } @Service public class MyServiceImpl implements MyService { @Override public String sayHello(String name) { return "Hello, " + name + "!"; } } @Configuration @EnableAutoConfiguration public class MyServiceProvider { public static void main(String[] args) { SpringApplication.run(MyServiceProvider.class, args); } } server.port=8080 spring.application.name=MyServiceProvider @Configuration public class MyServiceConsumerConfig { @Bean public MyService myService(RestTemplate restTemplate) { return restTemplate.getForObject("http://localhost:8080/myService", MyService.class); } @Bean public RestTemplate restTemplate() { return new RestTemplate(); } } @Service public class MyServiceConsumer { private final MyService myService; public MyServiceConsumer(MyService myService) { this.myService = myService; } public String sayHello(String name) { return myService.sayHello(name); } } @SpringBootApplication @Import(MyServiceConsumerConfig.class) public class MyServiceConsumerApp { public static void main(String[] args) { SpringApplication.run(MyServiceConsumerApp.class, args); } } spring.application.name=MyServiceConsumer


上一篇:
下一篇:
切换中文