springboot2.0+spring cloud+eureka

springboot2.0+spring cloud+eureka(分布式项目)项目搭建详细教程

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/hp_yangpeng/article/details/88803911

重点:本项目资源地址请点击:https://download.csdn.net/download/hp_yangpeng/11064773(ps:最好先看文档,跟着做完,然后再下载demo)

1、相关环境

  1. 开发工具:idea;
  2. springboot版本:2.1.13
  3. springcloud版本:Finchley.SR1(注意,此处使用的是springboot2.0.x以上的版本,而springcloud对应的版本为Finchley,且springboot2.0相比于springboot1.5.x来说,maven依赖变化较大,这个问题在搭建分布式项目时我会做出说明)
  4. 中间件:eureka、fegin(其他中间件比如hystrix、ribbon等后续再添加);

2、项目创建
说明:此处我们会创建一个父项目,其他子项目(生产者、消费者、注册中心)均以module的形式在展示在项目目录中,首先比较符合当前开发规范,其次也比较方便;
1. 创建父项目:springbootdemo,具体截图如下:
选择spring Initializr注意:此处可以选择一个web依赖,一个Eureka Server依赖
在这里插入图片描述
在Idea里我将无关的文件全部隐藏了,另外将test给删除了,具体文件目录如下:
在这里插入图片描述
pom.xml 做个修改,具体pom.xml配置如下::
说明:在创建当前项目的时候选择了springcloud的eureka,但是创建结束之后我发现springcloud的版本号为<spring-cloud.version>Greenwich.SR1</spring-cloud.version> 这个我没有细查,所以就直接用下面的配置文件吧,保证是没有问题的;

  <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- springboot 1.5.X eureka依赖 -->
        <!--<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>-->
        <!--springboot 2.X eureka 依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
&lt;build&gt;
    &lt;plugins&gt;
        &lt;plugin&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
        &lt;/plugin&gt;
    &lt;/plugins&gt;
&lt;/build&gt;

&lt;dependencyManagement&gt;
    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
            &lt;artifactId&gt;spring-cloud-dependencies&lt;/artifactId&gt;
            &lt;version&gt;Finchley.SR1&lt;/version&gt;
            &lt;type&gt;pom&lt;/type&gt;
            &lt;scope&gt;import&lt;/scope&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
&lt;/dependencyManagement&gt;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

父项目创建完毕;
2.创建子项目——公共entity项目 springbootdemo-entity
项目结构如下:entity子项目中主要有两个entity,包名为:com.example.springbootdemoentity.entity
entity中的字段自己随便写,我的内容如下:

