SpringCloud(二)服务注册中心集群搭建

我就是我 2022-03-29 09:37 385阅读 0赞

springcloud学习总结

2、服务注册中心集群搭建

一、新建服务注册中心eureka7002模块,拷贝eureka7001模块的pom以及yml

修改yml文件

  1. server:
  2. port: 7002
  3. eureka:
  4. instance:
  5. hostname: eureka7002.com #eureka服务端的实例名称
  6. client:
  7. register-with-eureka: false #false表示不向注册中心注册自己。
  8. fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
  9. service-url:
  10. defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/

编写主启动类。并标注@EnableEurekaServer

二、新建服务注册中心eureka7002模块,拷贝eureka7001模块的pom以及yml

修改yml文件

  1. server:
  2. port: 7003
  3. eureka:
  4. instance:
  5. hostname: eureka7003.com #eureka服务端的实例名称
  6. client:
  7. register-with-eureka: false #false表示不向注册中心注册自己。
  8. fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
  9. service-url:
  10. defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7001.com:7001/eureka/

编写主启动类。并标注@EnableEurekaServer

三、修改eureka7001的application.yml文件

  1. server:
  2. port: 7001
  3. eureka:
  4. instance:
  5. hostname: eureka7001.com #eureka服务端的实例名称
  6. client:
  7. register-with-eureka: false #false表示不向注册中心注册自己。
  8. fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
  9. service-url:
  10. #defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

四、修改provider8001的application.yml文件

  1. server:
  2. port: 8001
  3. mybatis:
  4. config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径
  5. type-aliases-package: com.atguigu.springcloud.entities # 所有Entity别名类所在包
  6. mapper-locations:
  7. - classpath:mybatis/mapper/**/*.xml # mapper映射文件
  8. spring:
  9. application:
  10. name: microservicecloud-dept
  11. datasource:
  12. type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
  13. driver-class-name: com.mysql.jdbc.Driver # mysql驱动包
  14. url: jdbc:mysql://localhost:3306/cloudDB01 # 数据库名称
  15. username: root
  16. password: 123321
  17. dbcp2:
  18. min-idle: 5 # 数据库连接池的最小维持连接数
  19. initial-size: 5 # 初始化连接数
  20. max-total: 5 # 最大连接数
  21. max-wait-millis: 200 # 等待连接获取的最大超时时间
  22. eureka:
  23. client: #客户端注册进eureka服务列表内
  24. service-url:
  25. defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  26. instance:
  27. instance-id: microservicecloud-dept8001 #自定义服务名称信息
  28. prefer-ip-address: true #访问路径可以显示IP地址
  29. #
  30. info:
  31. app.name: atguigu-microservicecloud
  32. company.name: www.atguigu.com
  33. build.artifactId: $project.artifactId$
  34. build.version: $project.version$

五、修改consumer80的application.yml

  1. server:
  2. port: 80
  3. eureka:
  4. client:
  5. register-with-eureka: false
  6. service-url:
  7. defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

修改服务处理类

  1. package com.atguigu.springcloud.controller;
  2. import java.util.List;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.PathVariable;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import org.springframework.web.client.RestTemplate;
  8. import com.atguigu.springcloud.entities.Dept;
  9. @RestController
  10. public class DeptController_Consumer
  11. {
  12. private static final String REST_URL_PREFIX = "http://MICROSERVICECLOUD-DEPT";
  13. /** * 使用 使用restTemplate访问restful接口非常的简单粗暴无脑。 (url, requestMap, * ResponseBean.class)这三个参数分别代表 REST请求地址、请求参数、HTTP响应转换被转换成的对象类型。 */
  14. @Autowired
  15. private RestTemplate restTemplate;
  16. @RequestMapping(value = "/consumer/dept/add")
  17. public boolean add(Dept dept)
  18. {
  19. return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class);
  20. }
  21. @RequestMapping(value = "/consumer/dept/get/{id}")
  22. public Dept get(@PathVariable("id") Long id)
  23. {
  24. return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class);
  25. }
  26. @SuppressWarnings("unchecked")
  27. @RequestMapping(value = "/consumer/dept/list")
  28. public List<Dept> list()
  29. {
  30. return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class);
  31. }
  32. }

启动三个服务注册中心,启动服务提供者与服务消费者,发送消费请求查看。

后续:服务发现与负载均衡ribbon…

发表评论

表情:
评论列表 (有 0 条评论,385人围观)

还没有评论,来说两句吧...

相关阅读