springcoud eureka+gateway

重新搭建一个微服务框架,之前项目中使用的微服务框架是springcloud eureka zuul 。

CAP

CAP原则又称CAP定理,指的是在一个分布式系统中,一致性(Consistency)、可用性(Availability)、分区容错性(Partition tolerance)
对比了一下几种服务注册中心,consule是遵从CP原则,为了保持一致性,在有consule节点不可用时,将导致consule整个集群不可用。

选型

nacos可以配置AP 和CP,同时具有注册中心和配置中心功能,但是比较麻烦的是需要另外启动一个nacos-server的服务,这个代码可在github找到下载地址。这样我们就需要多部署一个nacos-server集群,感觉比较麻烦。很多人不使用eureka的原因是官方不再发布eureka新版本,eureka2闭源,但是eureka1还是在更新,只是不会有大的更新变动。可能习惯了eureka的简洁方便,这里暂时选用eureka。
spring-gateway更加贴合springcloud框架,是官方推出的网关框架,有更多异步、非阻塞的处理,更好的性能。zuul基于servlet可能更加简单,但是在性能和功能拓展上没有springgateway好用。

创建iflynote-discovery

1.pom.xml添加依赖

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
 <dependencies>

        <!-- spring cloud Eureka Server 启动器 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
            <version>1.4.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
<!--            <version>2.2.0.RELEASE</version>-->
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>


    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2.application.properties配置文件

spring.profiles.active=@spring.profiles.active@
eureka.environment=@spring.profiles.active@
eureka.data-center-info=iflynote-discovery
spring.application.name=iflynote-discovery
eureka.instance.hostname=${spring.application.name}
eureka.instance.instanceId=${spring.cloud.client.ip-address}:${server.port}
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.healthcheck.enable=true
eureka.server.peer-node-read-timeout-ms=2000
spring.security.basic.enabled=true
server.port=8080
eureka.client.service-url.defaultZone=http://admin:note1111@localhost:8080/eureka
spring.security.user.name=admin
spring.security.user.password=admin:note1111
info.name=${spring.application.name}
info.server.port=${server.port}
management.endpoints.web.base-path=/actuator
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

3.启动类加注解启动eurekaserver,添加http配置

@SpringBootApplication
@EnableEurekaServer
public class IflynoteDiscoveryApplication {

public static void main(String[] args) {
    SpringApplication.run(IflynoteDiscoveryApplication.class, args);
}

@EnableWebSecurity
static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().ignoringAntMatchers("/**");
        super.configure(http);
    }
}

}

创建iflynote-gateway

1.pom.xml

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>
    </dependencies>


    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2.application.properties

server.port=8081
spring.application.name=iflynote-gateway
eureka.instance.instanceId=${spring.cloud.client.ip-address}:${server.port}
eureka.client.service-url.defaultZone=http://admin:note1111@localhost:8080/eureka
feign.hystrix.enabled=true
eureka.instance.appname=${spring.application.name}
spring.cloud.gateway.discovery.locator.enabled=false
spring.cloud.gateway.discovery.locator.lower-case-service-id=true
#路由ID
spring.cloud.gateway.routes[0].id=iflynote-auth
#路由URI
spring.cloud.gateway.routes[0].uri=lb://iflynote-auth
#设置路由断言,代理servicerId为auth-service的路径
spring.cloud.gateway.routes[0].predicates[0]=Path=/iflynote-auth/**
#忽略前缀  http://localhost:8081/iflynote-auth/test/get --> http://localhost:8082/test/get
spring.cloud.gateway.routes[0].filters[0]=StripPrefix=1

3.启动类添加注解

@EnableEurekaClient

创建iflynote-auth项目

1.pom.xml

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
<!--        权限验证-->
        <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>


    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2.application.properties

server.port=8082
spring.application.name=iflynote-auth
eureka.instance.appname=${spring.application.name}
eureka.instance.instanceId=${spring.cloud.client.ip-address}:${server.port}
eureka.client.service-url.defaultZone=http://admin:note1111@localhost:8080/eureka

feign.hystrix.enabled=true

创建TestController

@RestController
@RequestMapping("/test")
public class TestController {
    @GetMapping("/get")
    public String get() {
        return "success";
    }

}

分别先后启动服务

iflynote-discovery 、iflynote-gateway、iflynote-auth
查看eureka界面,填入用户名密码,进入页面
{O`X2TP@XPAU%DAV3J0QDZ9.png
可以看到注册到注册中心的服务列表

测试网关转发

http直连iflynote-auth的接口,http://localhost:8082/test/get ,返回success
使用网关转发的方式请求这个接口,请求地址: http://localhost:8081/iflynote-auth/test/get
同样放回正确结果,这就是网关将/iflynote-auth/**的接口请求转发到iflynote-auth服务的接口上。
后面搭建认证服务,集成spring-security-oauth2框架。待续

发表新评论