Spring Cloud学习笔记-------(一)Eureka使用

本节记录在springcloud中,使用Eureka进行分布式服务的提供和消费流程。

一、创建工程

在IDEA中创建一个empty Project,并加入三个module,分别作为注册中心、服务提供者、服务消费者。
注册中心选择Spring Cloud Discovery-> Eureka Server选项。
在这里插入图片描述
服务提供者和消费者选择Spring Cloud Discovery-> Eureka Discovery Client选项。
在这里插入图片描述

二、编写Eureka注册中心

1、配置文件,引入Eureka Server配置信息。

server:
  port: 8761
eureka:
  instance:
    hostname: eureka-server  #eureka-sever实例的主机名
  client:
    register-with-eureka: false # 不把自己注册到eureka上
    fetch-registry: false #不从eureka上来获取服务的注册信息
    service-url:
      defaultZone: http://localhost:8761/eureka/

2、在Application中增加@EnableEurekaServer注解

package com.roger.eurekaserver;

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

/**
 * 注册中心
 * 1、配置Eureka信息
 * 2.@EnableEurekaServer
 */
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

}

3、启动程序

启动EurekaServerApplication后在浏览器中输入地址:http://localhost:8761/,可以看到Eureka Server正常启动。
在这里插入图片描述

三、编写服务提供者

1、配置文件

server:
  port: 8081
spring:
  application:
    name: provider
eureka:
  instance:
    prefer-ip-address: true  #使用ip地址进行注册
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/


2、编写service

package com.roger.provider.service;

import org.springframework.stereotype.Service;

@Service
public class TicketService {

    public String getTiTicket(){
        return "杭州-宁波高铁票";
    }
}

3、提供服务 controller

package com.roger.provider.controller;

import com.roger.provider.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TicketController {
    @Autowired
    TicketService ticketService;

    @GetMapping("/ticket")
    public String getTicket(){
        return "车票信息:"+ticketService.getTiTicket();
    }
}

4、启动应用

运行ProviderApplication后可以在Eureka Server控制台中有服务注册成功。在浏览器中调用相应的接口能成功调用。
在这里插入图片描述

在这里插入图片描述

四、编写服务消费者

1、配置文件

server:
  port: 9001
spring:
  application:
    name: consumer
eureka:
  instance:
    prefer-ip-address: true  #使用ip地址进行注册
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/


2、applicaton中增加注解,并提供restTemplate

package com.roger.consumer;

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

@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerApplication {

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

	@Bean
	@LoadBalanced //启用负载均衡
	public RestTemplate restTemplate(){
		return new RestTemplate();
	}

}

3、controller调用相应的服务

package com.roger.consumer.controller;

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

@RestController
public class UserController {
    @Autowired
    RestTemplate restTemplate;
    @GetMapping("/buyTicket")
    public String buyTicket(String name){
        String ticket = restTemplate.getForObject("http://PROVIDER/ticket",String.class);
        return name+" 买到车票:"+ticket;
    }
}

4、启动应用

启动应用ConsumerApplication。
在Eureka server控制台中看到消费者也注册成功。
在这里插入图片描述
在浏览器中调用购票接口能正常从生产者中购买到车票信息。
在这里插入图片描述

配套代码

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值