Spring Cloud练习之服务注册与消费
spring cloud是基于springboot的,所以需要开发中对springboot有一定的了解,如果不了解的话可以先学习springboot。
一、创建服务注册中心
1.创建项目
这里利用spring向导创建项目,勾选eureka server 引入依赖
2.创建yml文件进行配置
server:
port: 8761
eureka:
instance:
hostname: eureka-server #eureka实例的主机名
client:
register-with-eureka: false #不把自己注册到eureka
fetch-registry: false #不从eureka上来获取服务的注册信息
service-url:
defaultZone: http://localhost:8761/eureka/
3.添加注解
这里要用到eureka注册中心服务要开启@EnableEurekaServer注解,在启动类上开启就行。
package com.hzw.eurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
启动之后我们可以访问http://localhost:8761/ 查看我们注册的服务,此时并没有
二、在服务中心注册服务
创建服务提供者项目,引入的依赖和服务中心项目依赖同步骤就行。
1.创建一个简单的Controller与Service
TiketService
package com.hzw.ticket.service;
import org.springframework.stereotype.Service;
@Service
public class TicketService {
public String ticket(){
System.out.println("8002");
return "《白蛇·缘起》";
}
}
TiketController
package com.hzw.ticket.controller;
import com.hzw.ticket.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 ticket(){
return ticketService.ticket();
}
}
2.创建yml文件进行配置
这里我们配置服务端口8002
server:
port: 8002
eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:8761/eureka/ #服务注册地址
spring:
application:
name: ticket #注册服务名
我们启动项目前服务注册中心要一直开着我们服务才能注册上去。
可以看到,我们注册的tiket服务已经显示在服务注册中心
三、消费服务
新建一个user项目用来测试消费服务,引入的依赖和以上项目相同。
我们在user项目的启动类上配置一个RestTemplate和开启EnableDiscoveryClient注解
package com.hzw.user;
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;
@EnableDiscoveryClient//开启发现服务功能
@SpringBootApplication
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
@LoadBalanced//启用负载均衡机制
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
创建一个Controller来测试消费服务
package com.hzw.user.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class UserController {
@Autowired
RestTemplate restTemplate;
@GetMapping("/buy/{name}")
public String buy(@PathVariable("name") String name){
//http://TICKET/ticket :TICKET为我们在服务中心注册的名字,ticket 为服务中的mapping
String s = restTemplate.getForObject("http://TICKET/ticket", String.class);
return name+"购买了"+s;
}
}
在测试消费服务,服务提供者要一直开启项目,否则消费服务会出现错误。