eureka注册中心

Eureka 是 Netflix 出品的用于实现服务注册和发现的工具。 Spring Cloud 集成了 Eureka,并提供了开箱即用的支持。其中, Eureka 又可细分为 Eureka Server 和 Eureka Client。


Eureka Server是提供服务注册服务,Eureka Client是一个java客户端,用于简化与Eureka Server的交互


首先新建一个文件夹然后在里面创建模型,创建一个客户端,导入相应的依赖


Lombok,Spring Web Starter,MySQL Driver,Spring Boot Actuator,Eureka Discovery Client,OpenFeign,Hystrix,Hystrix Dashboard

        <!--springboot监控依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--springboot安全框架权限依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <!--mvc整理-->
        <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-client</artifactId>
        </dependency>
        <!--服务熔断-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

        <!--服务熔断监控监控多少次成功与失败-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
        <!--服务之间相互调用的依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--mysql数据库-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--entity注解-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--单元测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

yml中进行配置

server:
  port: 8082 #启动端口
spring:
  application:
    name: my-business #项目名字
  datasource:
    username: root #链接账号
    password: root  #连接密码
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/usertype?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8 #路径
eureka:
  client:
    service-url:
      defaultZone: http://g01:123456@server1:8081/eureka/ #eureka web注册地址
  instance:
    prefer-ip-address: true   #是否显示ip
    instance-id: my_business8082  #实例id一般是应用名加上端口号

还需要在主程序类上加上@EnableEurekaClient当然这个也可以不加,建议写上


创建一个服务端项目:


只需要导入Eureka Server,也可以导入security安全框架权限如果导入了security依赖yml中必须设置账号密码还需要配置一个config配置

config配置
package com.bdqn.config;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().ignoringAntMatchers("/eureka/**");
        super.configure(http);
    }
}

yml配置
server:
  port: 8081 #启动端口号
spring:
  application:
    name: eureka-server #服务名称,自己不用给自己注册所以不显示
  security:
    user:     #增加用户账号密码的,如果不增加配置类,服务会注册不上去
      name: g01 #eureka可视化登录账号
      password: 123456 #eureka可视化登录密码
eureka:
  server:
    enable-self-preservation: false #eureka自我保护机制,生产模式下建议开启,测试模式下可以关闭
  instance:
    hostname: server1 #注册实例
    prefer-ip-address: true #是否显示ip
  client:
    fetch-registry: false #是否从服务端拉取注册列表
    register-with-eureka: false  #是否向eureka注册
    service-url:
      defaultZone: http://server2:8083/eureka/  #eureka web注册地址

这个主程序中必须加@EnableEurekaServer注解,如若不加 eureka前台web界面展示不出来

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值