SpringCloudEureka服务治理完整过程搭建



写在前面

Spring Boot 版本:2.1.8,Spring Cloud 版本:Greenwich.SR2

学习微服务最好使用容器来搭建,对于正在学习编程的小伙伴推荐买上属于自己的一台服务器,用来练练手,也顺带学习 Docker,这很重要。最近,阿里在搞活动,新用户 1C2G 只要 98 一年,我也比较了很多,还是比较划算的,我自己也入手了,可以点进来看看,对了,最便宜的一款在 【全部必抢爆款】 里面: 阿里云服务器,助力云上云!

服务治理用来实现各个微服务实例的自动化注册与发现。


由于需要创建多个服务,我们考虑使用父子模块的结构来管理服务。
首先,创建父模块 (Spring_Cloud),其它服务均为其子模块。

父模块 POM 文件内容如下:

	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
    </parent>

    <groupId>com.duofei</groupId>
    <artifactId>Spring_Cloud</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <modules>
        <module>eureka-server</module>
        <module>eureka-service-provider</module>
        <module>eureka-service-consumer</module>
    </modules>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR2</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>

父模块创建完成后,执行以下流程。

1. 服务注册中心搭建

创建 Spring_Cloud 模块的子模块,模块名称为 eureka-server,POM 文件依赖如下:

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

通过@EnableEurekaServer 注解启动服务注册中心:

@EnableEurekaServer
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(WebApplicationType.SERVLET).run(args);
    }
}

需要修改它的默认配置,application.yml 文件内容如下:

server:
  port: 1111
eureka:
  instance:
    hostname: localhost
  client:
    # 由于该应用为注册中心,设置为false ,代表不向注册中心注册自己
    register-with-eureka: false
    # 不需要去检索服务
    fetch-registry: false

访问 http://localhost:1111/,可以看到 Eureka 信息面板。


2. 服务注册

创建 Spring_Cloud 模块的子模块,模块名称为 eureka-service-provider,该模块将作为一个微服务应用,向服务注册中心注册自己。POM 文件依赖如下:

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

然后,添加请求处理接口:

@RestController
public class HelloController {

    @Autowired(required = false)
    private DiscoveryClient client;

    @RequestMapping(value = "hello",method = RequestMethod.GET)
    public String index(){
        return "Hello World";
    }
}

紧接着,在启动类中通过 @EnableDiscoveryClient 注解,激活Eureaka 客户端相关功能,包括服务注册,服务续约,服务获取等。

@EnableDiscoveryClient
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

最后,我们需要修改配置文件,指定服务注册中心地址:

spring:
  application:
    name: hello-service
eureka:
  client:
    service-url:
      defaultZone: http://localhost:1111/eureka/
server:
  servlet:
    context-path: /provider

成功启动该服务后,我们可以在服务注册中心的信息面板中看到该服务的注册信息。


注:如果你在信息面板看到如下的警告:

EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY’RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.

这是因为触发了 Eureka Server 的自我保护机制。可以通过 eureka.server.enable-self-preservation=false (默认为 true) 来关闭。


3. 服务发现与消费

创建 Spring_Cloud 模块的子模块,模块名称为 eureka-service-consumer,该模块将发现服务以及消费服务。其中,服务发现由Eureka的客户端完成,消费服务则由Ribbon(负载均衡器)完成,POM 文件依赖如下:

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

然后,编写请求接口,该接口调用了服务注册中心提供的服务:

@RestController
public class ConsumerController {

    @Autowired
    RestTemplate restTemplate;

    @GetMapping(value = "/ribbon-consumer")
    public String helloConsumer(){
        return restTemplate.getForEntity("http://HELLO-SERVICE/provider/hello", String.class).getBody();
    }
}

紧接着,编写该服务的启动类:

@EnableDiscoveryClient
@SpringBootApplication
public class Application {

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

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

@LoadBalanced 注解开启客户端负载均衡。

最后,在 application.yml 配置文件中指定 Eureka 服务注册中心的位置,需要与服务注册使用同一地址:

spring:
  application:
    name: service-consumer
eureka:
  client:
    service-url:
      defaultZone: http://localhost:1111/eureka/
server:
  servlet:
    context-path: /consumer

我们可以发现 Eureka 信息面板 多了 SERVICE-CONSUMER 服务。

通过访问 http://localhost:8081/consumer/ribbon-consumer ,成功响应 “Hello World” 。至此,整个基于 Eureka 的服务治理完整流程搭建完毕。

服务治理中的 服务注册中心、服务注册以及服务发现和消费均有涉及。

注:我想上下文路径 (content-path) 不应该指定,否则会导致服务调用时需要拼接该路径。微服务名已经很好的体现了上下文路径所能带给我们的信息,就像上面的 HELLO-SERVICESERVICE-CONSUMER 一样。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值