Personal notes of Spring Boot from 0 to learn 12 --spring Cloud (simple to use) and hot deployment

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 @EnableEurekaServeropen the Eureka server before the main program class
    Insert picture description here
    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/

Insert picture description here

2. Provider-ticket (service provider)

  • Write service layer and control layer
  • Configure eureka information and add the application to eureka
    Insert picture description here

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

Insert picture description here
Insert picture description here
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
Insert picture description here

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
  • @LoadBalancedUsing 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 restTemplateJust inject this automatically
  • getForObject("http://服务提供者的名字+服务提供类的控制类请求",发请求类的返回值的类型): The use of restTemplate http://is fixed, the name of the class that provides the service is provider-ticket, a request of its control class is /, the return type of this class is String.

4) Effect

Insert picture description here

Insert picture description here
Insert picture description here
This is just a simple application, the more complex is still learning

springboot-cloud

Three, hot deployment

<dependency>  
       <groupId>org.springframework.boot</groupId>  
       <artifactId>spring-boot-devtools</artifactId>   
</dependency>

What's new, ctrl+F9

Guess you like

Origin blog.csdn.net/yi742891270/article/details/107835987