springcloud微服务-项目搭建

附:乐优商城19天完整版

前言:

     乐优商城这个视频还可以,于是拿来练练手,我对着视频搭环境一直在service服务模块卡住了,注册中心和网关可以启动,服务模块却一直启动不了,报各种奇怪的错,网上也没有什么好的解决办法,于是我就自己改动了下,不完全按视频的,于是搭建成功了,值得注意的是springcloud的版本不同,配置的也不太一样的。

项目结构:

springcloud微服务-项目搭建_第1张图片

1.创建父工程:

父工程的pom.xml:

(ps:此处我的springcloud的版本是Finchley.SR1,并不是传智视频上的Finchley.RC1,我刚开始用的是RC1,结果发现在service模块的启动一直会报错,不知道有没有人遇到跟我一样的情况。)



    4.0.0

    com.leyou.parent
    leyou
    pom
    1.0.0-SNAPSHOT
    
        ly-registry
        ly-api-gateway
        ly-item
    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.2.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
        Finchley.SR1
        1.3.2
        2.0.2
        1.1.9
        5.1.32
        1.2.3
        1.0.0-SNAPSHOT
        1.26.1-RELEASE
    


    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    
    
        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
    
            
                org.mybatis.spring.boot
                mybatis-spring-boot-starter
                ${mybatis.starter.version}
            
            
            
                tk.mybatis
                mapper-spring-boot-starter
                ${mapper.starter.version}
            
            
            
                com.github.pagehelper
                pagehelper-spring-boot-starter
                ${pageHelper.starter.version}
            
            
            
                mysql
                mysql-connector-java
                ${mysql.version}
            
            
            
                com.github.tobato
                fastdfs-client
                ${fastDFS.client.version}
            
        

    
    
        
            org.springframework.boot
            spring-boot-maven-plugin
        
    
    

    
    
        spring-milestones
        Spring Milestones
        https://repo.spring.io/milestone
        
            false
        
    
    

2.创建 Eureka注册中心

模块结构:

springcloud微服务-项目搭建_第2张图片

pom.xml:



    
        leyou
        com.leyou.parent
        1.0.0-SNAPSHOT
    
    4.0.0

    com.leyou.common
    ly-registry
    1.0.0-SNAPSHOT

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

创建启动类:LyRegistry

package com.leyou;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * @author: Lucifer
 * @create: 2018-10-12 01:14
 * @description:
 **/
@SpringBootApplication
@EnableEurekaServer
public class LyRegistry {
     public static void main(String[] args) {
         SpringApplication.run(LyRegistry.class, args);
     }
}

application.yml: 

ps:

     这里的MySQL连接信息的url建议后面加上:useUnicode=true&characterEncoding=utf8,完整的:jdbc:mysql://localhost:3306/heima?useUnicode=true&characterEncoding=utf8,如果不加的话,后面你会遇到坑的,在数据库执行增加的时候。

server:
  port: 10086
spring:
  application:
    name: ly-registry
  datasource:
    url: jdbc:mysql://localhost:3306/heima?useUnicode=true&characterEncoding=utf8
    username: root
    password: 123456
    hikari:
      maximum-pool-size: 30
      minimum-idle: 10
eureka:
  client:
    fetchRegistry: false
    registerWithEureka: false
    serviceUrl:
      defaultZone: http://127.0.0.1:${server.port}/eureka
  server:
    enableSelfPreservation: false # 关闭自我保护
    evictionIntervalTimerInMs: 5000 # 每隔5秒进行一次服务列表清理

run启动类,如图:成功启动Eureka注册中心

springcloud微服务-项目搭建_第3张图片

3.创建Zuul网关

springcloud微服务-项目搭建_第4张图片

pom.xml:



    
        leyou
        com.leyou.parent
        1.0.0-SNAPSHOT
    
    4.0.0

    com.leyou.common
    ly-api-gateway
    1.0.0-SNAPSHOT

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-zuul
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
    

启动类:LyApiGateway

package com.leyou;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

/**
 * @author: Lucifer
 * @create: 2018-10-12 01:32
 * @description:
 **/
@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class LyApiGateway {
    public static void main(String[] args) {
        SpringApplication.run(LyApiGateway.class, args);
    }
}

网关的配置文件:application.yml: 

server:
  port: 10010
spring:
  application:
    name: api-gateway
  datasource:
    url: jdbc:mysql://localhost:3306/heima
    username: root
    password: 123456
    hikari:
      maximum-pool-size: 30
      minimum-idle: 10
eureka:
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:10086/eureka
    registryFetchIntervalSeconds: 5
  instance:
    preferIpAddress: true
    ipAddress: 127.0.0.1
    instanceId: ${spring.application.name}:${server.port}
zuul:
  prefix: /api # 添加路由前缀
  retryable: true
ribbon:
  ConnectTimeout: 250 # 连接超时时间(ms)
  ReadTimeout: 2000 # 通信超时时间(ms)
  OkToRetryOnAllOperations: true # 是否对所有操作重试
  MaxAutoRetriesNextServer: 1 # 同一服务不同实例的重试次数
  MaxAutoRetries: 1 # 同一实例的重试次数
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMillisecond: 10000 # 熔断超时时长:10000ms

run启动类:如图:成功运行

springcloud微服务-项目搭建_第5张图片

4.创建商品微服务

在ly-item中创建两个子工程:

  • ly-item-interface:主要是对外暴露的接口及相关实体类

  • ly-item-service:所有业务逻辑及内部使用接口

springcloud微服务-项目搭建_第6张图片

pom.xml: 



    
        ly-item
        com.leyou.service
        1.0.0-SNAPSHOT
    
    4.0.0

    com.leyou.service
    ly-item-service
    1.0.0-SNAPSHOT

    
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            ${mybatis.starter.version}
        
        
        
            tk.mybatis
            mapper-spring-boot-starter
            ${mapper.starter.version}
        
        
        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            ${pageHelper.starter.version}
        
        
        
            mysql
            mysql-connector-java
            ${mysql.version}
        
        
            com.leyou.service
            ly-item-interface
            ${leyou.latest.version}
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
    

启动类:LyItemService

package com.leyou;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @author: Lucifer
 * @create: 2018-10-12 01:38
 * @description:
 **/
@SpringBootApplication
@EnableDiscoveryClient
public class LyItemService {
    public static void main(String[] args) {
        SpringApplication.run(LyItemService.class, args);
    }
}

配置文件:application.yml: 

server:
  port: 8081
spring:
  application:
    name: item-service
  datasource:
    url: jdbc:mysql://localhost:3306/heima
    username: root
    password: 123456
    hikari:
      maximum-pool-size: 30
      minimum-idle: 10
eureka:
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:10086/eureka
  instance:
    leaseRenewalIntervalInSeconds: 5 # 每隔5秒发送一次心跳
    leaseExpirationDurationInSeconds: 10 # 10秒不发送就过期
    preferIpAddress: true
    ipAddress: 127.0.0.1
    instanceId: ${spring.application.name}:${server.port}

run启动类:如图,成功运行

springcloud微服务-项目搭建_第7张图片

然后访问浏览器http://127.0.0.1:10086/,如图:

springcloud微服务-项目搭建_第8张图片

服务成功注册!!!!

 

 

你可能感兴趣的:(微服务实战篇)