您现在的位置是:首页 >学无止境 >(一)服务发现组件 Eureka网站首页学无止境

(一)服务发现组件 Eureka

想买CT5的小曹 2023-06-09 08:00:02
简介(一)服务发现组件 Eureka

1、Eureka 简介

Eureka 是Spring Cloud Netflix 微服务套件中的一部分, 它基于Netflix Eureka 做了二次封装, 主要负责完成微服务架构中的服务治理功能。我们只需通过简单引入依赖和注解配置就能让Spring Boot 构建的微服务应用轻松地与Eureka 服务治理体系进行整合。

Eureka包含两个组件:Eureka Server(注册中心)和Eureka Client (微服务)。

Eureka Server提供服务注册服务。

Eureka Client是一个java客户端,用来简化与Eureka Server的交互、客户端同时也就是一个内置的、使用轮询(round-robin)负载算法的负载均衡器。

 

 

2、搭建 SpringCloud 父工程

 按照下图配置即可

 这里不选择任何依赖。

 pom 文件: 注意版本号修改,不然会出现错误,版本冲突导致。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>
    <modules>
        <module>EurekaServer01</module>
    </modules>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.helloparent</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>

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

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

</project>

 删除如下:这些文件夹没有用,直接删除即可,必须保留 pom.xml。

3、 创建 Maven 工程(注册中心 Eureka)

 pom文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>demo</artifactId>
        <groupId>com.helloparent</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>EurekaServer01</artifactId>

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


</project>

在新建的maven项目下面 src-->main-->resources建立一个application.yml文件

#内置的tomcat服务启动监听端口号
server:
  port: 8888
#应用名称
spring:
  application:
    name: EurekaServer01
#EurekaServer配置
eureka:
  instance:
    hostname: localhost
  server:
    #关闭自我保护模式(缺省为打开)
    enable-self-preservation: false
    #扫描失效服务的间隔时间(缺省为60*1000ms)
    eviction-interval-timer-in-ms: 1000
  client:
    register-with-eureka: false #此EurekaServer不在注册到其他的注册中心
    fetch-registry: false       #不在从其他中心中心拉取服务器信息
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka #注册中心访问地址

在main下面创建文件夹及文件,注意main函数文件名。

package com.eureka.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

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

运行查看结果。

主界面中

system status 为系统信息

General Info 为一般信息

Instances currently registered with Eureka 为注册到注册中心的所有微服务列表

4、创建第二个Maven 项目(接口)

创建文件夹及Service,如下图。

编写接口

package com.HelloInterface01.demo.service;

public interface HelloService {

        public String sayHello();
    }

注释掉 父工程里面的相关代码

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>
    <modules>
        <module>EurekaServer01</module>
        <module>HelloInterface01</module>
    </modules>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.helloparent</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>

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

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

</project>

5、服务提供者创建

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>demo</artifactId>
        <groupId>com.helloparent</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>HelloProvide01</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>com.helloparent</groupId>
            <artifactId>HelloInterface01</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>


</project>

在resources文件夹下面添加一个yml。

spring:
  application:
    name: HelloProvider01
server:
  port: 9001
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8888/eureka

创建如下文件夹及实现类

package com.HelloProvide.demo.service.Impl;


import com.HelloInterface01.demo.service.HelloService;
import org.springframework.stereotype.Service;

@Service
public class HelloServiceImpl implements HelloService {
    @Override
    public String sayHello() {
        return "hello Eureka!";
    }
}

在创建一个controller 文件夹及文件

package com.HelloProvide.demo.controller;

import com.HelloInterface01.demo.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @Autowired
    private HelloService helloService;
    @GetMapping("/hello")
    public String sayHello(){
        return helloService.sayHello();
    }
}

 编写启动类。

package com.HelloProvide.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

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

 服务已经注册上去了。

6、服务调用者创建

 修改pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>demo</artifactId>
        <groupId>com.helloparent</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>HelloConsumer01</artifactId>

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

        <dependency>
            <groupId>com.helloparent</groupId>
            <artifactId>HelloInterface01</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>


</project>

编写yml配置文件

spring:
  application:
    name: helloConsumer01
server:
  port: 9002

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8888/eureka

编写启动类

 

package com.HelloConsumer.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient


public class HelloConsumer01Start {
    @Bean
    public RestTemplate restTemplate(){

        return new RestTemplate();
    }
    public static void main(String[] args) {
        SpringApplication.run(HelloConsumer01Start.class,args);
    }
}

实现类代码:注意导包别错误。

package com.HelloConsumer.demo.Service.impl;


import com.HelloInterface01.demo.service.HelloService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Service
public class HelloServiceImpl implements HelloService {

    @Autowired
    private DiscoveryClient discoveryClient; //是一个服务查询工具,可以连接到EurekaServer,根据服务名称去查询服务信息,注意别导错包了


    @Autowired
    private RestTemplate restTemplate;  //调用rest风格接口工具类

    //从EurekaServer获取对应服务的地址和端口
    public String getServerInfo() {
        List<ServiceInstance> instanceList = discoveryClient.getInstances("HELLOPROVIDER01");
        if (instanceList != null && instanceList.size() > 0) {
            ServiceInstance serviceInstance = instanceList.get(0);
            //获取对应服务的主机地址
            String host = serviceInstance.getHost();
            //获取对应服务端口号
            int port = serviceInstance.getPort();
            return "http://" + host + ":" + port;
        }

        return null;
    }

    @Override
    public String sayHello() {
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(getServerInfo() + "/hello", String.class);
        String body = responseEntity.getBody();

        System.out.println("调用远程服务返回值:" + body);

        return body;
    }
}

编写controller

package com.HelloConsumer.demo.controller;

import com.HelloInterface01.demo.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

    @RestController
    public class HelloControllerConsumer {
        @Autowired
        private HelloService helloService;

        @RequestMapping("/testHello")
        public String sayHello(){
            return helloService.sayHello();
        }
}

启动服务,注册中心成功。

7、进行测试

http://localhost:9002/testHello

 

 

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。