Spring-BootJava学习之路

Springboot与Springcloud分布式

2019-07-20  本文已影响46人  椰子奶糖

SpringCloud概念

Spring Cloud provides tools for developers to quickly build some of the common 
patterns in distributed systems (e.g. configuration management, service discovery, 
circuit breakers, intelligent routing, micro-proxy, control bus, one-time tokens, global 
locks, leadership election, distributed sessions, cluster state). Coordination of 
distributed systems leads to boiler plate patterns, and using Spring Cloud 
developers can quickly stand up services and applications that implement those 
patterns. They will work well in any distributed environment, including the 
developer’s own laptop, bare metal data centres, and managed platforms such as 
Cloud Foundry.

SpringCloud分布式开发五大常用组件

分布式Demo

1.建立三个工程分别是:注册中心server,服务提供者provider和服务消费者consumer;
2.配置Server,这里用的是yaml文件写的,需要注意的是Server不需要将自己注册到eureka上,也不需要从eureka上获取信息
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.编写provider
server:
     port: 8002
spring:
     application:
         name: provider-ticket
eureka:
     instance:
        prefer-ip-address: true   #注册服务的时候使用服务的ip地址
     client:
        service-url:
            defaultZone: http://localhost:8761/eureka/
import org.springframework.stereotype.Service;

/**
 * Created by CHEN on 2019/7/19.
 */
@Service
public class TicketService {

    public String getTicket(){
        System.out.println("这是8002");
        return "《这是两张电影票》";
    }

}
import ch.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by CHEN on 2019/7/19.
 */
@RestController
public class TicketController {

    @Autowired
    TicketService ticketService;

    @GetMapping("/ticket")
    public String getTicket(){

        return  ticketService.getTicket();
    }
}
编写consumer

-消费者的服务的编写比较简单,首先配置:

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

eureka:
     instance:
        prefer-ip-address: true   #注册服务的时候使用服务的ip地址
    client:
        service-url:
            defaultZone: http://localhost:8761/eureka/


    @LoadBalanced//启动负载均衡机制
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

-就可以在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;

/**
 * Created by CHEN on 2019/7/19.
 */
@RestController
public class UserController {


    @Autowired
    RestTemplate restTemplate;


    @GetMapping("/buy")
    public String buyTicket(String name){

        //调用提供者的方法,可以看出他是通过网络访问的
        String ticket = restTemplate.getForObject("http://PROVIDER-TICKET/ticket", String.class);
        return name+"买了:"+ticket;

    }
}
image.png
上一篇 下一篇

猜你喜欢

热点阅读