当前位置:   article > 正文

SpringCloud中的Eureka使用_eureka-client4.1.0 instance-id

eureka-client4.1.0 instance-id

1.首先需要引入依赖

  1. <!--eureka的客户端-->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  5. </dependency>
  6. <!--eureka-服务端-->
  7. <dependency>
  8. <groupId>org.springframework.cloud</groupId>
  9. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  10. /dependency>

2.在提供者的application.yml中进行eureka配置(这里使用了集群,就不一一列举了)

  1. server:
  2. port: 8001
  3. spring:
  4. application:
  5. name: cloud-payment-service
  6. datasource:
  7. type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
  8. driver-class-name: com.mysql.cj.jdbc.Driver # mysql驱动包
  9. url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
  10. username: root
  11. password: root
  12. mybatis:
  13. mapperLocations: classpath:mapper/*.xml
  14. eureka:
  15. client:
  16. #表示是否将自己注册进Eurekaserver默认为true。
  17. register-with-eureka: true
  18. #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
  19. fetchRegistry: true
  20. service-url:
  21. #单机模式
  22. #defaultZone:http://eureka7001.com:7001/eureka
  23. #进行集群操作
  24. defaultZone: http://eureka7001.com:7001/eureka,http://eureka7003.com:7003/eureka
  25. instance:
  26. instance-id: payment8001

注册中心的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. #集群指向其它eureka服务
  11. defaultZone: http://eureka7003.com:7003/eureka/
  12. #单机就是7001自己
  13. #defaultZone: http://eureka7001.com:7001/eureka/

消费者的application.xml

  1. server:
  2. port: 80
  3. spring:
  4. application:
  5. name: cloud-order-service
  6. eureka:
  7. client:
  8. #表示是否将自己注册进Eurekaserver默认为true。
  9. register-with-eureka: true
  10. #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
  11. fetchRegistry: true
  12. service-url:
  13. defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/

3.在提供者启动类使用注解@EnableEurekaClient

       注册中心启动类使用@EnableEurekaServer

下面是一些测试Demo

首先是提供者:

Controller层

  1. @Value("${server.port}")
  2. private String serverPort;//添加serverPort
  3. @GetMapping(value = "/payment/get/{id}")
  4. public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id)
  5. {
  6. Payment payment = paymentService.getPaymentById(id);
  7. if(payment != null)
  8. {
  9. return new CommonResult(200,"查询成功,serverPort:"+serverPort,payment);
  10. }else{
  11. return new CommonResult(444,"没有对应记录,查询ID: "+id,null);
  12. }
  13. }

服务层

  1. @Service
  2. public class PaymentServiceImpl implements PaymentService
  3. {
  4. @Resource
  5. private PaymentDao1 paymentDao;
  6. public Payment getPaymentById(Long id)
  7. {
  8. return paymentDao.getPaymentById(id);
  9. }
  10. }

Dao层

  1. @Mapper
  2. public interface PaymentDao1
  3. {
  4. Payment getPaymentById(@Param("id") Long id);
  5. }

Mapper

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3. <mapper namespace="com.peng.springcloud.dao.PaymentDao1">
  4. <resultMap id="BaseResultMap" type="com.peng.springcloud.dto.Payment">
  5. <id column="id" property="id" jdbcType="BIGINT"/>
  6. <id column="serial" property="serial" jdbcType="VARCHAR"/>
  7. </resultMap>
  8. <select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap">
  9. select * from payment where id=#{id};
  10. </select>
  11. </mapper>

其次是消费者

Controller

  1. @RestController
  2. @Slf4j
  3. @RequestMapping("/consumer/payment")
  4. public class OrderController {
  5. final static String PAYMENT_URL="http://CLOUD-PAYMENT-SERVICE";
  6. @Resource
  7. private RestTemplate restTemplate;
  8. @GetMapping("/get/{id}")
  9. public CommonResult<Payment> getPayment(@PathVariable("id") Long id){
  10. return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id, CommonResult.class);
  11. }
  12. }

启动类

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. public class OrderMain80 {
  4. public static void main(String[] args) {
  5. SpringApplication.run(OrderMain80.class, args);
  6. }
  7. }

配置类

        集群需要在消费者的配置上加上@LoadBalanced注解,在controller中吧请求路径改为服务的名字       

 

  1. @Configuration
  2. public class ApplicationContextConfig {
  3. @Bean
  4. @LoadBalanced
  5. public RestTemplate getRestTemplate(){
  6. return new RestTemplate();
  7. }
  8. }

运行结果:

请求http://eureka7001.com:7001/这里的eureka7001.com路径需要在host中配置

 请求注册中心http://eureka7003.com:7003/ (这里没有在上面写,配置什么的和第一个注册中心的配置一样)

请求提供者请求http://localhost:8001/payment/get/11

请求消费者请求http://localhost/consumer/payment/get/11

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/914865
推荐阅读
相关标签
  

闽ICP备14008679号