Spring Cloud中使用Eureka
一、创建00-eurekaserver-8000
(1)创建工程
创建一个Spring Initializr工程,命名为00-eurekaserver-8000,仅导入Eureka Server依赖即可
(2)pom.xml文件如下
若你使用的是JDK6、7、8,那么这些依赖无需导入。而JDK9及其以上版本需要导入
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <groupId>com.abc</groupId>
- <artifactId>00-eurekaserver-8000</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
-
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.1.7.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
-
- <properties>
- <java.version>1.8</java.version>
- <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
- </properties>
-
- <dependencies>
- <!--JAXB,Java Architechture for Xml Binding,XML绑定的Java技术
- JDK6、7、8版本的不用导入以下依赖,但9及其以上版本需要导入
- -->
- <!--<dependency>-->
- <!--<groupId>javax.xml.bind</groupId>-->
- <!--<artifactId>jaxb-api</artifactId>-->
- <!--<version>2.2.11</version>-->
- <!--</dependency>-->
- <!--<dependency>-->
- <!--<groupId>com.sun.xml.bind</groupId>-->
- <!--<artifactId>jaxb-core</artifactId>-->
- <!--<version>2.2.11</version>-->
- <!--</dependency>-->
- <!--<dependency>-->
- <!--<groupId>com.sun.xml.bind</groupId>-->
- <!--<artifactId>jaxb-impl</artifactId>-->
- <!--<version>2.2.11</version>-->
- <!--</dependency>-->
- <!--<dependency>-->
- <!--<groupId>javax.activation</groupId>-->
- <!--<artifactId>activation</artifactId>-->
- <!--<version>1.1.1</version>-->
- <!--</dependency>-->
-
-
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-autoconfigure</artifactId>
- </dependency>
- </dependencies>
-
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>${spring-cloud.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
-
- </project>
(3)创建并配置yml文件
- server:
- port: 8000
-
- eureka:
- instance:
- hostname: localhost # 指定Eureka主机
- client:
- register-with-eureka: false # 指定当前主机是否向Eureka服务器进行注册
- fetch-registry: false # 指定当前主机是否要从Eurka服务器下载服务注册列表
- service-url: # 服务暴露地址
- defaultZone: http://localhost:8000/eureka
- # defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
-
- # server:
- # 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依赖如下
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.1.7.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.abc</groupId>
- <artifactId>02-provider-8081</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>02-provider-8081</name>
- <description>Demo project for Spring Boot</description>
-
- <properties>
- <java.version>1.8</java.version>
- <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
- </properties>
-
- <dependencies>
-
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.1.10</version>
- </dependency>
-
- <!--actuator依赖-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-actuator</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-jpa</artifactId>
- </dependency>
- <!--eureka客户端依赖-->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
-
- <!--修改MySQL驱动版本-->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.47</version>
- <scope>runtime</scope>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
-
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>${spring-cloud.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
-
- </project>
(3)修改yml文件
- server:
- port: 8081
-
- spring:
- jpa:
- # 指定在Spring容器启动时是否自动建表,默认为false
- generate-ddl: true
- # 指定是否在控制台显示其执行的SQL语句,默认false
- show-sql: true
- # 指定应用重启时是否重新创建更新表
- hibernate:
- ddl-auto: none
-
- # 配置数据源
- datasource:
- type: com.alibaba.druid.pool.DruidDataSource
- driver-class-name: com.mysql.jdbc.Driver
- url: jdbc:mysql:///test?useUnicode=true&characterEncoding=utf8
- username: root
- password: root
-
- # 指定当前微服务对象暴露的名称
- application:
- name: abcmsc-provider-depart
-
-
- logging:
- # 设置日志输出格式
- pattern:
- console: level-%level %msg%n
- level:
- root: info
- org.hibernate: info
- org.hibernate.type.descriptor.sql.BasicBinder: trace
- org.hibernate.type.descriptor.sql.BasicExtractor: trace
- com.abc.provider: debug
-
- # 指定Eureka服务中心
- eureka:
- client:
- service-url:
- defaultZone: http://localhost:8000/eureka
- # defaultZone: http://localhost:8100/eureka,http://localhost:8200/eureka,http://localhost:8300/eureka
-
- # 指定当前客户端主机在注册中心的名称
- # instance:
- # instance-id: abcmsc-provider-8081
-
- info:
- company.name: www.abc.com
- company.addr: China Beijing
- company.tel: 12345678
- app.name: abc-msc
- app.desc: mic-server-cloud
- author: abc
三、创建消费工程02-consumer-8080
消费者将使用提供者暴露的服务名称(spring.application.name)来消费服务
(1)创建工程
复制01-consumer-8080,并重命名为02-consumer-8080
https://blog.csdn.net/u012965203/article/details/99065322
(2)添加依赖管理及依赖
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <groupId>com.abc</groupId>
- <artifactId>02-consumer-8080</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
-
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.1.3.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
-
- <properties>
- <java.version>1.8</java.version>
- <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
- </properties>
-
- <dependencies>
- <!--actuator依赖-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-actuator</artifactId>
- </dependency>
- <!--eureka客户端依赖-->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- <version>2.1.2.RELEASE</version>
- </dependency>
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-commons</artifactId>
- <version>2.1.2.RELEASE</version>
- <scope>compile</scope>
- </dependency>
-
-
- </dependencies>
-
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>${spring-cloud.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
-
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
-
-
- </project>
(3)修改yml文件
- spring:
- application:
- name: abcmsc-consumer-depart
-
- eureka:
- client:
- service-url:
- defaultZone: http://localhost:8000/eureka
- # 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文件中添加如下域名映射信息
- 127.0.0.1 eureka8100.com
- 127.0.0.1 eureka8200.com
- 127.0.0.1 eureka8300.com
一、创建00-eurekaserver-8100
(1)创建工程
复制00-eurekaserver-8000工程,并重命名为00-eurekaserver-8100
(2) 修改pom
(3)修改配置文件
- server:
- port: 8100
-
- eureka:
- instance:
- hostname: eureka8100.com # 指定Eureka主机
- client:
- register-with-eureka: false # 指定当前主机是否需要向注册中心注册(不用,因为当前主机是Server,不是Client)
- fetch-registry: false # 指定当前主机是否需要获取注册信息(不用,因为当前主机是Server,不是Client)
- service-url: # 暴露服务中心地址
- 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)查看提供服务列表