美文网首页
Spring Cloud 基本配置

Spring Cloud 基本配置

作者: ruoshy | 来源:发表于2019-08-28 19:38 被阅读0次

概述

  Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智能路由,微代理,控制总线)。分布式系统的协调导致了样板模式, 使用Spring Cloud开发人员可以快速地支持实现这些模式的服务和应用程序。他们将在任何分布式环境中运行良好,包括开发人员自己的笔记本电脑,裸机数据中心,以及Cloud Foundry等托管平台。

⚠️以下用例基于 Spring Boot 2.0 以及 JDK 11 环境

一、服务注册和发现

创建 Eureka Server 工程添加以下依赖

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>

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

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

由于 JDK11 中 JAXB 不再与JDK捆绑在一起我们还需要在pom.xml中添加以下依赖

        <!-- jaxb模块引用 -->
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
        </dependency>

        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.3.0</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.0</version>
        </dependency>

        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>
        <!---->

在项目启动类上添加 @EnableEurekaServer 注解

@EnableEurekaServer
@SpringBootApplication
public class ServerApplication {

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

}

添加配置

server:
  #  注册中心端口号
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    # 指示此实例是否应注册其信息使用Eureka服务器供他人发现 默认为true
    registerWithEureka: false
    # 表示此客户端是否应从Eureka服务器获取Eureka注册表信息 默认为true
    fetchRegistry: false
    serviceUrl:
      # 服务注册的 URL
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

完成以上配置后注册中心就可以启动了,使用浏览器访问 http://localhost:8761/

注册中心面板

目前已经成功的运行了注册中心,但是还没有服务提供者注册,所以接下来开始创建服务提供者

创建 Eureka Client 工程添加以下依赖:

<properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <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.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

在项目启动类上添加 @EnableEurekaClient 注解,@RestController 注解,并创建Api接口方便后面测试

@EnableEurekaClient
@RestController
@SpringBootApplication
public class Client1Application {
    @Value("${server.port}")
    private String port;

    @RequestMapping("/")
    public String index() {
        return "Port is: " + port;
    }

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

添加配置

server:
  # 服务提供者端口号
  port: 8762

eureka:
  client:
    serviceUrl:
      # 服务注册的 URL
      defaultZone: http://localhost:8761/eureka/

spring:
  application:
    # 服务提供者的名字
    name: client

以上服务提供者就创建成功了,运行服务提供者项目后再次访问注册中心面板,服务提供者注册成功

注册中心面板

二、负载均衡

完成了服务注册中心和服务提供者后,发现并没有发挥出服务注册中心的作用,接下来我们使用 Ribbon 通过注册中心来实现负载均衡。

创建 Service Ribbon 工程添加以下依赖:

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <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-netflix-ribbon</artifactId>
        </dependency>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

在项目启动类上添加 @EnableEurekaClient 注解并添加Bean以及@LoadBalanced注解

@EnableEurekaClient
@SpringBootApplication
public class ServiceRibbonApplication {

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

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

}

创建 Controller 类

@RestController
public class TestController {
    @Resource
    private RestTemplate restTemplate;

    @RequestMapping("/")
    public String index() {
        // 通过服务提供者名字访问对应接口
        return restTemplate.getForObject("http://client", String.class);
    }
}

添加配置

server:
  # Ribbon 服务的端口
  port: 8764

eureka:
  client:
    serviceUrl:
      # 服务注册的 URL
      defaultZone: http://localhost:8761/eureka/

spring:
  application:
    name: service-ribbon

由于一个客户端无法看出效果,所以我们再创建一个服务提供者并运行,所有配置与上面的服务提供者一致,只需要修改端口,再次访问注册中心面板,发现多了一个服务提供者。

注册中心面板

然后运行我们的Service Ribbon 工程,访问 Service Ribbon 工程 Controller 类中的接口,并重复刷新查看结果。

服务提供者(一) 服务提供者(二)

以上就简单的完成了负载均衡的功能


目前工程的结构

三、路由器和过滤器

路由在微服务体系结构的一个组成部分。例如,/可以映射到您的Web应用程序,/api/users映射到用户服务,并将/api/shop映射到商店服务。Zuul是Netflix的基于JVM的路由器和服务器端负载均衡器。

创建 Service Zuul 工程添加以下依赖:

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>

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

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

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

在项目启动类上添加 @EnableZuulProxy 注解

@EnableZuulProxy
@SpringBootApplication
public class ServiceZuulApplication {

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

}

添加配置

zuul:
  routes:
    users:
      # 路由匹配
      path: /client/**
      # 转发的服务
      serviceId: client

server:
  # zuul 服务的端口
  port: 8765

eureka:
  client:
    serviceUrl:
      # 服务注册的 URL
      defaultZone: http://localhost:8761/eureka/

spring:
  application:
    name: service-zuul

运行相应项目

直接访问http://localhost:8765/client就可以通过Zuul路由负载均衡访问client服务了

相关文章

网友评论

      本文标题:Spring Cloud 基本配置

      本文链接:https://www.haomeiwen.com/subject/pyhyectx.html