Spring Cloud中使用Eureka

一、创建00-eurekaserver-8000

(1)创建工程

创建一个Spring Initializr工程,命名为00-eurekaserver-8000,仅导入Eureka Server依赖即可

(2)pom.xml文件如下

若你使用的是JDK6、7、8,那么这些依赖无需导入。而JDK9及其以上版本需要导入

  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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.abc</groupId>
  6. <artifactId>00-eurekaserver-8000</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <parent>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-parent</artifactId>
  12. <version>2.1.7.RELEASE</version>
  13. <relativePath/> <!-- lookup parent from repository -->
  14. </parent>
  15. <properties>
  16. <java.version>1.8</java.version>
  17. <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
  18. </properties>
  19. <dependencies>
  20. <!--JAXB,Java Architechture for Xml Binding,XML绑定的Java技术
  21. JDK6、7、8版本的不用导入以下依赖,但9及其以上版本需要导入
  22. -->
  23. <!--<dependency>-->
  24. <!--<groupId>javax.xml.bind</groupId>-->
  25. <!--<artifactId>jaxb-api</artifactId>-->
  26. <!--<version>2.2.11</version>-->
  27. <!--</dependency>-->
  28. <!--<dependency>-->
  29. <!--<groupId>com.sun.xml.bind</groupId>-->
  30. <!--<artifactId>jaxb-core</artifactId>-->
  31. <!--<version>2.2.11</version>-->
  32. <!--</dependency>-->
  33. <!--<dependency>-->
  34. <!--<groupId>com.sun.xml.bind</groupId>-->
  35. <!--<artifactId>jaxb-impl</artifactId>-->
  36. <!--<version>2.2.11</version>-->
  37. <!--</dependency>-->
  38. <!--<dependency>-->
  39. <!--<groupId>javax.activation</groupId>-->
  40. <!--<artifactId>activation</artifactId>-->
  41. <!--<version>1.1.1</version>-->
  42. <!--</dependency>-->
  43. <dependency>
  44. <groupId>org.springframework.cloud</groupId>
  45. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  46. </dependency>
  47. <dependency>
  48. <groupId>org.springframework.boot</groupId>
  49. <artifactId>spring-boot-autoconfigure</artifactId>
  50. </dependency>
  51. </dependencies>
  52. <dependencyManagement>
  53. <dependencies>
  54. <dependency>
  55. <groupId>org.springframework.cloud</groupId>
  56. <artifactId>spring-cloud-dependencies</artifactId>
  57. <version>${spring-cloud.version}</version>
  58. <type>pom</type>
  59. <scope>import</scope>
  60. </dependency>
  61. </dependencies>
  62. </dependencyManagement>
  63. <build>
  64. <plugins>
  65. <plugin>
  66. <groupId>org.springframework.boot</groupId>
  67. <artifactId>spring-boot-maven-plugin</artifactId>
  68. </plugin>
  69. </plugins>
  70. </build>
  71. </project>

(3)创建并配置yml文件

  1. server:
  2. port: 8000
  3. eureka:
  4. instance:
  5. hostname: localhost # 指定Eureka主机
  6. client:
  7. register-with-eureka: false # 指定当前主机是否向Eureka服务器进行注册
  8. fetch-registry: false # 指定当前主机是否要从Eurka服务器下载服务注册列表
  9. service-url: # 服务暴露地址
  10. defaultZone: http://localhost:8000/eureka
  11. # defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
  12. # server:
  13. # enable-self-preservation: false # 关闭自我保护

(4)定义spring boot启动类

二、创建提供者工程02-provider-8081

(1)创建工程

复制01-provider-8081,并重命名为02-provider-8081

https://blog.csdn.net/u012965203/article/details/99065322

