Eureka注册与发现集群
Posted 你所学的知识,就是你持有的武器
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Eureka注册与发现集群相关的知识,希望对你有一定的参考价值。
3、eureka集群构建
1)Eureka Server集群环境 7001 7002 构建步骤
① application.yml
互相注册,相互守望
server: port: 7001 eureka: instance: hostname: eureka7001.com client: register-with-eureka: false fetch-registry: false service-url: #集群版 defaultZone: http://eureka7002.com:7002/eureka/ server: port: 7002 eureka: instance: hostname: eureka7002.com client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://eureka7001.com:7001/eureka/
2)支付微服务8001微服务发布到上面eureka集群
①、yml文件配置
server: port: 8001 spring: application: name: cloud-provider-hystrix-payment eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
3)订单服务80微服务发布到上面eureka集群
server: port: 80 spring: application: name: cloud-consumer-order eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
4)打开eureka注册中心,查看服务的注册情况
4、支付微服务集群配置
增加支付服务 payment:8002
①、pom文件和payment8001一致
②、yml
server: port: 8002 spring: application: name: cloud-payment-service datasource: type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型 driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包 url: jdbc:mysql://localhost:3306/springcloud2?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: houchen mybatis: mapperLocations: classpath:mapper/*.xml #指定sql映射文件的位置 type-aliases-package: com.atguigu.springcloud.entity # 所有Entity别名类所在包 eureka: client: register-with-eureka: true #表示是否将自己注册到EurekaServer fetch-registry: true #是否从eurekaServer抓取已有的注册信息,默认为true,单点无所谓,集群必须设置weitrue才能配合ribbon使用负载均衡 service-url: defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka
③、主启动类
@SpringBootApplication @EnableEurekaClient public class PaymentMain8002 { public static void main(String[] args) { SpringApplication.run(PaymentMain8002.class,args); } }
④、payment8001 8002 业务类controller
@RestController @Slf4j public class PaymentController { @Resource private PaymentService paymentService; @Value("${server.port}") //标记本次order调用payment使用的是哪个端口的服务 private String serverPort; @PostMapping(value = "/payment/create") public CommonResult create(@RequestBody Payment payment){ int result = paymentService.create(payment); if(result>0){ return new CommonResult(200,"插入数据成功 O(∩_∩)O哈哈~"+serverPort); }else{ return new CommonResult(444,"插入数据失败 port:"+serverPort,null); } } // {id}代表者路径占位符 @GetMapping(value = "/payment/get/{id}") public CommonResult getPaymentById(@PathVariable("id") Long id){ Payment payment = paymentService.getPaymentById(id); log.info("查询数据===热部署后"+payment); if(payment!=null){ return new CommonResult(200,"数据查询成功 port:"+serverPort,payment); }else{ return new CommonResult(444,"数据查询失败 port:"+serverPort,null); } } }
查看eureka:发现有两个服务提供者
⑤、order80动态选择 payment集群服务中的一台进行访问
在order80中,远程服务的地址不能写死,需要动态写payment服务的spring.application.name
@RestController @Slf4j public class OrderController { //private static final String PAYMANT_URL ="http://localhost:8001"; private static final String PAYMANT_URL ="http://CLOUD-PAYMENT-SERVICE"; @Autowired private RestTemplate restTemplate; @GetMapping("/consumer/payment/create") public CommonResult create(Payment payment){ return restTemplate.postForObject(PAYMANT_URL+"/payment/create",payment,CommonResult.class); } @GetMapping("/consumer/payment/get/{id}") public CommonResult getPaymentById(@PathVariable("id") Long id){ return restTemplate.getForObject(PAYMANT_URL+"/payment/get/"+id,CommonResult.class); } }
此时访问 http://localhost/consumer/payment/get/1 会报错,因为order80并不知道要调用哪个payment服务
此时,要给 RestTemplate开启负载均衡功能
添加配置类:
@Configuration public class ApplicationContextConfig { @Bean @LoadBalanced public RestTemplate getRestTemplate(){ return new RestTemplate(); } }
访问同一个url ,会在8001 8002之间轮询
5、微服务信息完善
主机名称的修改 + 偏向ip
eureka: instance: instance-id: payment8001 prefer-ip-address: true //eureka中的路径会显示ip信息
6、服务发现Discovery
通过discovey可以获取注册进eureka所有服务的信息
@RestController @Slf4j public class PaymentController { @Resource private PaymentService paymentService; @Value("${server.port}") private String serverPort; @Resource private DiscoveryClient discoveryClient; @GetMapping("/payment/discovery") public Object discovery(){ List<String> services = discoveryClient.getServices(); for(String service : services){ log.info("element:",service); } List<ServiceInstance> instances = discoveryClient.getInstances("cloud-payment-service"); for(ServiceInstance instance : instances){ log.info(instance.getServiceId()+"\\t"+instance.getHost()+"\\t"+instance .getPort()+ "\\t"+instance.getUri()); } return this.discoveryClient; } }
以上是关于Eureka注册与发现集群的主要内容,如果未能解决你的问题,请参考以下文章
SpringCloud系列四:Eureka 服务发现框架(定义 Eureka 服务端Eureka 服务信息Eureka 发现管理Eureka 安全配置Eureka-HA(高可用) 机制Eur(代码片段