UserApplication.java 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package org.zhongzheng.user;
  2. import org.mybatis.spring.annotation.MapperScan;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  7. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  8. import org.springframework.cloud.openfeign.EnableFeignClients;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.ComponentScan;
  11. import org.springframework.web.bind.annotation.GetMapping;
  12. import org.springframework.web.bind.annotation.RestController;
  13. import org.springframework.web.client.RestTemplate;
  14. @SpringBootApplication
  15. @EnableFeignClients
  16. @EnableDiscoveryClient //开启服务注册发现功能
  17. @RestController
  18. @ComponentScan(basePackages = {"org.zhongzheng.common","org.zhongzheng.user"})
  19. public class UserApplication {
  20. public static void main(String[] args) {
  21. SpringApplication.run(UserApplication.class, args);
  22. }
  23. @Autowired
  24. private RestTemplate restTemplate;
  25. /**
  26. * 注入RestTemplate调用server-provider服务;
  27. * LoadBalanced注解表示如果被调用的服务有多个节点,则默认采用轮询调用,实现负载均衡
  28. */
  29. @Bean
  30. @LoadBalanced
  31. public RestTemplate getRestTemplate(){
  32. return new RestTemplate();
  33. }
  34. @GetMapping("/consumer")
  35. public String test() {
  36. //url拼写规则: 传输协议://服务名/访问路径
  37. return restTemplate.getForObject("http://SERVER-PROVIDER/hello",String.class);
  38. }
  39. }