SpringCloud--Eureka就是这么简单

Eureka 学习

本文章
Springboot 版本号: 2.0.2.RELEASE
Springcloud版本号: Finchley.RELEASE

1. 创建一个maven项目

hayes-spring-cloud

  1. pom.xml
<parent>
  <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
 <modules>
</modules>
<properties>
    <java.version>1.8</java.version>
    <spring.boot.version>2.0.2.RELEASE</spring.boot.version>
    <spring.cloud.version>Finchley.RELEASE</spring.cloud.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring.cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.53</version>
        </dependency>
</dependencies>

2. 创建module

hayes-spring-cloud-eureka

  1. pom.xml
<parent>
    <groupId>com.hayes.base</groupId>
    <artifactId>hayes-spring-cloud-</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--引入eureka-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    <!--引入security 加密-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
</dependencies>


  1. application.yml
server:
  port: 1111

spring:
  security:
    user:
      name: wolf
      password: cloud

eureka:
  instance:
    hostname: localhost
    # 使用主机名来定义注册中心的地址, 也可以使用IP地址的形式
    prefer-ip-address: true
    # 上一次收到client的心跳之后,等待下一次心跳的超时时间
    lease-expiration-duration-in-seconds: 90
    # 发送心跳给server端的频率 默认30
    lease-renewal-interval-in-seconds: 20
  server:
    # 设为false,关闭自我保护
    enable-self-preservation: false
    # 清理无效节点的时间间隔
    eviction-interval-timer-in-ms: 60000
  client:
    # 不注册自己
    register-with-eureka: false
    # 注册中心的职责就是维护服务实例, 它并不需要去检索服务
    fetch-registry: false
    # 间隔多久去拉取服务注册信息 默认30
    registry-fetch-interval-seconds: 20
    service-url.defaultZone: http://wolf:cloud@${eureka.instance.hostname}:${server.port}/eureka/
    # service-url.defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  1. EurekaApplication.class
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
    public static void main(String[] args) {

        SpringApplication.run(EurekaApplication.class,args);

    }

    @EnableWebSecurity
    @Configuration
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable(); //关闭csrf
            http.authorizeRequests().anyRequest().authenticated().and().httpBasic(); //开启认证
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值