SpringCloud 微服务搭建和使用


  微服务架构是将一个完整的项目,拆分成多个模块去分别开发。   SpringCloud 是微服务架构落地的一套技术栈。

  SpringCloud 的八个技术点。   Eureka,服务的注册与发现;   Robbin,服务之间的负载均衡;   Feign,服务之间的通讯;   Hystrix,服务的线程隔离以及断路器;   Zuul,服务网关;   Stream,实现MQ的使用;   Config,动态配置;   Sleuth,服务追踪。

  Eureka 帮助我们维护所有服务的信息,以便服务之间的相互调用。

  创建 SpringBoot 工程作为父工程。删除 src 文件夹。   SpringBoot 和 Eureka 版本对应。

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

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>1.0.0</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <packaging>pom</packaging>

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

    <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>

  创建 Module -> Maven 工程作为 EurekaServer。自己建立启动文件等。

// pom.xml

	<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
// EurekaServerApplication.java 
// 启动类添加服务端注解

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
          
   

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

}
// application.yml

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${
          
   eureka.instance.hostname}:${
          
   server.port}/eureka/

  创建 Module -> Maven , 工程 Customer 作为 EurekaClient。自己建立启动文件等。

// pom.xml

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

<!--    web 工程服务一直运行   -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>
// CustomerApplication.java
// 启动类添加客户端注解

@SpringBootApplication
@EnableEurekaClient
public class CustomerApplication {
          
   

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

}
# 把当前 EurekaClient 服务注册到 EurekaServer 端
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
      
spring:
  application:
    name: CUSTOMER

  再创建工程名 Search 的 EurekaClient。修改端口。

// application.yml

server:
  port: 8081

  启动时右下角选择将所有服务启动统一管理。

  打开图形界面查看启动和注册是否成功。


经验分享 程序员 微信小程序 职场和发展