SpringCloud Eureka注册中心入门使用

为什么要使用注册中心?

使用springCloud微服务框架是需要进行服务拆分,降低服务功能之间的耦合性。一个服务往往需要进行集群负载均衡的动作,这时候Eureka可以对服务进行注册管理,不管这个服务有多少集群,程序员不需要关注每一个的ip地址以及端口,只需要关注在Eureka中注册的服务名即可。

Eureka入门步骤

1.创建Eureka模块

	1.1建model
		在project下创建一个maven模块
	1.2改pom
		引入Eureka所需要的pom标签
<dependencies>
		//Eureka服务端所引入的jar
        <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>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
    </dependencies>
</project>
	1.3 改配置文件application.yml
server:
  port: 7001

eureka:
  instance:
    hostname: eureka7001.com #eureka服务端实例名称
  client:
    #false表示不向注册中心注册自己
    register-with-eureka: false
    #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    fetch-registry: false
    service-url:
    #设置与eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
	1.4 写启动类
		注:类注解要加上EnableEurekaServer说明这个服务是Eureka的服务端
@SpringBootApplication
@EnableEurekaServer
public class XXXXXXX{
    public static void main(String[] args) {
        SpringApplication.run(XXXXXXX.class,args);
    }
}
	1.5 修改服务提供端的pom文件
		//Eureka的客户端引入jar包
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
	1.6 修改服务提供端的启动类
		在服务提供端的启动类上加上注解
		@EnableEurekaClient  //说明这个服务是Enable的客户端
		@EnableDiscoveryClient  //用于获取Eureka的服务信息(暂时这样理解,不正确欢迎指出)
		
	1.7 修改服务提供方的application.yml
eureka:
  client:
    #表示是否要将自己注册进EurekaServer 默认为true
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetchRegistry: true
    service-url:
    	defaultZone: http://localhost:7001/eureka
	1.8 进行到这一步我们先启动Eureka服务端 再启动Eureka客户方服务
		通过浏览器打开:http://localhost:7001 可以查看eureka服务以及注册的服务
		![在这里插入图片描述](https://img-blog.csdnimg.cn/20210608161619160.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpc3Rlcm9uZQ==,size_16,color_FFFFFF,t_70#pic_center)
	
	1.9 下面进行服务消费方的代码修改
	
	1.10 修改服务消费方pom文件
		与修改服务提供方1.5一致
		
	1.11 修改服务消费方application.yml
		与修改服务提供方1.7一致
		注意:不同服务的name值不能相同
spring:
  application:
    name: cloud-order-service
	1.12 修改服务消费方启动类
		与修改服务提供方1.6一致

	1.13 编写ApplicationContextConfig.java文件依赖注入RestTemplate
//依赖注入RestTemplate
    @Bean
    @LoadBalanced //使用@LoadBalanced注解赋予RestTemplate负载均衡的能力
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
	1.13 再次打开http://localhost:7001 检查服务消费方服务是否也被注册
	![在这里插入图片描述](https://img-blog.csdnimg.cn/20210608163505845.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpc3Rlcm9uZQ==,size_16,color_FFFFFF,t_70)

	到此完成单机(非集群)的搭建步骤

如何在单机的基础上完成集群

	2.1 多创建Eureka服务端
			让Eureka服务相互守望,不会说一个Eureka掉线让整个服务瘫痪
			再创建一个Eureka模块两个服务相互注册,比如我是7001注册到7002,7002注册到7001
			使用 defaultZone进行注册
eureka:
  instance:
    hostname: eureka7002.com #eureka服务端实例名称
  client:
    #false表示不向注册中心注册自己
    register-with-eureka: false
    #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    fetch-registry: false
    service-url:
      #设置与eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
      defaultZone: http://eureka7001.com/eureka/
	2.2 修改需要注册到Eureka服务的Application.yml
		有多个Eureka需要注册进多个Eureka用逗号相隔开
eureka:
  client:
    #表示是否要将自己注册进EurekaServer 默认为true
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetchRegistry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群
	2.3 复制服务提供方模块,修改端口号然后运行服务
		比如我现在服务提供方有两个集群 8001,8002;我们进入 http://localhost:7001查看可以发现,同一个服务名下,有多个服务正在运行。而且DS Replicas这一栏我们可以看到相互注册的两个Eureka服务
		![在这里插入图片描述](https://img-blog.csdnimg.cn/20210608165630156.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xpc3Rlcm9uZQ==,size_16,color_FFFFFF,t_70)

以上就是Eureka的集群

那么我们服务消费方如何调用服务提供方的接口?

	3.1 服务提供方需要使用RestTemplate 需要在配置类中进行依赖注入
			加上注解@LoadBalanced 可以进行负载均衡
    //依赖注入RestTemplate
    @Bean
    @LoadBalanced //使用@LoadBalanced注解赋予RestTemplate负载均衡的能力
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
	3.2 调用方法代码
		我们的url使用的是在Eureka中注册的服务名,我们通过这个服务名负载均衡调用服务的接口
@RestController
@Slf4j
public class OrderController {

    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/consumer/payment/create")
    public CommonResult<Payment> create(Payment payment){
        return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class);
    }

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id")Long id){
        return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
    }

}

如何查询Eureka中的服务信息(列表)

    @GetMapping(value = "/payment/diccovery")
    public Object discovery(){
        List<String> services = discoveryClient.getServices();//获取eureks服务列表的信息
        for (String element : services){
            log.info("*****element:"+element);
        }

        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance instance : instances){
            log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
        }

        return this.discoveryClient;
    }

如何关闭Eureka自我保护机制?

Eureka 服务端 application.yml

eureka:
  instance:
    hostname: eureka7001.com #eureka服务端实例名称
  client:
    #false表示不向注册中心注册自己
    register-with-eureka: false
    #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    fetch-registry: false
    service-url:
    #设置与eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
#      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      defaultZone: http://eureka7002.com:7002/eureka/
  server:
    #关闭eureka自我保护机制,保证不可用服务被及时剔除
    enable-self-preservation: false
    eviction-interval-timer-in-ms: 2000

Eureka Client 端 application.yml

eureka:
  client:
    #表示是否要将自己注册进EurekaServer 默认为true
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetchRegistry: true
    service-url:
#      defaultZone: http://localhost:7001/eureka
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群
  instance:
    instance-id: payment8001
#    访问路径可以显示ip地址
    prefer-ip-address: true
#    Eureka客户端向服务器端发送心跳时间间隔,单位为秒(默认30秒)
    lease-renewal-interval-in-seconds: 1
#    Eureka服务端在收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除服务
    lease-expiration-duration-in-seconds: 2

交流中进步,如有错误地方请务必指出,谢谢了。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值