步骤:

1:创建空工程用来管理各个功能模块

2:分别创建注册中心、服务提供者、服务消费者子模块

3:注册中心:

  1)添加注册中心的启动器相关依赖

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

 

  2)配置

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)开启注解@EnableEurekaServer注解

package cn.xiwei.eureka;

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

/**
* 注册中心
* 1:配置注册信息
* 2:开启注解
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {

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

}

 

4:服务提供者

  0)添加注册服务的启动器依赖

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

  1)编写提供的服务相关接口

@Service
public class TicketService {

public String getTicket(){
return "<<厉害了,我的国...>>";
}
}

  2)将提供的服务以http请求的方式暴露出来,即在controller中提供访问接口

@RestController
public class TicketController {

@Autowired
TicketService ticketService;

@GetMapping("/ticket")
public String sayHello() {
return ticketService.getTicket() + "8002";
}
}

 

  3)配置文件中,进行相关配置,将服务注册进注册中心

server:
port: 8002

eureka:
instance:
prefer-ip-address: true # 注册服务的时候使用服务的ip地址

client:
service-url:
defaultZone: http://localhost:8761/eureka/

spring:
application:
name: provider-ticket

 

5:服务消费者

  1)添加注册服务的启动器依赖

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

 

  2)配置文件中进行相关配置

spring:
application:
name: consumer-user
server:
port: 8200

#注册中心的相关配置
eureka:
instance:
prefer-ip-address: true # 注册服务的时候使用服务的ip地址

client:
service-url:
defaultZone: http://localhost:8761/eureka/

 

  3)在主类开启注册发现注解 @EnableDiscoveryClient

  4)向容器中注册一个请求模板

/**
* 消费服务
*
* 1:配置文件中进行相关配置
* 2:编写消费服务的请求
* 3:开启注册发现注解 @EnableDiscoveryClient
* 4:使用模板请求服务
*
*/
@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerUserApplication {

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

/**
* 向容器中注册一个用来发现服务的模板 并开启负载均衡
* @return
*/
@LoadBalanced
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}

}

  5)编写消费服务的请求

/**
* @author lykan
* @create 2019-09-11 14:46
*/
@RestController
public class UserController {

@Autowired
RestTemplate restTemplate;

@GetMapping("buy")
public String buyTicket(String name){
//http://PROVIDER-TICKET/ticket
//PROVIDER-TICKET:是服务提供者注册的服务名称
//ticket: 提供服务的请求方法
String str = restTemplate.getForObject("http://PROVIDER-TICKET/ticket", String.class);
return name + "购买了" + str;
}
}

 

posted on 2019-09-11 15:29  lykan2lykan  阅读(390)  评论(0)    收藏  举报