SpringCloud系列使用Eureka进行服务治理 (3)

eureka也引用SpringBoot actuator监控管理,SpringBoot actuator可以参考我之前博客: SpringBoot系列之actuator监控管理极速入门与实践

在这里插入图片描述


具体可以进行配置,配置成eureka server的ip,加上actuator

eureka: instance: status-page-url-path: :8761/actuator/info health-check-url-path: :8761/actuator/health

显示ip和实例id配置:

在这里插入图片描述


在这里插入图片描述

6. Eureka服务消费者 server: port: 8082 spring: application: name: eureka-service-consumer eureka: client: service-url: defaultZone: :8761/eureka/ fetch-registry: true register-with-eureka: false healthcheck: enabled: false instance: status-page-url-path: :8761/actuator/info health-check-url-path: :8761/actuator//health prefer-ip-address: true instance-id: eureka-service-consumer8082

在这里插入图片描述


关键点,使用SpringCloud的@LoadBalanced,才能调? 接口的数据,浏览器是不能直接调的

package com.example.springcloud.consumer; import com.example.springcloud.consumer.bean.User; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableEurekaClient @RestController public class SpringcloudServiceConsumerApplication { @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(SpringcloudServiceConsumerApplication.class, args); } @GetMapping("/findUser/{username}") public User index(@PathVariable("username")String username){ return restTemplate().getForObject("http://EUREKA-SERVICE-PROVIDER/api/users/"+username,User.class); } }

附录:

ok,本博客参考官方教程进行实践,仅仅作为入门的学习参考资料,详情可以参考Spring Cloud官方文档

代码例子下载:code download

优质学习资料参考:

方志鹏大佬系列Spring Cloud博客:https://www.fangzhipeng.com/spring-cloud.html

使用Spring Cloud与Docker实战微服务:https://eacdy.gitbooks.io/spring-cloud-book/content/

程序员DD大佬系列Spring Cloud博客:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpdsjd.html