SpringCloud demo

Easter79
• 阅读 652

1.首先创建一个maven项目

  这个maven项目会包含springcloud相关的项目,目录结构如下图:

    本项目所有的springcloud版本为Finchley.SR2,对应的springboot的版本为2.0.7.RELEASE。

    SpringCloud demo 

2.创建一个服务,也称为注册中心:eureka-service

2.1 在该maven项目下新建一个module,eureka-service。

    即创建一个springboot项目,并添加相关依赖。步骤如下图:

    SpringCloud demo

    SpringCloud demo

2.2 application.yml配置文件

server: port: 8761spring: application:  name: eureka-server security:  user:   name: admin   password: 123eureka: instance:  hostname: localhost  #服务注册中心实例的主机名 server:  enableSelfPreservation: false  #关闭自我保护  eviction-interval-timer-in-ms: 1000  # 续期时间,即扫描失效服务的间隔时间(缺省为60*1000ms) client:  register-with-eureka: false  #是否向服务注册中心注册自己  fetch-registry: false  #是否检索服务  service-url:   defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/

2.3.pom.xml

<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-test</artifactId>        <scope>test</scope>    </dependency>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-security</artifactId>    </dependency></dependencies>

2.4 在springboot启动类加上注解@EnableEurekaServer

package com.forezp;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.config.http.SessionCreationPolicy;@EnableEurekaServer@SpringBootApplicationpublic class EurekaServerApplication {    public static void main(String[] args) {        SpringApplication.run(EurekaServerApplication.class, args);    }}

2.5 启动项目。访问localhost:8761

     可以看到没有消费者注册。SpringCloud demo

3.创建客户端,并在服务端注册了,也称为生产者:eureke-client

3.1 application.yml配置文件

server:
  port: 8762
spring:
  application:
    name: provider
eureka:
  instance:
    lease-renewal-interval-in-seconds: 5     #心跳时间,即服务续约间隔时间(缺省为30s)
    lease-expiration-duration-in-seconds: 15  #发呆时间,即服务续约到期时间(缺省为90s)
  client:
    healthcheck:
      enabled: true  #健康检查,配合pring-boot-starter-actuator依赖使用
    service-url:
      defaultZone: http://admin:123@localhost:8761/eureka/  #服务注册中心的配置内容,指定服务注册中心的位置

3.2 pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

3.3 在springboot启动类加上注解@EnableEurekaClient

package com.transsion;

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


@EnableEurekaClient
@SpringBootApplication
public class ProvideApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProvideApplication.class, args);
    }

}

3.4 增加测试的controller类

package com.transsion;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @RequestMapping("/test")
    public String test(){
        return System.currentTimeMillis() + ":provider一号";
    }

}

3.5 继续访问localhost:8761

    可以看到已经注册了一个客户端了。

SpringCloud demo

3.6 注解方式:@EnableEurekaClient、@EnableDiscoveryClient

    @EnableEurekaClient和@EnableDiscoveryClient的区别:

      spring cloud中discovery service有许多种实现(eureka、consul、zookeeper等等),@EnableDiscoveryClient基于spring-cloud-commons,

      @EnableEurekaClient基于spring-cloud-netflix。就是如果选用的注册中心是eureka,那么就推荐@EnableEurekaClient,如果是其他的注册中心,

      那么推荐使用@EnableDiscoveryClient

3.7 自我保护机制

    自我保护模式打开时,已关停节点是会一直显示在 Eureka 首页的关闭自我保护模式后,由于其默认的心跳周期比较长等原因,
    要过一会儿才会发现已关停节点被自动踢出了,若想尽快的及时踢出,那就只有修改默认的心跳周期参数了。在注册中
    心和client添加如下配置后,关闭eureka-client,注册中心就会很快删除节点。

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Easter79 Easter79
2年前
SpringBoot自定义序列化的使用方式
场景及需求:项目接入了SpringBoot开发,现在需求是服务端接口返回的字段如果为空,那么自动转为空字符串。例如:\    {        "id":1,        "name":null    },    {        "id":2,        "name":"x
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Stella981 Stella981
2年前
SpringBoot自定义序列化的使用方式
场景及需求:项目接入了SpringBoot开发,现在需求是服务端接口返回的字段如果为空,那么自动转为空字符串。例如:\    {        "id":1,        "name":null    },    {        "id":2,        "name":"x
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k