网上关于这方便的集成很多,实际自己搭建时候,可能会有一些问题,在这总结一下,容易出错的地方,我会加特殊标记

  本集成使用springboot版本:2.3.3.RELEASE,spring cloud 版本:Hoxton.SR8,自己搭建环境时,注意去官网比对,springboot与 spring cloud版本是否匹配

  整体目录结构:

  •  首先创建一个springboot项目,然后再创建一个子目录项目,在父级pom.xml文件添加相关引用

<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>org.example</groupId>
    <artifactId>consuldemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <modules>
        <module>service-provider</module>
    </modules>
    <packaging>pom</packaging>

    <name>consuldemo</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.boot.version>2.3.3.RELEASE</spring.boot.version>
        <spring-cloud.version>Hoxton.SR8</spring-cloud.version>
    </properties>

    <!--继承spring-boot-starter-parent依赖-->
    <!--使用继承方式,实现复用,符合继承的都可以被使用-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
    </parent>

    <!--项目依赖管理 父项目只是声明依赖,不会实际引入jar,子项目需要写明需要的依赖(可以省略版本信息)-->
    <dependencyManagement>
        <dependencies>
            <!--spring-cloud依赖-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--spring boot web依赖-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>${spring.boot.version}</version>
            </dependency>

            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>
        </dependencies>

    </dependencyManagement>

</project>

View Code

 

  • 在子项目service-provider的pom.xml中添加引用

<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>consuldemo</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>service-provider</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>

    <name>service-provider</name>
    <url>http://maven.apache.org</url>

    <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>
    </properties>

    <dependencies>

        <!--Consul依赖,服务发现-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

        <!--spring boot actuator依赖,健康监测-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--spring boot web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--lombok依赖,构建实体类-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

</project>

View Code

 

  • 在子项目service-provider,创建resource文件夹,同时创建application.yml,注意名字默认不能修改并且标记resource目录为如图所标格式,否则报找不到配置文件里变更的,各种问题


server:
  port: 7070 # 端口
spring:
  application:
    name: service-provider # 应用名称
    # 配置Consul注册中心
  cloud:
    consul:
      # 注册中心的访问地址
      host: localhost
      port: 8500
      # 服务提供者信息
      discovery:
        register: true # 是否需要注册
        instance-id: ${spring.application.name}-01 # 注册实例id(必须唯一)
        service-name: ${spring.application.name} # 服务名称
        port: ${server.port} # 服务端口
        prefer-ip-address: true #是否使用ip地址注册
        ip-address: ${spring.cloud.client.ip-address} # 服务请求ip

View Code

  • 服务层IProductService接口,关键代码:

package org.example.service;

import org.example.entity.Product;

import java.util.List;

public interface IProductService {
    /**
     * 查询商品列表
     * @return
     */
    List<Product> selectProductList();
}

View Code

  • 服务层ProductServiceImpl接口,关键代码:

package org.example.service;

import org.example.entity.Product;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class ProductServiceImpl implements  IProductService {

    public List<Product> selectProductList() {

        return Arrays.asList(
                new Product(1,"华为手机",1,5800D),
                new Product(2,"联想笔记本",1,6888D),
                new Product(3,"小米平板",5,2020D)
        );
    }
}

View Code

  • 基础层,实体类关键代码:

package org.example.entity;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.context.annotation.Bean;

import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Product implements Serializable {
    private Integer id;
    private String productName;
    private Integer productNum;
    private Double productPrice;
}

View Code

  • 控制器,关键代码:

package org.example.controller;

import org.example.entity.Product;
import org.example.service.IProductService;
import org.example.service.ProductServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/prduct")
public class ProductController {
    @Autowired
    private IProductService IproductService;

    /**
     * 查询商品列表
     * @return
     */
    @GetMapping("/list")
    public List<Product> selectProductList(){
        return IproductService.selectProductList();
    }


}

View Code

  • 启动类

package org.example;

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

/**
 * Hello world!
 *
 */
@EnableDiscoveryClient
@SpringBootApplication
public class ServiceProviderApp
{
    public static void main( String[] args )
    {
        SpringApplication.run(ServiceProviderApp.class, args);

    }
}

View Code

 

 

 

  

原文地址:http://www.cnblogs.com/personblog/p/16820973.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性