您的位置:首页 > 其它

eureka注册中心

2019-06-26 23:28 323 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/weixin_44693130/article/details/93795454

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界面展示不出来

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: