Eureka + Springboot 配置 【注册中心】,【生产者】与【消费者】

本文详细介绍了如何搭建Eureka注册中心,包括配置pom.xml、application.properties及启动类。接着展示了生产者服务的配置,包括服务注册、服务信息查看。然后是消费者服务的设置,使用Ribbon进行负载均衡。通过实例演示了服务间的调用与测试,确保服务正常运行。
摘要由CSDN通过智能技术生成

1. 注册中心:

=>pom.xml

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <version>${netflix.version}</version>
        </dependency>

由于是注册中心,所以只需要【spring-cloud-starter-netflix-eureka-server】就够了,不需要【spring-cloud-starter-netflix-eureka-client】

=>application.properties

server.port=9001

eureka.instance.hostname=eureka-9001

# eureka.client.register-with-eureka = false 表示不向注册中心注册自己,因为自己就是注册中心
eureka.client.register-with-eureka=false

# eureka.client.fetch-register = false 表示自己为注册中心
eureka.client.fetch-registry=false

说明就都在注释里面了。

=>启动类

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

}

需要注解 @EnableEurekaServer 开启 Eureka-Server 

=>简单测试一下

>>http://localhost:9001/

出现上面的界面就表示成功了。

备注:界面丑丑的,回来看看能不能自定义界面的。

2. 生产者:

=>pom.xml

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>${netflix.version}</version>
        </dependency>

生产者和消费者的服务都属于客户端,所以需要添加【spring-cloud-starter-netflix-eureka-client】的依赖

=>application.properties

server.port=8001

# 服务名称(生产者)
spring.application.name=eureka-provider-service

# 服务描述信息
eureka.instance.instance-id=eureka-provider-service-instance
eureka.client.service-url.defaultZone=http://localhost:9001/eureka

服务名称与服务描述信息不说明了,

最后的是将生产者服务注册到注册中心的地址【http://localhost:9001/】+【eureka】

备注:

  • 这里有一个注意点,在做成服务名称的时候,Eureka是不识别下划线的,因此这里原本使用下划线的地方都换成【-】了。
  • 这里配置的【eureka.instance.instance-id=eureka-provider-service-instance】就是SpringEureka界面服务的Status列的名称。

=>启动类

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

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class EurekaProvider01Application {

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

}

客户端要添加注解 @EnableEurekaClient  或者  @EnableDiscoveryClient(似乎只需要添加一个就行了,这里添加了2个。。。)

备注:这里@EnableEurekaClient确实只需要加这一个就行了,因为两者实现的功能基本相同,同时@EnableEurekaClient内部实现了@EnableDiscoveryClient,

  • 所以说如果使用Eureka作为注册中心的话,推荐使用@EnableEurekaClient
  • 如果使用别的注册中心的话(如Nacos),那么就可以使用@EnableDiscoveryClient

=>Controller

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

@RestController
public class ProviderController01 {

    @RequestMapping("/echo/{str}")
    public String echo(@PathVariable String str){
        return "Hello" + str;
    }
}

简简单单的Controller

=>简单测试一下:

>>http://127.0.0.1:8001/echo/12:测试生产者正常启动

>>http://127.0.0.1:9001/:生产者的服务在注册中心正常注册

备注:红字并不时报错,是因为Eureka-Server长时间没有心跳而挂起(差不多是这个意思),具体原因需要参考Eureka的设计原理。

2-1. 生产者 - info信息查看

查看SpringEureka的页面,如果想要查看服务的自定义的【info】信息,需要如下配置:

=>pom.xml

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>${spring.version}</version>
        </dependency>

=>application.properties

server.port=8001

# 服务名称(生产者)
spring.application.name=eureka-provider-service

# 服务描述信息
eureka.instance.instance-id=eureka-provider-service-instance

eureka.client.service-url.defaultZone=http://eureka-9002:9002/eureka,http://eureka-9001:9001/eureka,http://eureka-9003:9003/eureka

info.user.name=wang

最后一行配置的【info.user.name=wang】就是自定义配置的服务信息

=>简单测试一下:

点击服务的Status的链接(eureka-provider-service-instance):

自定义的服务信息会以Json的形式显示出来

3. 消费者

=>pom.xml

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>${netflix.version}</version>
        </dependency>

说明同生产者。

备注:这里可能会有一个小疑问,为什么在消费者使用Ribbon负载均衡了,没有导入依赖呢?原因是【spring-cloud-starter-netflix-eureka-client】之中自带了Ribbon了。

=>application.properties

server.port=7001

spring.application.name=eureka-consumer-service

# 消费者只是向注册中心取服务,并不将本身注册到注册中心中,因此 eureka.client.register-with-eureka = false
eureka.client.register-with-eureka=false
eureka.client.service-url.defaultZone=http://localhost:9001/eureka

说明同生产者。

=>启动类

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

@SpringBootApplication
@EnableEurekaClient
public class EurekaConsumer01Application {

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

}

=>Ribbon负载均衡

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;

@Configuration
public class MyConfiguration {

    @LoadBalanced
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

=>Controller

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ConsumerController01 {

    @Autowired
    private RestTemplate getRestTemplate;

    @RequestMapping("/consumer/echo/{str}")
    public String echo(@PathVariable String str){
        return getRestTemplate.getForObject("http://eureka-provider-service/echo/" + str, String.class);
    }
}

这里利用Ribbon的RestTemplate,采用了生产者的服务名称来获得服务。

=>简单测试一下:

>>http://localhost:7001/consumer/echo/14:能获得生产者提供的对应服务

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值