一:介绍
二:入门
1. 注册中心
1)导包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
2 )application.yml配置Eureka信息
server:
port: 8761
eureka:
instance:
hostname: eureka-server #eureka实例的主机名
client:
register-with-eureka: false #不把自己注册到eureka
fetch-registry: false #不从eureka上获取服务的注册信息
service-url:
defaultZone: http:
3)开启@EnableEurekaServer注解
package cn.zyzpp.eurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* 开启@EnableEurekaServer注解
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
4)开启注册中心
2.服务提供者
1)导入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
2)application.yml配置Eureka信息
server:
port: 8001 #8002
spring:
application:
name: provider-ticket
eureka:
instance:
prefer-ip-address: true #注册服务的时候使用服务的Ip地址
client:
service-url:
defaultZone: http:
3)Service层方法
package cn.zyzpp.providerticket.service;
import org.springframework.stereotype.Service;
/**
* Create by yster@foxmail.com 2018/6/4/004 18:37
*/
@Service
public class TicketService {
public String getTicket(){
return "《厉害了,我的国》";
}
}
4)暴露HTTP接口
/**
* Create by yster@foxmail.com 2018/6/4/004 18:39
*/
@RestController
public class TicketControllert {
@Autowired
private TicketService ticketService;
@GetMapping("/ticket")
public String getTicket(){
System.out.println("8001");
return ticketService.getTicket();
}
}
5)然后更改端口,分别打包该模块为jar包。运行。
3.服务消费者
1)application.yml配置Eureka信息
server:
port: 8200
spring:
application:
name: consumer-user
eureka:
instance:
prefer-ip-address: true #注册服务的时候使用服务的Ip地址
client:
service-url:
defaultZone: http:
2)开启发现服务的功能,使用负载均衡机制(默认轮询)
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 ConsumerUserApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerUserApplication.class, args)
}
@LoadBalanced
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate()
}
}
3)获取服务
/**
* Create by yster@foxmail.com 2018/6/4/004 19:13
*/
@RestController
public class UserController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/buy")
public String getTicket(){
String s = restTemplate.getForObject("http://PROVIDER-TICKET/ticket",String.class);
return "购买了 "+s;
}
}
- 启动服务消费者模块。查看服务提供者的控制台打印。
- 会发现第一次请求8001,第二次8002,8001,8002,,这是因为使用了负载均衡。
本文只讲解了服务的注册与发现,Spring cloud的更多内容推荐阅读:SpringCloud分布式教程