微服务Eureka和Zuul的配置

Eureka注册中心

  1. 导入的依赖
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
  1. 注册中心application.yml的配置
spring:
  application:
    name: hrm-eureka
server:
  port: 1010
eureka:
  instance:
    hostname: localhost
  client: #配置eureka客户端连接当前eureka服务时的访问地址
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #单机配置
  1. 配置启动类
package cn.zxq;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/*注册中心启动类*/
@SpringBootApplication
@EnableEurekaServer //开启注册中心服务端
public class EurekaServerApp1010 {
    public static void main( String[] args )
    {
        SpringApplication.run(EurekaServerApp1010.class) ;
    }
}

网关的配置

  1. 导入的依赖
    <dependencies>
        <!-- Eureka 客户端依赖 -->
        <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>

        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
    </dependencies>
  1. 网关application.yml的配置
#注册中心 , 服务的配置 ,zuul的配置(前缀,忽略服务 ,路由)
eureka:
  client:
    registry-fetch-interval-seconds: 5  #客户端去重连eureka服务端设置的重连时间,单位:秒
    serviceUrl:
      defaultZone: http://localhost:1010/eureka/
  instance:
    prefer-ip-address: true  #使用ip注册到Eureka
    instance-id: zuul-server:1020  #指定客户端实例的ID
server:
  port: 1020
spring:
  application:
    name: zuul-server  #服务名
zuul:
  prefix: "/hrm" #统一访问的前缀
  ignored-services: "*" #所有的服务都不要使用服务名的方式去访问
  routes: #配置路由。每个微服务通过zuul访问的前缀
    HRM-SYSTEM: "/system/**" #系统管理服务
    HRM-COURSE: "/course/**" #课程管理系统
    HRM-FILE: "/oss/**"  #file
  retryable: true #是否开启重试功能
  ribbon:
    eager-load.enabled: true  	# 饥饿加载
ribbon:
  MaxAutoRetries: 1 #对当前服务的重试次数
  MaxAutoRetriesNextServer: 1 #切换相同Server的次数
  OkToRetryOnAllOperations: false # 对所有的操作请求都进行重试,如post就不能重试,如果没做幂等处理,重试多次post会造成数据的多次添加或修改
  ConnectTimeout: 10000 #请求连接的超时时间
  ReadTimeout: 10000 #请求处理的超时时间
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 15000
            #如果配置ribbon的重试,hystrix的超时时间要大于ribbon的超时时间
  1. 配置启动类
package cn.zxq;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableZuulProxy  //开启网关配置
@EnableEurekaClient  //开启注册中心服务
public class ZuulServerApp1020 {
    public static void main(String[] args) {
        SpringApplication.run(ZuulServerApp1020.class) ;
    }
}

3.1. config–配置网关swagger-ui

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("源码人力资源系统")
                .description("源码人力资源接口文档说明")
                .termsOfServiceUrl("http://localhost:1020")
                .contact(new Contact("qi'ge", "", "254666062@qq.com"))
                .version("1.0")
                .build();
    }
}

3.2. config–配置网关

package cn.zxq.config;

import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.ArrayList;
import java.util.List;

@Component
@Primary
public class DocumentationConfig implements SwaggerResourcesProvider {
    @Override
    public List<SwaggerResource> get() {
       List resources = new ArrayList<>();
       resources.add(swaggerResource("系统管理", "/hrm/system/v2/api-docs", "2.0"));

        resources.add(swaggerResource("课程管理", "/hrm/course/v2/api-docs", "2.0"));
       return resources;
    }

    private SwaggerResource swaggerResource(String name, String location, String version) {
        SwaggerResource swaggerResource = new SwaggerResource();
        swaggerResource.setName(name);
        swaggerResource.setLocation(location);
        swaggerResource.setSwaggerVersion(version);
        return swaggerResource;
    }
}

3.3. config–/网关上配置跨越问题

package cn.zxq.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

//跨域配置
@Configuration
public class GlobalCorsConfig {
    @Bean
    public CorsFilter corsFilter() {
        //1.添加CORS配置信息
        CorsConfiguration config = new CorsConfiguration();
        //1) 允许的域,不要写*,否则cookie就无法使用了
        config.addAllowedOrigin("http://localhost:6001");
        config.addAllowedOrigin("http://127.0.0.1:6001");
        //2) 是否发送Cookie信息
        config.setAllowCredentials(true);
        //3) 允许的请求方式
        config.addAllowedMethod("OPTIONS");
        config.addAllowedMethod("HEAD");
        config.addAllowedMethod("GET");
        config.addAllowedMethod("PUT");
        config.addAllowedMethod("POST");
        config.addAllowedMethod("DELETE");
        config.addAllowedMethod("PATCH");
        // 4)允许的头信息
        config.addAllowedHeader("*");
        //2.添加映射路径,我们拦截一切请求
        UrlBasedCorsConfigurationSource configSource = new
                UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration("/**", config);
        //3.返回新的CorsFilter.
        return new CorsFilter(configSource);
    }
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值