Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能
首先创建spring-cloud文件夹,复制上个文章中的项目并修改名称为microservice-consumer-movie、microservice-provider-user(具体的代码不在这里详写了,请到https://blog.csdn.net/oGuiGui12345/article/details/89915712查看)。
然后创建SpringBoot项目microservice-discovery-eureka,并放到spring-cloud文件夹下。
创建pom.xml并放到spring-cloud文件夹下。
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
com.itmuch.cloud
microservice-spring-cloud
0.0.1-SNAPSHOT
pom
1.8
microservice-consumer-movie
microservice-provider-user
microservice-discovery-eureka
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-dependencies
2.1.4.RELEASE
pom
import
org.springframework.cloud
spring-cloud-dependencies
Greenwich.RELEASE
pom
import
org.springframework.boot
spring-boot-maven-plugin
然后导入spring-cloud目录下的3个项目
点击下图中的右上角的小三角,选择Projects Presentation-->Hierarchical, 显示效果如下图
修改pom.xml文件
4.0.0
com.itmuch.cloud
microservice-spring-cloud
0.0.1-SNAPSHOT
microservice-discovery-eureka
jar
UTF-8
org.springframework.boot
spring-boot-starter-security
修改application.yml
spring:
security:
user:
name: user
password: password123
server:
port: 8761
tomcat:
uri-encoding: UTF-8
max-http-header-size: 8096
eureka:
client:
registerWithEureka: false
fetchRegistry: false
service-url:
defaultZone: http://user:password123@localhost:8761/eureka
创建配置类WebSecurityConfig
Spring Cloud 2.0 以上的security默认启用了csrf检验,要在eurekaServer端配置security的csrf检验为false,覆盖父类的 configure(HttpSecurity http) 方法,关闭掉 csrf
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
/**
* Spring Cloud 2.0 以上的security默认启用了csrf检验
* 要在eurekaServer端配置security的csrf检验为false
* 覆盖父类的 configure(HttpSecurity http) 方法,关闭掉 csrf
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
super.configure(http);
}
}
修改启动类EurekaApplication,增加@EnableEurekaServer注解开启服务发现服务
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main( String[] args ){
SpringApplication.run(EurekaApplication.class, args);
}
}
修改pom.xml
4.0.0
com.itmuch.cloud
microservice-spring-cloud
0.0.1-SNAPSHOT
microservice-provider-user
jar
1.8
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-web
com.h2database
h2
runtime
修改application.yml
server:
port: 7900
spring:
jpa:
generate-ddl: false
show-sql: true
hibernate:
ddl-auto: none
datasource:
platform: h2
schema: classpath:schema.sql
data: classpath:data.sql
application:
name: microservice-provider-user
eureka:
client:
serviceUrl:
defaultZone: http://user:password123@localhost:8761/eureka
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
修改启动类,增加@EnableEurekaClient注解表示开启Eureka客户端
@SpringBootApplication
@EnableEurekaClient
public class MicroserviceSimpleProviderUserApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceSimpleProviderUserApplication.class, args);
}
}
修改pom.xml
4.0.0
microservice-consumer-movie
jar
com.itmuch.cloud
microservice-spring-cloud
0.0.1-SNAPSHOT
1.8
org.springframework.boot
spring-boot-starter-web
修改启动类,加入@EnableEurekaClient注解开启Eureka客户端服务
@SpringBootApplication
@EnableEurekaClient
public class MicroserviceSimpleConsumerMovieApplication {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(MicroserviceSimpleConsumerMovieApplication.class, args);
}
}
修改application.yml
spring:
application:
name: microservice-consumer-movie
server:
port: 7901
user:
userServicePath: http://localhost:7900/simple/
eureka:
client:
serviceUrl:
defaultZone: http://user:password123@localhost:8761/eureka
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
首先启动microservice-discovery-eureka 再启动microservice-consumer-movie 和microservice-provider-user
输入http://localhost:8761/ 输入用户名和密码正确后,展示Eureka首页。
Instances currently registered with Eureka中已显示注册了2个微服务项目分别是microservice-consumer-movie 和microservice-provider-user,服务发起搭建完成