Euraka适合初学者的简单小demo

1. 创建父工程:父工程的的打包形式该为pom,删除其余无关的文件

  修改父工程的pom文件内容如下:



    4.0.0

    com.offcn
    microservice_cloud_01
    1.0-SNAPSHOT

    
    pom
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.3.RELEASE
    
    
        UTF-8
        1.8
        1.8
        4.12
        Finchley.SR2
    

    
    
    
        
            org.springframework.cloud
            spring-cloud-dependencies
            Greenwich.RELEASE
            pom
            import
        
    
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


 2. 创建子工程Eureka01

   添加ereka服务daunt的依赖

  
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server

 添加一个application.yml的配置文件,并配置相关内容(注意配置文件中书写的规范)

#内置的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服务器

3. 为了解决用户量访问过多导致服务器出现崩塌的问题,建立集群

 步骤与上面的步骤一致  http://localhost:${server.port}/eureka 一般是一个服务器时执行自己的url,多个服务器的时间想上面一样,指向另一个服务器http://eureka6002.com:6002/eureka

4. 模拟功能放入服务器当中

创建子工程作为公共的实体类,这里只需要创建相对应的数据库表和对应的实体类即可,并打包放入本地仓库

5. 创建子工程,实现简单的曾删改查功能

  父工程中增加依赖


            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        
        
            com.alibaba
            druid
            1.1.12
        
        
            mysql
            mysql-connector-java
            8.0.13
        
        
            junit
            junit
            ${junit.version}
            test
        

 (1) 导入相关的依赖,尤其在这里需要用到实体类,所以需要把实体类的依赖引入这里

   


        
            com.offcn
            microservice_cloud_02_api
            1.0-SNAPSHOT
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        
        
            org.springframework.boot
            spring-boot-starter-test
        
        
            junit
            junit
        
        
            mysql
            mysql-connector-java
        
        
            com.alibaba
            druid
            1.1.12
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
            2.0.2.RELEASE
        
    

 (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的发现注册的实现的一个小入门

 别忘记配置:

ip与域名绑定
C:\Windows\System32\drivers\etc的hosts文件
127.0.0.1 eureka6001.com
127.0.0.1 eureka6002.com

你可能感兴趣的:(Euraka适合初学者的简单小demo)