springCLoud——Eureka详解+例子

目录

 

1、Eureka基础知识

(1)服务治理

(2)服务注册

(3)Eureka Server

(4)Eureka Client

2、搭建Eureka服务端

(1)创建maven工程

 (2)导入依赖

(3)配置application.yml

(4)创建主启动类

(5)启动Eureka注册中心

 3、搭建服务提供者

(1)创建maven工程

(2)导入依赖

 (3)配置application.yml

(4)创建启动类

(5)创建controller

(6)测试该服务

4、搭建服务消费者

(1)创建maven工程

 (2)导入依赖

(3)配置application.yml

(4)创建主启动类

(5)创建配置类配置远程调用

(6)创建controller

5、整体测试


 

1、Eureka基础知识

(1)服务治理

管理服务与服务之间的依赖关系,以实现服务调用,负载均衡,容错等,实现服务注册与服务发现。

(2)服务注册

在服务注册与发现中,有一个注册中心,当服务启动时,会将该服务的相关信息注册到注册中心上去。

如:服务url地址,服务消费者会以该别名方式去注册中心上获取对应服务,进而进行远程调用。

2332e215b1cf41f0abb8bf8f2ffe3e06.png 

(3)Eureka Server

提供服务注册,个微服务节点启动后,会在注册中心上注册相关信息,服务节点可以在界面中直观看到。

 

(4)Eureka Client

java客户端,客户端默认使用轮询负载均衡,在应用启动后,会向server发送心跳,若server端在多个心跳周期中未发现该服务,会自动将其移除注册表。 

2、搭建Eureka服务端

(1)创建maven工程

ef9d8d6fd35e463fa53276c1eaff3587.png

 (2)导入依赖

导入Eureka服务端、web模块依赖

    <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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

(3)配置application.yml

①服务端口为7001;

②Eureka服务端主机名;

③Eureka客户端:

register-with-eureka:是否在服务中心注册

fetchRegistry:是否可以在注册中心被发现

service-url:服务中心url地址

server:
  port: 7001

eureka:
  instance:
    hostname: localhost

  client:
    register-with-eureka: false
    fetchRegistry: false
    service-url:
      defaultZone: http://localhost:7001/eureka

(4)创建主启动类

@EnableEurekaServer:Eureka服务端注解

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

(5)启动Eureka注册中心

访问http://localhost:7001

78ca559626be4fbfbcb6b62d630f5d70.png

 3、搭建服务提供者

(1)创建maven工程

d8067fbe756845d5ad8f7c3caf2750bc.png

(2)导入依赖

导入Eureka客户端、web模块、监控依赖

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

 (3)配置application.yml

①配置服务端口号为8003;

②配置服务应用名称;

③配置Eureka注册中心,开启注册,指明注册中心地址。

server:
  port: 8003

spring:
  application:
    name: provider8003

eureka:
  client:
    register-with-eureka: true
    fetchRegistry: true
    service-url:
      defaultZone: http://localhost:7001/eureka

(4)创建启动类

@EnableEurekaClient:指明该服务为Eureka客户端

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

(5)创建controller

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello(){
        return "Hello,Eureka";
    }
}

(6)测试该服务

启动访问http://localhost:8003/hello

可以发现注册中心已经注册该服务

 9253bdfd4d8e41b6b69e364e90b152ac.png

 

4、搭建服务消费者

(1)创建maven工程

691a3fd5c6384ac688def72527b436fc.png

 (2)导入依赖

导入Eureka客户端、web、监控依赖

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

(3)配置application.yml

①配置服务·端口号为8004;

②配置服务名称为:Consumer8004;

③配置Eureka客户端,开启注册。配置注册中心地址。

server:
  port: 8004

spring:
  application:
    name: Consumer8004

eureka:
  client:
    register-with-eureka: true
    fetchRegistry: true
    service-url:
      defaultZone: http://localhost:7001/eureka

(4)创建主启动类

开启Eureka客户端

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

(5)创建配置类配置远程调用

@LoadBalanced:负载均衡注解

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

(6)创建controller

①PROVIDER8003:服务提供者的名称,其在注册中心上注册,可以通过该名称找到对应ip地址;

②restTemplate.getForObject(”远程调用服务ip地址“,“调用服务方法返回类型”)

@RestController
public class HelloController {
    //调用服务地址
    private final static String url="http://PROVIDER8003";

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("consumer")
    public String hello(){
        return restTemplate.getForObject(url+"/hello",String.class);
    }
}

5、整体测试

①启动Eureka7001

②启动服务提供者Provider8003

③启动服务消费者Consumner8004

④访问http://loaclhost:8004/hello

结果如下:

ba1d448a894c471a86544be7f6f517cb.png

 92ea0f25d18848d9a4abbe7a6b32ba30.png

 测试成功!!!

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

swttws.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值