本节记录在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控制台中看到消费者也注册成功。
在浏览器中调用购票接口能正常从生产者中购买到车票信息。