spring-cloud学习之Eureka注册中心
文章目录
1 springcloud和dubbo的区别
1 传输协议
dubbo由于是二进制的传输,占用带宽会更少
springCloud是http协议传输,带宽会比较多,同时使用http协议一般会使用JSON报文,消耗会更大
2 开发特性
dubbo的开发难度较大,原因是dubbo的jar包依赖问题很多大型工程无法解决
springcloud的接口协议约定比较自由且松散,需要有强有力的行政措施来限制接口无序升级
3 注册中心
dubbo的注册中心可以选择zk,redis等多种,springcloud的注册中心主要是使用Eureka或者自研
2 Eureka-server注册中心搭建
2.1 介绍
Eureka是Netflix开源的服务发现组件,本身是一个基于rest的服务。他包含server和Client两个部分。SpringCloud将它集成在子项目SpringCloudNetflix中,从而实现微服务的注册和发现
2.2 pom介绍
在这里使用的版本是 springboot 2.1.6和springcloud 使用的是Greenwich.SR1
核心pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
整体pom
<?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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cloud</groupId>
<artifactId>eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<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>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.3 配置启动类
使用eureka非常简单是需要在启动类上加上一个注解
@EnableEurekaServer 标明这个服务是一个eureka服务就可以了
2.4 配置yaml文件
spring:
application:
name: eureka-server
server:
port: 8761
eureka:
client:
fetch-registry: false # 关闭自身服务注册到eureka
register-with-eureka: false # 关闭自身服务注册到eureka
serviceUrl:
defaultZone: http://localhost:8761/eureka/ #设置与eureka交互的地址
2.5 测试
启动服务
输入 http://localhost:8761
到目前为止eureka搭建完成
3 服务提供者Eureka-client的搭建
3.1 pom
核心pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
整体pom
<?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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cloud</groupId>
<artifactId>provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>provider</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.2 启动类配置
这里需要使用到一个注解 @EnableEurekaClient
标明这个服务是eureka-client 让eureka扫描到
3.3 yaml
spring:
application:
name: eureka-client
server:
port: 9002
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ #配置eureka服务地址
3.4 测试
在eureka控制页面可以看到
表示已经注册上了
4 eureka-server添加安全认证-登入
如果项目需要为eureka-server添加安全认证 这里使用的是spring的security
只需要改造 eureka-server项目添加
4.1 pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
4.2 yaml
spring:
application:
name: eureka-server
security: # 安全拦截
user:
name: root # 登入账号
password: root # 登入密码
server:
port: 8761
eureka:
client:
fetch-registry: false # 关闭自身服务注册到eureka
register-with-eureka: false # 关闭自身服务注册到eureka
serviceUrl:
defaultZone: http://root:root@localhost:8761/eureka/ #设置与eureka交互的地址
需要注意的是 如果添加了安全认证 那么 交互地址需要改造
http://user:password@localhost:8761/eureka/
4.3 遇到的坑
需要注意的是如果使用springboot 2.0以上的朋友这里安全认证的时候有个坑
spring官网提供的文档并没有提及到security在新版本中添加了csrf过滤,csrf将微服务的注册也给过滤了
package com.cloud.eureka;
import org.springframework.context.annotation.Configuration;
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
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable(); // 关闭csrf
http.authorizeRequests().anyRequest().authenticated().and().httpBasic(); // 开启认证
}
}
4.4 eureka-client需要修改yaml
4.5 测试
登入后
5 源码
github : https://github.com/imqw/spring-cloud
参考文档 :
https://blog.csdn.net/u010664947/article/details/80007767
https://blog.csdn.net/lovexiaotaozi/article/details/82662109