SpringCloud之Eureka

本文详细介绍了如何使用SpringCloud搭建Eureka服务治理中心,包括创建注册中心、服务提供方和服务消费方的步骤。通过配置pom.xml文件引入依赖,设置yml文件,编写启动类并启动,最终实现服务的注册、发现和调用。
摘要由CSDN通过智能技术生成

SpringCloud之Eureka的使用




一、创建Eureka注册中心

1.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">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.yun</groupId>
  <artifactId>springcloud-euerka-service</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>springcloud-euerka-service</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <!-- 引入springboot-parent父项目-->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.7.RELEASE</version>
  </parent>

  <dependencies>
<!--    引入springcloud 的eureka server依赖-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>


    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <!--指定下载源和使用springcloud的版本-->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Edgware.SR5</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

2.yml文件配置

server:
  port: 8700
eureka:
  client:
    service-url:
        defaultZone: http://${eureka.instance.hostname}:${server.port}/euerka
    register-with-eureka: false #自身,不在向euerka注册
    fetch-registry: false #启动时禁用client的注册

  instance:
    hostname: localhost
spring:
  application:
    name: eureka-server





3.编写启动类并且启动

package com.yun;

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

/**
 * @author kaka
 * @description EurekaServerApplication启动类
 * @date 2022-07-11 11:19
 */
@SpringBootApplication
@EnableEurekaServer  //当前使用Euerka的 server
public class EuerkaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EuerkaServerApplication.class);
    }
}

4.启动结果

在这里插入图片描述


二、创建Eureka提供服务方

1.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">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.yun</groupId>
  <artifactId>springcloud-euerka-servicesupport</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>springcloud-euerka-servicesupport</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <!-- 引入springboot-parent父项目-->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.7.RELEASE</version>
  </parent>

  <dependencies>
    <!--    引入springcloud 的eureka client依赖-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <!--指定下载源和使用springcloud的版本-->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Edgware.SR5</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

</project>
**注意此处应该是:spring-cloud-starter-netflix-eureka-client**

2.yml文件配置

server:
  port: 8701
eureka:
  client:
    service-url:
        defaultZone: http://${eureka.instance.hostname}:8700/eureka  #这个地址为注册中心地址
  instance:
    hostname: localhost

#当前服务名称
spring:
  application:
    name: eureka-service

3.编写SpringBoot启动类

package com.yun;

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

/**
 * @author kaka
 * @description 启动类
 * @date 2022-07-11 11:36
 */
@SpringBootApplication
@EnableDiscoveryClient //代表自己是一个服务提供方,注意这里注解的区别
public class EuerkaServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(EuerkaServiceApplication.class);
    }
}

4.启动后查看注册中心网页

    这里可以看到已经注册成功

在这里插入图片描述

5.编写测试类进行测试

1.测试类代码

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author kaka
 * @description hello
 * @date 2022-07-11 11:38
 */
@RestController
@RequestMapping("/hello")
public class HelloController {
    @RequestMapping("/world")
    public String helloWorld(String s){
        System.out.println("传入的值:"+s);
        return "传入的值为:"+s;
    }
}

2.结果

在这里插入图片描述

三、创建Eureka服务消费方

1.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">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.yun</groupId>
  <artifactId>springcloud-euerka-serviceconsume</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>springcloud-euerka-serviceconsume</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
  <!-- 引入springboot-parent父项目-->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.7.RELEASE</version>
  </parent>

  <dependencies>
    <!--    引入springcloud 的eureka client依赖-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

  </dependencies>

  <!--指定下载源和使用springcloud的版本-->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Edgware.SR5</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

2.yml文件配置

server:
  port: 8702 #服务消费方
eureka:
  client:
    service-url:
        defaultZone: http://${eureka.instance.hostname}:8700/eureka
  instance:
    hostname: localhost

#当前服务名称
spring:
  application:
    name: eureka-consumer

3.编写SpringBoot启动类

package com.yun;

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

/**
 * @author kaka
 * @description consumer
 * @date 2022-07-11 13:26
 */
@SpringBootApplication
@EnableDiscoveryClient  //当前使用eureka的server
public class EuerkaConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EuerkaConsumerApplication.class);
    }
}

4.启动后查看注册中心网页

在这里插入图片描述

5.编写消费方代码

1.消费方代码

package com.yun;

import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author kaka
 * @description \
 * @date 2022-07-11 13:28
 */
@RestController
@RequestMapping("/Hello")
public class ConsumerController {

    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private RestTemplate restTemplate; //这里使用Bean注入一下

    @RequestMapping("/consumer")
    public String helloWorld(String s){
        System.out.println("传入的值:"+s);
        String forObject3 =restTemplate.getForObject("http://EUREKA-SERVICE/hello/world?s="+s,String.class);
        return forObject3;
    }
}

package com.yun;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * @author kaka
 * @description
 * @date 2022-07-11 14:07
 */
@Configuration
public class Beans {
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

2.重新启动进行消费调用

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

HappyAcmen

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

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

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

打赏作者

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

抵扣说明:

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

余额充值