public class Consumer {
private String name;
private int age;
private String add;
private String email;

public Consumer() {
    this.name = "name";
    this.age = 12;
    this.add = "北京市历史互通";
    this.email = "6666.qq.com";
}

@Override
public String toString() {
    return "Consumer{" +
            "name='" + name + '\'' +
            ", age=" + age +
            ", add='" + add + '\'' +
            ", email='" + email + '\'' +
            '}';
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
/**
 * @author YangPeng
 * @Title: Product
 * @ProjectName springboot
 * @Description: TODO
 * @date 2019/3/25-16:23
 */
public class Product {
private String name;
private int age;
private String add;
private String email;

public Product() {
    this.name = "name";
    this.age = 12;
    this.add = "北京市历史互通";
    this.email = "6666.qq.com";
}

@Override
public String toString() {
    return "Product{" +
            "name='" + name + '\'' +
            ", age=" + age +
            ", add='" + add + '\'' +
            ", email='" + email + '\'' +
            '}';
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

entity子项目创建结束!!!
在这里插入图片描述

3.创建子项目——注册中心 springbootdemo-eureka
项目目录如下:
在这里插入图片描述

  • 修改pom.xml,添加eureka依赖,此处需要注意的是要添加springcloud的依赖管理,版本号为Finchley.SR1
<dependencies>
        <!-- springboot2.0.*对应的eureka依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <!-- springboot 2.0.* 对应的springcloud依赖管理 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 修改application.properties为application.yml,并添加如下配置:
server:
  port: 5060
  servlet:
    context-path: /eureka
spring:
  application:
    name: eureka-server
eureka:
  client:
    #是否启用湖区注册服务信息,因为这是一个单节点的EurekaServer,不需要同步其他的EurekaServer节点的数据,所以设置为false;
    fetch-registry: false
    #表示是否向eureka注册服务,即在自己的eureka中注册自己,默认为true,此处应该设置为false;
    register-with-eureka: true
    service-url:
      defaultZone:  http://localhost:5060/eureka/eureka
  instance:
    hostname: localhost
  server:
    #设为false,关闭自我保护,即Eureka server在云心光器件会去统计心跳失败比例在15分钟之内是否低于85%,如果低于85%,EurekaServer
    #会将这些事例保护起来,让这些事例不会过期,但是在保护器内如果刚哈这个服务提供者非正常下线了,此时服务消费者会拿到一个无效的服务
    #实例,此时调用会失败,对于这个问题需要服务消费者端有一些容错机制,如重试、断路器等;
    enable-self-preservation: false
    #扫描失效服务的间隔时间(单位是毫秒,摩恩是60*1000),即60s
    eviction-interval-timer-in-ms: 10000

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 修改启动文件,添加@EnableEurekaServer注解,表示这个是一个Eureka注册中心
    在这里插入图片描述启动eureka项目,并访问:http://localhost:5060/eureka ,出现如下页面表示Eureka项目创建成功!
    在这里插入图片描述
    4.创建子项目——生产者 springbootdemo-product
    在这里插入图片描述说明:此处删除无用的test文件夹,并将application.properties改为application.yml;
    pom.xml文件修改:

  • 修改该工程的父依赖:
    原来为:

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

修改为:

  <parent>
        <groupId>com.example</groupId>
        <artifactId>springbootdemo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> 
    </parent>

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

具体配置如下:

<?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>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>springbootdemo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>springbootdemo-product</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springbootdemo-product</name>
    <description>Demo project for Spring Boot</description>
&lt;properties&gt;
    &lt;java.version&gt;1.8&lt;/java.version&gt;
&lt;/properties&gt;

&lt;dependencies&gt;

<!-- 将entity依赖添加到product工程中 -->
<dependency>
<groupId>com.example</groupId>
<artifactId>springbootdemo-entity</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<!-- 添加springboot fegin依赖,product项目即可以作为生产者,又可以作为消费者–>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 修改resources下的application.properties为application.yml并添加如下配置:
spring:
  application:
    name: product-server
server:
  port: 7002
  servlet:
    context-path: /product
eureka:
  client:
    service-url:
    #defaultZone 这个是不会提示的,此处需要自己写
    #实际上属性应该是service-url,这个属性是个map(key-value)格式;当key是defaultZone的时候才能被解析;所以这里没有提示,
    #但是自己还需要写一个defaultZone;
      defaultZone: http://localhost:5060/eureka/eureka

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 创建接口Controller
    在这里插入图片描述
  • 启动类添加注解:添加@EnableEurekaClient和EnableFeginClients注解
@SpringBootApplication
//@EnableEurekaClient 和 @EnableDiscoveryClient 都是让eureka发现该服务并注册到eureka上的注解
//相同点:都能让注册中心Eureka发现,并将该服务注册到注册中心上;
//不同点:@EnableEurekaClient只适用于Eureka作为注册中心,而@EnableDiscoveryClient可以是其他注册中心;
@EnableEurekaClient
//表示开启Fegin客户端
@EnableFeignClients
public class SpringbootProductApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootProductApplication.class, args);
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

5.创建子项目——消费者 springbootdemo-cousumer

  • module的创建过程同product工程的创建过程,此处就不细写了,主要写一下相关的配置文件;
  • pom.xml文件修改:
    • 修改parent父依赖为:
<parent>
        <groupId>com.example</groupId>
        <artifactId>springbootdemo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
    • 添加依赖如下:
 <!-- 公共entity依赖 -->
 <dependency>
            <groupId>com.example</groupId>
            <artifactId>springbootdemo-entity</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
        <!-- fegin客户端依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <build>
    &lt;plugins&gt;
        &lt;plugin&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
        &lt;/plugin&gt;
    &lt;/plugins&gt;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 修改application.properties 为application.yml并添加如下配置:

server:
  servlet:
  #  定义项目的访问访问路径
    context-path: /consumer
    #定义端口号
  port: 7001
spring:
#  下面是我整合redis使用的配置,你们此处不需要
#  redis:
#    cluster:
#      expire-seconds: 120
#      command-timeout: 5000
#      nodes:   
#  namenode22:6379,datanode23:6379,datanode24:6379,datanode25:6379,datanode26:6379,datanode27:6379
  application:
  #定义应用名称,即服务名称
    name: consumer-server
eureka:
  client:
    service-url:
      defaultZone: http://localhost:5060/eureka/eureka

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 启动类添加注解:@EnableEurekaClient和@EnableEurekaClient
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class SpringbootdemoConsumerApplication {
public static void main(String[] args) {
    SpringApplication.run(SpringbootdemoConsumerApplication.class, args);
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

- 整合fegin步骤:

    • 第一步:启动类中添加注解:@EnableFeignClients,表示开启fegin客户端(该操作我们已经在上面搞定)
    • 第二步:创建service接口,在接口的类名称上指明服务名称(application name)和服务的应用名称(contest-path),并在接口中添加方法,方法名上添加 @RequestMapping注解,注解的value就是你要访问的方法的路径;并写明返回值和参数(如果有参数的话,需要使用@RequestParam标注参数名称),具体如下:

下图为我服务的接口相关信息:
fegin调用接口信息如下:

/**
 * @author YangPeng
 * @Title: ProductService
 * @ProjectName springbootdemo
 * @Description: TODO
 * @date 2019/3/27-11:23
 */
//name 为product项目中application.yml配置文件中的application.name;
//path 为product项目中application.yml配置文件中的context.path;
@FeignClient(name = "product-server",path ="/product" )
//@Componet注解最好加上,不加idea会显示有错误,但是不影响系统运行;
@Component
public interface ProductService {
    @RequestMapping(value = "getProduct")
    String getProduct();
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
    • 第三步:创建controller,注入service,并进行调用;
@RestController
public class ConsumerController {
    @Autowired
    private ProductService productService;
@RequestMapping(value = "getConsumer")
public String getConsumer(){
   String str =  productService.getProduct();
   return str;
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

重点中的重点:鉴于好多朋友的CSDN账号没有C币,所以我把项目传到github上了,哈哈哈哈,朋友们可以直接去github clone下来,地址如下:https://github.com/yanpgeng/springbootdemo 克隆下来之后直接使用idea工具open这个项目,然后将项目add as maven project即可

下一篇:springboot整合redis集群详解

                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-095d4a0b23.css" rel="stylesheet">
                </div>
</article><article class="baidu_pl">
            <div id="article_content" class="article_content clearfix">
                                            <div class="article-copyright">
            <span class="creativecommons">
            <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">
                </a>
        <span>
            版权声明:本文为博主原创文章,遵循<a href="http://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="noopener"> CC 4.0 BY-SA </a>版权协议,转载请附上原文出处链接和本声明。            </span>
           <div class="article-source-link2222">
                本文链接:<a href="https://blog.csdn.net/hp_yangpeng/article/details/88803911">https://blog.csdn.net/hp_yangpeng/article/details/88803911</a>
            </div>
        </span>
                </div>
                                                <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-3019150162.css">
                                    <div id="content_views" class="markdown_views prism-atom-one-dark">
                <!-- flowchart 箭头图标 勿删 -->
                <svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
                    <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
                </svg>
                                        <p><strong>重点:本项目资源地址请点击:<a href="https://download.csdn.net/download/hp_yangpeng/11064773" rel="nofollow" data-token="3d265df1e276d842a91cdf810f87a207">https://download.csdn.net/download/hp_yangpeng/11064773</a></strong>(ps:最好先看文档,跟着做完,然后再下载demo)</p>

1、相关环境

  1. 开发工具:idea;
  2. springboot版本:2.1.13
  3. springcloud版本:Finchley.SR1(注意,此处使用的是springboot2.0.x以上的版本,而springcloud对应的版本为Finchley,且springboot2.0相比于springboot1.5.x来说,maven依赖变化较大,这个问题在搭建分布式项目时我会做出说明)
  4. 中间件:eureka、fegin(其他中间件比如hystrix、ribbon等后续再添加);

2、项目创建
说明:此处我们会创建一个父项目,其他子项目(生产者、消费者、注册中心)均以module的形式在展示在项目目录中,首先比较符合当前开发规范,其次也比较方便;
1. 创建父项目:springbootdemo,具体截图如下:
选择spring Initializr注意:此处可以选择一个web依赖,一个Eureka Server依赖
在这里插入图片描述
在Idea里我将无关的文件全部隐藏了,另外将test给删除了,具体文件目录如下:
在这里插入图片描述
pom.xml 做个修改,具体pom.xml配置如下::
说明:在创建当前项目的时候选择了springcloud的eureka,但是创建结束之后我发现springcloud的版本号为<spring-cloud.version>Greenwich.SR1</spring-cloud.version> 这个我没有细查,所以就直接用下面的配置文件吧,保证是没有问题的;

  <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- springboot 1.5.X eureka依赖 -->
        <!--<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>-->
        <!--springboot 2.X eureka 依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
&lt;build&gt;
    &lt;plugins&gt;
        &lt;plugin&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
        &lt;/plugin&gt;
    &lt;/plugins&gt;
&lt;/build&gt;

&lt;dependencyManagement&gt;
    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
            &lt;artifactId&gt;spring-cloud-dependencies&lt;/artifactId&gt;
            &lt;version&gt;Finchley.SR1&lt;/version&gt;
            &lt;type&gt;pom&lt;/type&gt;
            &lt;scope&gt;import&lt;/scope&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
&lt;/dependencyManagement&gt;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

父项目创建完毕;
2.创建子项目——公共entity项目 springbootdemo-entity
项目结构如下:entity子项目中主要有两个entity,包名为:com.example.springbootdemoentity.entity
entity中的字段自己随便写,我的内容如下:

public class Consumer {
private String name;
private int age;
private String add;
private String email;

public Consumer() {
    this.name = "name";
    this.age = 12;
    this.add = "北京市历史互通";
    this.email = "6666.qq.com";
}

@Override
public String toString() {
    return "Consumer{" +
            "name='" + name + '\'' +
            ", age=" + age +
            ", add='" + add + '\'' +
            ", email='" + email + '\'' +
            '}';
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
/**
 * @author YangPeng
 * @Title: Product
 * @ProjectName springboot
 * @Description: TODO
 * @date 2019/3/25-16:23
 */
public class Product {
private String name;
private int age;
private String add;
private String email;

public Product() {
    this.name = "name";
    this.age = 12;
    this.add = "北京市历史互通";
    this.email = "6666.qq.com";
}

@Override
public String toString() {
    return "Product{" +
            "name='" + name + '\'' +
            ", age=" + age +
            ", add='" + add + '\'' +
            ", email='" + email + '\'' +
            '}';
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

entity子项目创建结束!!!
在这里插入图片描述

3.创建子项目——注册中心 springbootdemo-eureka
项目目录如下:
在这里插入图片描述

  • 修改pom.xml,添加eureka依赖,此处需要注意的是要添加springcloud的依赖管理,版本号为Finchley.SR1
<dependencies>
        <!-- springboot2.0.*对应的eureka依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <!-- springboot 2.0.* 对应的springcloud依赖管理 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 修改application.properties为application.yml,并添加如下配置:
server:
  port: 5060
  servlet:
    context-path: /eureka
spring:
  application:
    name: eureka-server
eureka:
  client:
    #是否启用湖区注册服务信息,因为这是一个单节点的EurekaServer,不需要同步其他的EurekaServer节点的数据,所以设置为false;
    fetch-registry: false
    #表示是否向eureka注册服务,即在自己的eureka中注册自己,默认为true,此处应该设置为false;
    register-with-eureka: true
    service-url:
      defaultZone:  http://localhost:5060/eureka/eureka
  instance:
    hostname: localhost
  server:
    #设为false,关闭自我保护,即Eureka server在云心光器件会去统计心跳失败比例在15分钟之内是否低于85%,如果低于85%,EurekaServer
    #会将这些事例保护起来,让这些事例不会过期,但是在保护器内如果刚哈这个服务提供者非正常下线了,此时服务消费者会拿到一个无效的服务
    #实例,此时调用会失败,对于这个问题需要服务消费者端有一些容错机制,如重试、断路器等;
    enable-self-preservation: false
    #扫描失效服务的间隔时间(单位是毫秒,摩恩是60*1000),即60s
    eviction-interval-timer-in-ms: 10000

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 修改启动文件,添加@EnableEurekaServer注解,表示这个是一个Eureka注册中心
    在这里插入图片描述启动eureka项目,并访问:http://localhost:5060/eureka ,出现如下页面表示Eureka项目创建成功!
    在这里插入图片描述
    4.创建子项目——生产者 springbootdemo-product
    在这里插入图片描述说明:此处删除无用的test文件夹,并将application.properties改为application.yml;
    pom.xml文件修改:

  • 修改该工程的父依赖:
    原来为:

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

修改为:

  <parent>
        <groupId>com.example</groupId>
        <artifactId>springbootdemo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> 
    </parent>

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

具体配置如下:

<?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>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>springbootdemo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>springbootdemo-product</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springbootdemo-product</name>
    <description>Demo project for Spring Boot</description>
&lt;properties&gt;
    &lt;java.version&gt;1.8&lt;/java.version&gt;
&lt;/properties&gt;

&lt;dependencies&gt;

<!-- 将entity依赖添加到product工程中 -->
<dependency>
<groupId>com.example</groupId>
<artifactId>springbootdemo-entity</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<!-- 添加springboot fegin依赖,product项目即可以作为生产者,又可以作为消费者–>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 修改resources下的application.properties为application.yml并添加如下配置:
spring:
  application:
    name: product-server
server:
  port: 7002
  servlet:
    context-path: /product
eureka:
  client:
    service-url:
    #defaultZone 这个是不会提示的,此处需要自己写
    #实际上属性应该是service-url,这个属性是个map(key-value)格式;当key是defaultZone的时候才能被解析;所以这里没有提示,
    #但是自己还需要写一个defaultZone;
      defaultZone: http://localhost:5060/eureka/eureka

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 创建接口Controller
    在这里插入图片描述
  • 启动类添加注解:添加@EnableEurekaClient和EnableFeginClients注解
@SpringBootApplication
//@EnableEurekaClient 和 @EnableDiscoveryClient 都是让eureka发现该服务并注册到eureka上的注解
//相同点:都能让注册中心Eureka发现,并将该服务注册到注册中心上;
//不同点:@EnableEurekaClient只适用于Eureka作为注册中心,而@EnableDiscoveryClient可以是其他注册中心;
@EnableEurekaClient
//表示开启Fegin客户端
@EnableFeignClients
public class SpringbootProductApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootProductApplication.class, args);
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

5.创建子项目——消费者 springbootdemo-cousumer

  • module的创建过程同product工程的创建过程,此处就不细写了,主要写一下相关的配置文件;
  • pom.xml文件修改:
    • 修改parent父依赖为:
<parent>
        <groupId>com.example</groupId>
        <artifactId>springbootdemo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
    • 添加依赖如下:
 <!-- 公共entity依赖 -->
 <dependency>
            <groupId>com.example</groupId>
            <artifactId>springbootdemo-entity</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
        <!-- fegin客户端依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <build>
    &lt;plugins&gt;
        &lt;plugin&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
        &lt;/plugin&gt;
    &lt;/plugins&gt;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 修改application.properties 为application.yml并添加如下配置:

server:
  servlet:
  #  定义项目的访问访问路径
    context-path: /consumer
    #定义端口号
  port: 7001
spring:
#  下面是我整合redis使用的配置,你们此处不需要
#  redis:
#    cluster:
#      expire-seconds: 120
#      command-timeout: 5000
#      nodes:   
#  namenode22:6379,datanode23:6379,datanode24:6379,datanode25:6379,datanode26:6379,datanode27:6379
  application:
  #定义应用名称,即服务名称
    name: consumer-server
eureka:
  client:
    service-url:
      defaultZone: http://localhost:5060/eureka/eureka

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 启动类添加注解:@EnableEurekaClient和@EnableEurekaClient
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class SpringbootdemoConsumerApplication {
public static void main(String[] args) {
    SpringApplication.run(SpringbootdemoConsumerApplication.class, args);
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

- 整合fegin步骤:

    • 第一步:启动类中添加注解:@EnableFeignClients,表示开启fegin客户端(该操作我们已经在上面搞定)
    • 第二步:创建service接口,在接口的类名称上指明服务名称(application name)和服务的应用名称(contest-path),并在接口中添加方法,方法名上添加 @RequestMapping注解,注解的value就是你要访问的方法的路径;并写明返回值和参数(如果有参数的话,需要使用@RequestParam标注参数名称),具体如下:

下图为我服务的接口相关信息:
fegin调用接口信息如下:

/**
 * @author YangPeng
 * @Title: ProductService
 * @ProjectName springbootdemo
 * @Description: TODO
 * @date 2019/3/27-11:23
 */
//name 为product项目中application.yml配置文件中的application.name;
//path 为product项目中application.yml配置文件中的context.path;
@FeignClient(name = "product-server",path ="/product" )
//@Componet注解最好加上,不加idea会显示有错误,但是不影响系统运行;
@Component
public interface ProductService {
    @RequestMapping(value = "getProduct")
    String getProduct();
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
    • 第三步:创建controller,注入service,并进行调用;
@RestController
public class ConsumerController {
    @Autowired
    private ProductService productService;
@RequestMapping(value = "getConsumer")
public String getConsumer(){
   String str =  productService.getProduct();
   return str;
}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

重点中的重点:鉴于好多朋友的CSDN账号没有C币,所以我把项目传到github上了,哈哈哈哈,朋友们可以直接去github clone下来,地址如下:https://github.com/yanpgeng/springbootdemo 克隆下来之后直接使用idea工具open这个项目,然后将项目add as maven project即可

下一篇:springboot整合redis集群详解

                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-095d4a0b23.css" rel="stylesheet">
                </div>
</article>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值