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

  启动时右下角选择将所有服务启动统一管理。
在这里插入图片描述

  打开图形界面查看启动和注册是否成功。
在这里插入图片描述


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值