1. 建立父工程:父工程的的打包形式该为pom,删除其他无关的文件java
修改父工程的pom文件内容以下:mysql
<?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.offcn</groupId> <artifactId>microservice_cloud_01</artifactId> <version>1.0-SNAPSHOT</version> <!--手动指定 pom--> <packaging>pom</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <junit.version>4.12</junit.version> <spring-cloud.version>Finchley.SR2</spring-cloud.version> </properties> <!--依赖声明--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.RELEASE</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>
2. 建立子工程Eureka01web
添加ereka服务daunt的依赖spring
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
添加一个application.yml的配置文件,并配置相关内容(注意配置文件中书写的规范)sql
#内置的tomcat服务启动监听端口号 server: port: 6001 #EurekaServer配置 eureka: instance: hostname: eureka6001.com client: register-with-eureka: false #此EurekaServer不在注册到其余的注册中心 fetch-registry: false #不在从其余中心中心拉取服务器信息 service-url: defaultZone: http://eureka6002.com:6002/eureka #注册中心访问地址
建立一个启动类数据库
@SpringBootApplication @EnableEurekaServer public class AppStart { public static void main(String[] args) { SpringApplication.run(AppStart.class, args); } }
以上的三步就至关于建立了一个ereka服务器apache
3. 为了解决用户量访问过多致使服务器出现崩塌的问题,创建集群api
步骤与上面的步骤一致 http://localhost:${server.port}/eureka 通常是一个服务器时执行本身的url,多个服务器的时间想上面同样,指向另外一个服务器http://eureka6002.com:6002/eurekatomcat
4. 模拟功能放入服务器当中springboot
建立子工程做为公共的实体类,这里只须要建立相对应的数据库表和对应的实体类便可,并打包放入本地仓库
5. 建立子工程,实现简单的曾删改查功能
父工程中增长依赖
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.12</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency>
(1) 导入相关的依赖,尤为在这里须要用到实体类,因此须要把实体类的依赖引入这里
<dependencies> <dependency> <groupId>com.offcn</groupId> <artifactId>microservice_cloud_02_api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.12</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.0.2.RELEASE</version> </dependency> </dependencies>
(2)建立并设置配置文件application.yml
server: port: 8001 mybatis: config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径 type-aliases-package: com.offcn.springcloud.entities # 全部Entity别名类所在包 mapper-locations: classpath:mybatis/mapper/**/*.xml # mapper映射文件 spring: application: name: microservice-product #这个很重要,这在之后的服务与服务之间相互调用通常都是根据这个name datasource: type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操做类型 driver-class-name: com.mysql.jdbc.Driver # mysql驱动包 url: jdbc:mysql://127.0.0.1:3306/springboot?serverTimezone=GMT%2B8 # 数据库名称 username: root password: 111111 dbcp2: min-idle: 5 # 数据库链接池的最小维持链接数 initial-size: 5 # 初始化链接数 max-total: 5 # 最大链接数 max-wait-millis: 150 # 等待链接获取的最大超时时间 #EurekaServer配置 eureka: client: register-with-eureka: true #此EurekaServer不在注册到其余的注册中心 fetch-registry: true #不在从其余中心中心拉取服务器信息 service-url: defaultZone: http://eureka6002.com:6002/eureka,http://eureka6001.com:6001/eureka #注册中心访问地址
(3) 建立mapper层、service层、controller层实现简单的增删改查功能
这里的mapper映射文件放在resources目录下
mybatis全局配置文件一样也在resources目录下
(4) 建立启动类
@SpringBootApplication @MapperScan("com.offcn.springcloud.mapper") @EnableEurekaClient public class SpringCloudApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudApplication.class,args); } }
以上就是eureka的发现注册的实现的一个小入门