SpringCloud项目后台环境搭建实战(二)

目录

创建Zuul网关

1.创建Module 

2.填写保存的目录

3.找到pom文件添加依赖

4.在resources文件夹下添加application.yml文件

5.编写启动类LeyouGatewayApplication

6.打开dashboard面板

7. 启动gatewayapplication

8.重启registeredapplicaiton

创建"商品"微服务

1.创建公共接口模块

2.填写保存的目录

3.填写保存的位置

4.完善pom文件,把打包的方式设置为pom

5.创建leyou-item-interface

6.填写interface的保存位置

7.创建商品微服务service模块

8.填写service的保存位置

9.在pom文件中添加依赖

10.新建一个本地数据库

11.在service里面新建yml文件

12. 在interface里面新建一个包

13.在service里面编写引导类

14在zuul模块的yml文件中配置路由

15.添加actuator依赖进行服务监控

16.测试"商品"微服务的启动

创建工具类模块

1.在父工程中新建一个module

2.添加工程描述

3.填写位置信息


创建Zuul网关

1.创建Module 

 

2.填写保存的目录

3.找到pom文件添加依赖

<?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">
    <parent>
        <artifactId>leyou</artifactId>
        <groupId>com.leyou.parent</groupId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.leyou.gateway</groupId>
    <artifactId>leyou.gateway</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
    </dependencies>

</project>

4.在resources文件夹下添加application.yml文件

server:
  port: 10010
spring:
  application:
    name: leyou-gateway
eureka:
    client:
      service-url:
        defaultZone: http://localhost:10086/eureka
      registry-fetch-interval-seconds: 5
zuul:
  prefix: /api

5.编写启动类LeyouGatewayApplication

@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class LeyouGatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(LeyouGatewayApplication.class);
    }
}

6.打开dashboard面板

刷新maven包,会提示是否showdashboard,你点show,之后的效果如下

7. 启动gatewayapplication

这时eureka会多出一个服务项

8.重启registeredapplicaiton

再刷新页面就只能看到一个gateway服务了

创建"商品"微服务

1.创建公共接口模块

New一个module

 

2.填写保存的目录

3.填写保存的位置

 

4.完善pom文件,把打包的方式设置为pom

<?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">
    <parent>
        <artifactId>leyou</artifactId>
        <groupId>com.leyou.parent</groupId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.leyou.item</groupId>
    <artifactId>leyou-item</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <!--项目的打包方式设置为pom-->
    <packaging>pom</packaging>

</project>

5.创建leyou-item-interface

在leyou-item中new一个子module

6.填写interface的保存位置

7.创建商品微服务service模块

仍然在leyou-item中new一个子module

8.填写service的保存位置

9.在pom文件中添加依赖

<?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">
    <parent>
        <artifactId>leyou-item</artifactId>
        <groupId>com.leyou.item</groupId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.leyou.item</groupId>
    <artifactId>leyou-item-service</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.leyou.item</groupId>
            <artifactId>leyou-item-interface</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>
</project>

10.新建一个本地数据库

先用你的navicat连接你本地的mysql,然后新建一个名为leyou的库

11.在service里面新建yml文件

位置如下

配置信息

server:
  port: 8081
spring:
  application:
    name: item-service
  datasource:
    url: jdbc:mysql:///leyou
    username: root
    password: root
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka
  instance:
    lease-renewal-interval-in-seconds: 5
    lease-expiration-duration-in-seconds: 15
mybatis:
  type-aliases-package: com.leyou.item.pojo

12. 在interface里面新建一个包

13.在service里面编写引导类

@SpringBootApplication
@EnableDiscoveryClient
public class LeyouItemApplication {
    public static void main(String[] args) {
        SpringApplication.run(LeyouItemApplication.class);
    }
}

14在zuul模块的yml文件中配置路由

server:
  port: 10010
spring:
  application:
    name: leyou-gateway
eureka:
    client:
      service-url:
        defaultZone: http://localhost:10086/eureka
      registry-fetch-interval-seconds: 5
zuul:
  prefix: /api
  routes:
    item-service: /item/** #路由到商品的微服务

15.添加actuator依赖进行服务监控

进入service的pom文件中

完善依赖

<?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">
    <parent>
        <artifactId>leyou-item</artifactId>
        <groupId>com.leyou.item</groupId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.leyou.item</groupId>
    <artifactId>leyou-item-service</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.leyou.item</groupId>
            <artifactId>leyou-item-interface</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>
</project>

16.测试"商品"微服务的启动

把三个服务都运行起来,可以看到服务列表中三个服务都显示成功了

我们再点击ITEM最右面的绿色连接地址,如果点进去没有报错就说明商品微服务项目可以正常访问

如上图显示即代表没有问题

创建工具类模块

1.在父工程中新建一个module

2.添加工程描述

3.填写位置信息

新建成功之后的图示

至此整个springcloud的后台环境搭建完成,下一篇博客解说项目结构

发布了308 篇原创文章 · 获赞 157 · 访问量 14万+

猜你喜欢

转载自blog.csdn.net/Delicious_Life/article/details/104146157