(2)pom.xml依赖如下

  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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.7.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.abc</groupId>
  12. <artifactId>02-provider-8081</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>02-provider-8081</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>com.alibaba</groupId>
  23. <artifactId>druid</artifactId>
  24. <version>1.1.10</version>
  25. </dependency>
  26. <!--actuator依赖-->
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-actuator</artifactId>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-starter-data-jpa</artifactId>
  34. </dependency>
  35. <!--eureka客户端依赖-->
  36. <dependency>
  37. <groupId>org.springframework.cloud</groupId>
  38. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  39. </dependency>
  40. <!--修改MySQL驱动版本-->
  41. <dependency>
  42. <groupId>mysql</groupId>
  43. <artifactId>mysql-connector-java</artifactId>
  44. <version>5.1.47</version>
  45. <scope>runtime</scope>
  46. </dependency>
  47. <dependency>
  48. <groupId>org.springframework.boot</groupId>
  49. <artifactId>spring-boot-starter-web</artifactId>
  50. </dependency>
  51. <dependency>
  52. <groupId>org.projectlombok</groupId>
  53. <artifactId>lombok</artifactId>
  54. <optional>true</optional>
  55. </dependency>
  56. <dependency>
  57. <groupId>org.springframework.boot</groupId>
  58. <artifactId>spring-boot-starter-test</artifactId>
  59. <scope>test</scope>
  60. </dependency>
  61. </dependencies>
  62. <dependencyManagement>
  63. <dependencies>
  64. <dependency>
  65. <groupId>org.springframework.cloud</groupId>
  66. <artifactId>spring-cloud-dependencies</artifactId>
  67. <version>${spring-cloud.version}</version>
  68. <type>pom</type>
  69. <scope>import</scope>
  70. </dependency>
  71. </dependencies>
  72. </dependencyManagement>
  73. <build>
  74. <plugins>
  75. <plugin>
  76. <groupId>org.springframework.boot</groupId>
  77. <artifactId>spring-boot-maven-plugin</artifactId>
  78. </plugin>
  79. </plugins>
  80. </build>
  81. </project>

(3)修改yml文件

  1. server:
  2. port: 8081
  3. spring:
  4. jpa:
  5. # 指定在Spring容器启动时是否自动建表,默认为false
  6. generate-ddl: true
  7. # 指定是否在控制台显示其执行的SQL语句,默认false
  8. show-sql: true
  9. # 指定应用重启时是否重新创建更新表
  10. hibernate:
  11. ddl-auto: none
  12. # 配置数据源
  13. datasource:
  14. type: com.alibaba.druid.pool.DruidDataSource
  15. driver-class-name: com.mysql.jdbc.Driver
  16. url: jdbc:mysql:///test?useUnicode=true&amp;characterEncoding=utf8
  17. username: root
  18. password: root
  19. # 指定当前微服务对象暴露的名称
  20. application:
  21. name: abcmsc-provider-depart
  22. logging:
  23. # 设置日志输出格式
  24. pattern:
  25. console: level-%level %msg%n
  26. level:
  27. root: info
  28. org.hibernate: info
  29. org.hibernate.type.descriptor.sql.BasicBinder: trace
  30. org.hibernate.type.descriptor.sql.BasicExtractor: trace
  31. com.abc.provider: debug
  32. # 指定Eureka服务中心
  33. eureka:
  34. client:
  35. service-url:
  36. defaultZone: http://localhost:8000/eureka
  37. # defaultZone: http://localhost:8100/eureka,http://localhost:8200/eureka,http://localhost:8300/eureka
  38. # 指定当前客户端主机在注册中心的名称
  39. # instance:
  40. # instance-id: abcmsc-provider-8081
  41. info:
  42. company.name: www.abc.com
  43. company.addr: China Beijing
  44. company.tel: 12345678
  45. app.name: abc-msc
  46. app.desc: mic-server-cloud
  47. author: abc

三、创建消费工程02-consumer-8080

消费者将使用提供者暴露的服务名称(spring.application.name)来消费服务

(1)创建工程

复制01-consumer-8080,并重命名为02-consumer-8080

https://blog.csdn.net/u012965203/article/details/99065322

