eureka 消费服务
1:pom
<!--- eureka-client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
2:yml
eureka: client: fetch-registry: true register-with-eureka: true service-url: defaultZone: http://localhost:7001/eureka
3:注解
@EnableEurekaClient @SpringBootApplication public class OrderMain80 { public static void main(String[] args) { SpringApplication.run(OrderMain80.class); } }
端口分配 80
4使用eureka服务中的名称调用
服务的名称:
spring:
application:
name: cloud-payment-service
8001,8002端口
客户端调用
@Autowired private RestTemplate restTemplate; private String url = "http://CLOUD-PAYMENT-SERVICE"; //写到配置文件中 @RequestMapping("/insert") public BaseResult insert(Payment payment) { restTemplate.postForEntity("url" + "/payment/insert", payment, BaseResult.class); return sendSuccess(); }
备注:注册bean,开启负载
@Component @Configuration public class AppConfiguration { @LoadBalanced // 负载均衡 @Bean public RestTemplate restTemplate() { return new RestTemplate(); } }