spring-cloud(1)服务发现eureka
eureka是Netflix提供的一个基于rest风格的服务,提供服务发现功能(图片来源网上)使用eureka可以方便快捷的整合一个springcloud微服务架构
首先创建一个maven项目
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.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>xyz.wangfan</groupId> <artifactId>eureke-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureke-server</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>
resource文件:新建application.properties文件。当然也可以使用yml格式文件,添加基本的配置
###端口号 server.port=9000 #表示表示是否从EurekaServer获取注册信息,默认为true。单节点不需要同步其他的EurekaServer节点的数据 eureka.client.fetch-registry=false #表示是否将自己注册在EurekaServer上,默认为true。由于当前应用就是EurekaServer,所以置为false eureka.client.register-with-eureka=false #eureka用户名 spring.security.user.name=admin #eureka密码 spring.security.user.password=admin #设置Eureka的地址 eureka.client.service-url.defaultZone:http://localhost:9000/eureka
创建一个启动的java文件,
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.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @SpringBootApplication @EnableEurekaServer public class EurekeServerApplication { public static void main(String[] args) { SpringApplication.run(EurekeServerApplication.class, args); } @EnableWebSecurity static class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable(); super.configure(http); } } }
执行程序就可以了。输入本地网址:http://localhost:9000

输入上面配置文件里面的用户名,密码就可以登录进去,如果上面没有设置用户名密码,则这一步就会省略,直接进入下面的额网页。

因为这里的项目使用的是最新的版本,所有在写法上和之前的有些区别,具体体现在:启动类中的这个注解。这段代码主要是为了解决eureka客户端注册服务的时候通过用户名密码,无法注册到eureka上。
CSRF在SpringSecurity中默认是启动的,客户端的请求必须POST请求。当时我们的eureka客户端的连接方式一般为
eureka.client.service-url.defaultZone:http://admin:admin@localhost:9000/eureka
所以这里我们需要手动的吧csrf关闭。否则客户端注册的时候会提示验证失败(当然如果上面的eureka配置里面没有配置用户名密码,这一段代码则可以不用,客户端的连接行业不需要填写用户名密码)
@EnableWebSecurity static class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable(); super.configure(http); } }
本文章只为了简单的讲述如何启动一个eureka服务,更多的功能后续的介绍中逐渐增加。