I. Overview
It is to separate the client and the server one by one, and then throw each one in the spring boot Cloud, and put them together when they are needed.
2. Simple to use
The structure is like this, an eureka-server, used as an intermediary, registration center.
1. eureka-server (Registration Center)
eureka is made by Netflix, but it is part of spring cloud.
The eureka-server project only contains this
Registry
- Configure eureka information
- Use annotations to
@EnableEurekaServer
open the Eureka server before the main program class
Configure eureka information: application.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/
2. Provider-ticket (service provider)
- Write service layer and control layer
- Configure eureka information and add the application to eureka
1) Configure eureka information: 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/
Yeah, there is always something to output, then two
2) Service category
@Service
public class TicketService {
public String getTicket(){
return "《一张掉影票》";
}
}
3) Control category
@RestController
public class TicketController {
@Autowired
TicketService ticketService;
@GetMapping("/")
public String getTicket(){
return ticketService.getTicket();
}
}
When the eureka-server is still started, the provider-ticket is also started, and the application will be added to eureka
As you can see, it has been added. If you copy more copies and change the port, there will be more copies displayed here.
3. Consumer-user (consumer, client)
- Write configuration file
- Write control class
- Add annotations and methods to the main program
Also import
1) Configuration file
server:
port: 8200
spring:
application:
name: consumer-user
eureka:
instance:
prefer-ip-address: true #注册的时候使用服务的IP地址
client:
service-url:
defaultZone: http://localhost:8761/eureka/
2) Main program category
@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerUserApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerUserApplication.class, args);
}
@Bean //加入到容器中
@LoadBalanced //使用负载均衡机制
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
@EnableDiscoveryClient
: Service Discovery Notes@LoadBalanced
Using load balancing mechanism, what is load balancing mechanism? All you need to know is that if you open this, you can reduce the pressure on the memory.RestTemplate restTemplate()
Something to help us send http requests
3) Control category
@RestController
public class UserController {
@Autowired
RestTemplate restTemplate;
@GetMapping("/buy")
public String buyTicket(String name){
String s = restTemplate.getForObject("http://provider-ticket/", String.class);
return name + "购买了"+s;
}
}
RestTemplate restTemplate
Just inject this automaticallygetForObject("http://服务提供者的名字+服务提供类的控制类请求",发请求类的返回值的类型)
: The use of restTemplatehttp://
is fixed, the name of the class that provides the service isprovider-ticket
, a request of its control class is/
, the return type of this class is String.
4) Effect
This is just a simple application, the more complex is still learning
Three, hot deployment
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
What's new, ctrl+F9