当前位置 博文首页 > u011767319的博客:spring cloud 微服务框架搭建(个人笔记)

    u011767319的博客:spring cloud 微服务框架搭建(个人笔记)

    作者:[db:作者] 时间:2021-09-22 12:51

    父工程包引入

    <properties>
           <spring-cloud-dependencies.version>Hoxton.SR3</spring-cloud-dependencies.version>
           <nacos.version>2.2.1.RELEASE</nacos.version>
           <dubbo.version>2.7.5</dubbo.version>
    </properties>
    
    <dependencyManagement>
            <dependencies>
            	<!--spring-cloud 依赖管理-->
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud-dependencies.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <!--nacos-->
                <dependency>
                    <groupId>com.alibaba.cloud</groupId>
                    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
                    <version>${nacos.version}</version>
                </dependency>
                <!--dubbo start-->
                <dependency>
                    <groupId>org.apache.dubbo</groupId>
                    <artifactId>dubbo-spring-boot-starter</artifactId>
                    <version>${dubbo.version}</version>
                </dependency>
                <!--dubbo end-->
            </dependencies>
    </dependencyManagement>
    

    naocs注册中心

    本人使用的是nacos 作为注册中心
    nacos是阿里开源的。具有可视化页面。

    子项目引包

    <dependencies>
        <!--nacos-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>
    
    

    配置文件

    spring:
      application:
        name: gateway
      cloud:
        nacos:
          discovery:
            server-addr: 127.0.0.1:8848
    

    duboo

    配置

    dubbo:
      application:
        name: ${spring.application.name}
      provider:
        timeout: 5000
      consumer:
        check: false #不检查服务是否启动
        timeout: 5000
      registry:
        address: nacos://${spring.cloud.nacos.discovery.server-addr}
        check: false #不检查注册表
      protocol:
        name: dubbo
        port: 20881
      scan:
        base-packages: com.*.*.service
    

    zuul 网关

    超时问题太烦了,感觉和spring 框架就不匹配。难受。本人已经放弃了

    怎么解决跨域问题呢,因为zuul 是基于servlet的。直接在WebMvcConfig解决就行了

    @Configuration
    @EnableWebMvc
    public class WebMvcConfig implements WebMvcConfigurer {
    	/* 解决跨域问题 */
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            // 设置允许跨域的路径
            registry.addMapping("/**")
                    //放行哪些原始域
                    .allowedOrigins("*")
                    //是否发送Cookie信息
                    .allowCredentials(true)
                    //放行哪些原始域(请求方式)
                    .allowedMethods("*")
                    //放行哪些原始域(头部信息)
                    .allowedHeaders("*")
                    // 跨域允许时间
                    .maxAge(3600);
        }
    }
    

    配置

    zuul:
      # host配置适用于routes 为url请求服务的路由方式,如果是service-id路由方式则配置ribbon
      host:
        connect-timeout-millis: 6000
        socket-timeout-millis: 6000
        max-per-route-connections: 2000
        max-total-connections: 10000
    ribbon:
      #请求处理的超时时间 下级服务响应最大时间,超出时间消费方(路由也是消费方)返回timeout
      ReadTimeout: 5000
      #ribbon请求连接的超时时间- 限制3秒内必须请求到服务,并不限制服务处理的返回时间
      ConnectTimeout: 3000
    #设置hystrix超时(Edgware版本下default配置不生效,默认超时2,建议hystrix超时时间>其他超时时间配置[如ribbon])
    hystrix:
      command:
        default:
          execution:
            isolation:
              thread:
                timeoutInMilliseconds: 5000
    

    自定义启动类配置

    /**
     * @email 867170960@qq.com
     * @author:刘俊秦
     * @date: 2019/5/16 0016
     * @time: 上午 8:51
     * @remarks:自定义配置类
     */
    @Component
    @EnableDiscoveryClient
    @EnableZuulProxy
    public class CustomConfiguration {
    
    
    }
    

    Gateway 网关

    由于是Webflux 与web框架冲突。好用

    配置

    spring:
      cloud:
        gateway:
          globalcors:
          	#跨域解决方案
            globalcors:
            corsConfigurations:
              '[/**]':
                #这里有个allowCredentials: true这个东西是设置允许访问携带cookie的,这点一定要和前端对应!
                allowCredentials: true
                #可以填写多个域名用","隔开 例如:"http://www.xiaolc.cn,https://spring.io"  "*"代表允许所有
                allowedOrigins: "*"
                allowedMethods: "*"
                allowedHeaders: "*"
         routes:
         - id: gateway
    

    如何优雅的扩展oauth2的认证方式。

    本人参考了大量的文章。后觉得使用改变原aouth2框架的情况下,扩展我们需要的图片验证码登陆。短信登陆之类的认证方式
    在原创作者的代码上进行了微量的修改,使代码更加简洁易懂。认证流程也不会被调用两次或多次重复的情况。
    (这里的代码不是全部的,我只将核心的贴出来,仅供诸君参考)

    导包

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-oauth2</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.60</version>
    </dependency>
    

    WebSecurityConfig 配置如下

    package com.ljq.oauth2.config.basic;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.http.HttpMethod;
    import org.springframework.security.authentication.AuthenticationManager;
    import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
    import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
    import org.springframework.security.crypto.password.PasswordEncoder;
    
    @Configuration
    @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Bean
        public PasswordEncoder passwordEncoder() {
            return new BCryptPasswordEncoder();
        }
    
        
    
    下一篇:没有了