源码地址:https://download.csdn.net/download/level_Tiller/12026054
1-新建project ->empty project
2-new module->spring initializr
erueka-server模块(作为服务中心),选择eureka server module功能块
3-创建新模块(生产者)provider-ticket,选择eureka discovery client功能模块
4-新建module(消费者),consumer-user,选择eureka discovery client功能模块
5-在eureka-server模块的resources下新建application.yml文件
server:
port: 8761
eureka:
instance:
hostname: eureka-server #eureka实例的主机名
client:
register-with-eureka: false #不把自己注册到eureka上
fetch-registry: false #不从eureka上来获取服务的注册信息
service-url: #指定eureka注册中心的地址,其他服务要在这注册
defaultZone: http://localhost:8761/eureka/
6-启用注册中心
启动类加入注解
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
运行
7-访问localhost:8761
8-去provider-ticket模块
新建service包TicketService类
package com.example.providerticket.service;
import org.springframework.stereotype.Service;
@Service
public class TicketService {
public String getTicket(){
return "<<厉害了,我的锅锅锅>>";
}
}
9-新建controller包 TicketController类
引入spring-boot-starter-web依赖
package com.example.providerticket.controller;
import com.example.providerticket.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.getTicket();
}
}
10-在provider-ticket模块中新建application.yml将提供者注册到服务中心中
server:
port: 8001
spring:
application:
name: provider-ticket #应用的名字
eureka:
instance:
prefer-ip-address: true #注册服务的时候使用服务的ip地址
client:
service-url:
defaultZone: http://localhost:8761/eureka/
11-在启动服务注册者程序的前提下启动服务提供者程序
此时访问8761端口
访问8001/ticket接口
12-将provider-ticket打包
复制出来,修改TicketService8002再次package
运行8001
同时,运行8002 --server.port=8002
此时 同一个应用的两个实例
13-将consumer-user应用注册到注册中心
consumer-user resources中新增application.yml
server:
port: 8200
spring:
application:
name: consumer-user #应用的名字
eureka:
instance:
prefer-ip-address: true #注册服务的时候使用服务的ip地址
client:
service-url:
defaultZone: http://localhost:8761/eureka/
14-让服务注册者发现服务
修改consumer-user的主程序
package com.example.consumeruser;
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 restTemplate(){
return new RestTemplate();
}
}
15-消费服务
consumer-user中新建controller包UserController类
package com.example.consumeruser.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("/buy")
public String buyTicket(String name){
String s = restTemplate.getForObject("http://PROVIDER-TICKET/ticket",String.class);//请求服务名/接口,返回类型
return name+"购买了"+s;
}
}
16-运行eureka-server,provider-ticket:8001端口,provider-ticket:8002端口,consumer-user
此时
访问localhost:8200/buy?name=xxx,使用restTemplate方法来请求远程的PROVIDER-TICKET来获取票
负载均衡机制
再刷新一次请求后