springCloud学习笔记(三)——eureka服务消费者

一、项目搭建

1.引入jar

  1. <!-- 引入eureka客户端 -->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  5. </dependency>

2.完整的pom文件内容

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <artifactId>com.springcloud</artifactId>
  7. <groupId>com.sc2020</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <groupId>com.example</groupId>
  11. <artifactId>consumer-eureka-client81</artifactId>
  12. <version>1.0.0-SNAPSHOT</version>
  13. <name>consumer-client81</name>
  14. <description>Demo project for Spring Boot</description>
  15. <properties>
  16. <java.version>1.8</java.version>
  17. </properties>
  18. <dependencies>
  19. <dependency>
  20. <groupId>com.sc2020</groupId>
  21. <artifactId>cloud-payment8001</artifactId>
  22. <version>1.0-SNAPSHOT</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>com.sc2020</groupId>
  26. <artifactId>cloud-api-commons</artifactId>
  27. <version>1.0-SNAPSHOT</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-web</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-actuator</artifactId>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.mybatis.spring.boot</groupId>
  39. <artifactId>mybatis-spring-boot-starter</artifactId>
  40. </dependency>
  41. <dependency>
  42. <groupId>com.alibaba</groupId>
  43. <artifactId>druid-spring-boot-starter</artifactId>
  44. <version>1.1.10</version>
  45. </dependency>
  46. <dependency>
  47. <groupId>mysql</groupId>
  48. <artifactId>mysql-connector-java</artifactId>
  49. </dependency>
  50. <dependency>
  51. <groupId>org.springframework.boot</groupId>
  52. <artifactId>spring-boot-starter-jdbc</artifactId>
  53. </dependency>
  54. <dependency>
  55. <groupId>org.springframework.boot</groupId>
  56. <artifactId>spring-boot-starter-test</artifactId>
  57. <scope>test</scope>
  58. </dependency>
  59. <dependency>
  60. <groupId>org.springframework.boot</groupId>
  61. <artifactId>spring-boot-devtools</artifactId>
  62. <scope>runtime</scope>
  63. <optional>true</optional>
  64. </dependency>
  65. <!-- 引入eureka客户端 -->
  66. <dependency>
  67. <groupId>org.springframework.cloud</groupId>
  68. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  69. </dependency>
  70. </dependencies>
  71. </project>

3.yml文件配置

  1. server:
  2. port: 81
  3. eureka:
  4. client:
  5. register-with-eureka: true #是否将自己注册到注册中心,集群必须设置为true配合ribbon
  6. fetch-registry: true #是否从服务端抓取已有的注册信息
  7. service-url:
  8. defaultZone: http://eureka7001.com:7001/eureka #hosts文件配置了eureka7001.com,如果没有配置则使用localhost
  9. instance:
  10. instance-id: consumer-client81
  11. prefer-ip-address: true
  12. spring:
  13. datasource:
  14. type: com.alibaba.druid.pool.DruidDataSource
  15. driver-class-name: org.gjt.mm.mysql.Driver
  16. url: jdbc:mysql://101.34.49.127:3306/cloud?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false
  17. username: 你的数据库用户
  18. password: 你的数据用户密码
  19. application:
  20. name: consumer-client
  21. mybatis:
  22. mapper-locations: classpath:mapper/*.xml

4.Application中main方法

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

注意:使用@EnableEurekaClient注解声明使用eureka组件

5.Controller控制层方法

  1. @RestController
  2. public class TestController {
  3. @Resource
  4. RestTemplate restTemplate;
  5. //你的服务提供者的名称
  6. public static final String PAYMENT_URL = "http://cloud-eureka-client";
  7. @GetMapping(value = "getMessage")
  8. public String getMessage(){
  9. //你项目服务提供者的接口路径
  10. String forObject = restTemplate.getForObject( PAYMENT_URL +"/paymentController/getPortMessage", String.class);
  11. return forObject;
  12. }
  13. }

3.eureka服务消费者 - 图1

6.请求访问

3.eureka服务消费者 - 图2

请求访问CLOUD-EUREKA-CLIENT7003接口

项目地址: https://gitee.com/xyz1041221997/springcloud.git