(2)添加依赖管理及依赖

  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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.abc</groupId>
  6. <artifactId>02-consumer-8080</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <parent>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-parent</artifactId>
  12. <version>2.1.3.RELEASE</version>
  13. <relativePath/> <!-- lookup parent from repository -->
  14. </parent>
  15. <properties>
  16. <java.version>1.8</java.version>
  17. <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
  18. </properties>
  19. <dependencies>
  20. <!--actuator依赖-->
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-actuator</artifactId>
  24. </dependency>
  25. <!--eureka客户端依赖-->
  26. <dependency>
  27. <groupId>org.springframework.cloud</groupId>
  28. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  29. <version>2.1.2.RELEASE</version>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-starter-web</artifactId>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.projectlombok</groupId>
  37. <artifactId>lombok</artifactId>
  38. <optional>true</optional>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.springframework.cloud</groupId>
  42. <artifactId>spring-cloud-commons</artifactId>
  43. <version>2.1.2.RELEASE</version>
  44. <scope>compile</scope>
  45. </dependency>
  46. </dependencies>
  47. <dependencyManagement>
  48. <dependencies>
  49. <dependency>
  50. <groupId>org.springframework.cloud</groupId>
  51. <artifactId>spring-cloud-dependencies</artifactId>
  52. <version>${spring-cloud.version}</version>
  53. <type>pom</type>
  54. <scope>import</scope>
  55. </dependency>
  56. </dependencies>
  57. </dependencyManagement>
  58. <build>
  59. <plugins>
  60. <plugin>
  61. <groupId>org.springframework.boot</groupId>
  62. <artifactId>spring-boot-maven-plugin</artifactId>
  63. </plugin>
  64. </plugins>
  65. </build>
  66. </project>

(3)修改yml文件

  1. spring:
  2. application:
  3. name: abcmsc-consumer-depart
  4. eureka:
  5. client:
  6. service-url:
  7. defaultZone: http://localhost:8000/eureka
  8. # defaultZone: http://localhost:8100/eureka,http://localhost:8200/eureka,http://localhost:8300/eureka

(4)修改处理器

(5)修改JavaConfig类

四、测试

(1)启动eureka

(2)启动提供者

(3)启动消费者

(4)查看eureka服务列表

(5)测试功能

五、EurekaServer集群集群

这里要搭建的EurekaServer集群中包含三个EurekaServer节点,其端口号分别为8100、8200与8300

设置域名

在C:\Windows\System32\drivers\etc的host文件中添加如下域名映射信息

  1. 127.0.0.1 eureka8100.com
  2. 127.0.0.1 eureka8200.com
  3. 127.0.0.1 eureka8300.com

一、创建00-eurekaserver-8100

(1)创建工程

复制00-eurekaserver-8000工程,并重命名为00-eurekaserver-8100

(2) 修改pom

(3)修改配置文件

  1. server:
  2. port: 8100
  3. eureka:
  4. instance:
  5. hostname: eureka8100.com # 指定Eureka主机
  6. client:
  7. register-with-eureka: false # 指定当前主机是否需要向注册中心注册(不用,因为当前主机是Server,不是Client)
  8. fetch-registry: false # 指定当前主机是否需要获取注册信息(不用,因为当前主机是Server,不是Client)
  9. service-url: # 暴露服务中心地址
  10. defaultZone: http://eureka8100.com:8100/eureka,http://eureka8200.com:8200/eureka,http://eureka8300.com:8300/eureka

(4)创建00-eurekaserver-8200

再以相同的方式再复制出00-eurekaserver-8200

(5)创建00-eurekaserver-8300

再以相同的方式再复制出00-eurekaserver-8300

(6)修改提供者配置文件

(7)修改消费者配置文件

(8)启动eureka8100.com

(9)启动eureka8200.com

(10)启动eureka8300.com

(11)启动消费者

(12)启动提供者

(13)查看提供服务列表

posted @ 2020-07-07 22:48  edda_huang  阅读(171)  评论(0编辑  收藏  举报