eureka server
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-security</artifactId> </dependency> </dependencies>
@SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class,args); } /** * 落地 Spring Could 所踩过的坑(一)eureka开启验证后无法连接注册中心 * 查资料了解到新版(Spring Cloud 2.0 以上)的security默认启用了csrf检验,要在eurekaServer端配置security的csrf检验为false */ @EnableWebSecurity static class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable(); super.configure(http); } } }
server: port: 7001 spring: application: name: eureka-${server.port} security: user: name: yangzhengxing #定义用户名 password: yangzhengxing #定义密码 eureka: instance: hostname: localhost client: fetch-registry: false #由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己 register-with-eureka: false #由于注册中心的职责就是维护服务示例,它并不需要去检索服务,所以也设置为false service-url: defaultZone: http://yangzhengxing:yangzhengxing@${eureka.instance.hostname}:${server.port}/eureka/ server: enable-self-preservation: false #关闭保护机制 eviction-interval-timer-in-ms: 5000 #默认60000L=60秒剔除
eureka client+zuul
<!--eureka client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--zuul网关--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency>
@SpringBootApplication @EnableDiscoveryClient @EnableZuulProxy public class ZuulApplication { public static void main(String[] args) { SpringApplication.run(ZuulApplication.class,args); } // 开发时候配置跨域,现在使用vue-cli 自己的代理解决,生成部署,是用Nginx 代理 @Bean public CorsFilter corsFilter() { final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); final CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.setAllowCredentials(true); corsConfiguration.addAllowedHeader("*"); corsConfiguration.addAllowedOrigin("*"); corsConfiguration.addAllowedMethod("*"); source.registerCorsConfiguration("/**", corsConfiguration); return new CorsFilter(source); } }
server: port: 7002 spring: application: name: zuul-${server.port} eureka: instance: lease-renewal-interval-in-seconds: 5 # ,表示eureka client发送心跳给server端的频率。如果在leaseExpirationDurationInSeconds后,server端没有收到client的心跳,则将摘除该instance。除此之外,如果该instance实现了HealthCheckCallback,并决定让自己unavailable的话,则该instance也不会接收到流量。默认30秒 lease-expiration-duration-in-seconds: 20 #表示eureka server至上一次收到client的心跳之后,等待下一次心跳的超时时间,在这个时间内若没收到下一次心跳,则将移除该,默认为90秒,如果该值太大,则很可能将流量转发过去的时候,该instance已经不存活了。如果该值设置太小了,则instance则很可能因为临时的网络抖动而被摘除掉。该值至少应该大于leaseRenewalIntervalInSeconds instance-id: ${spring.application.name} prefer-ip-address: true client: service-url: defaultZone: http://yangzhengxing:yangzhengxing@localhost:7001/eureka register-with-eureka: true zuul: routes: auth: path: /auth/** serviceId: auth stripPrefix: true sensitiveHeaders: user: path: /user/** serviceId: user stripPrefix: true sensitiveHeaders:
中途出现问题参考网址:
https://my.oschina.net/u/3962987/blog/3035689
https://www.jianshu.com/p/a40219751264