1. 程式人生 > >《Spring Cloud微服務》讀書筆記

《Spring Cloud微服務》讀書筆記

Spring Cloud概述

  • dubbo與spring cloud的區別

dubbo基於Netty的TCP及二進位制的資料傳輸,spring cloud基於http,http每次都要建立連線,在效能上有些損耗

  • 什麼是微服務

微服務是一種架構風格,將單體應用劃分為小型的服務單元,微服務之間通過http的api進行資源的訪問與操作

  • 什麼是spring cloud

spring cloud是一系列框架的有序集合,利用spring boot開發的便利性,簡化了分散式系統基礎設施的開發,如服務註冊,服務發現,負載均衡和斷路器等

Euraka註冊中心

  • 為什麼eureka比zookeeper適合作為註冊中心

分散式系統的CAP定理,C一致性,A可用性,P分割槽容錯性。eureka是基於AP構建的,zookeeper是基於CP構建的

  • zookeeper為何能滿足一致性

zookeeper有一個leader節點,在leader無法使用的時候通過ZAB演算法選舉出一個新的leader。這個leader節點的任務就是保證寫資料的時候只向這個leader節點寫入,leader節點會同步資訊到其他節點,通過這個操作保證了資料一致性。dubbo中是基於zookeeper作為註冊中心的,spring cloud是使用eureka作為註冊中心的

  • 微服務fangjia-eureka作為註冊中心
@EnableEurekaServer// 開啟eureka服務端
@SpringBootApplication
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
spring.application.name=fangjia-eureka
server.port=8761
eureka.instance.hostname=localhost
#該應用為註冊中心,所以設定為false,表示不向自己註冊自己
eureka.client.register-with-eureka=false
#註冊中心的職責是維護服務例項,並不需要去檢索服務,所以也設定為false
eureka.client.fetch-registry=false
  • 微服務fangjia-house作為服務的提供者
@SpringBootApplication
@EnableDiscoveryClient//當前的服務是eureka客戶端
public class FshHouseServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(FshHouseServiceApplication.class, args);
    }
}
@RestController
@RequestMapping("/house")
public class HouseController {

    @Value("${server.port}")
    private String serverPort;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello() {
        return "Hello World" + serverPort;
    }

    @RequestMapping(value = "/data", method = RequestMethod.GET)
    public HouseInfo getData(@RequestParam("name") String name) {
        return new HouseInfo(1L, "上海", "虹口區", "東體小區");
    }

    @RequestMapping(value = "/data/{name}", method = RequestMethod.GET)
    public String getData2(@PathVariable("name") String name) {
        return name;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Long addData(@RequestBody HouseInfo houseInfo) {
        System.out.println(houseInfo.getAddress());
        return 1001L;
    }
}
spring.application.name=fsh-house
server.port=8081
#在啟動時將自身資訊註冊到eureka上
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
  • 微服務fsh-substitution作為服務的消費者
@SpringBootApplication
@EnableDiscoveryClient// 當前的服務是eureka客戶端
@EnableFeignClients// 啟用feign
public class FshSubstitutionServiceApplication {

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

 RestTemplate是spring提供的用於訪問rest服務的客戶端,RestTemplate提供了多種便捷訪問遠端http服務的方法

@Configuration
public class BeanConfiguration {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}

feign介面定義(後面會講到,這裡直接給出程式碼)

@FeignClient(value = "fsh-house",path = "/house")
public interface HouseRemoteClient {

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    String hello();
}
@RestController
@RequestMapping("/substitution")
public class SubstitutionController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private HouseRemoteClient houseRemoteClient;

    @RequestMapping(value = "/callHello", method = RequestMethod.GET)
    public String callHello() {
        //String result = restTemplate.getForObject("http://fsh-house/house/hello", String.class);
        String result = houseRemoteClient.hello();
        System.out.println("呼叫結果是:" + result);
        return result;
    }

    @RequestMapping(value = "/data", method = RequestMethod.GET)
    public HouseInfo getData(@RequestParam("name") String name) {
        return restTemplate.getForObject("http://fsh-house/house/data?name=" + name, HouseInfo.class);
    }

    @RequestMapping(value = "/data/{name}", method = RequestMethod.GET)
    public String getData2(@PathVariable("name") String name) {
        return restTemplate.getForObject("http://fsh-house/house/data/{name}", String.class, name);
    }

    @RequestMapping(value = "/save", method = RequestMethod.GET)
    public Long add() {
        HouseInfo houseInfo = new HouseInfo();
        houseInfo.setCity("上海");
        houseInfo.setArea("虹口");
        houseInfo.setAddress("東體小區");
        Long id = restTemplate.postForObject("http://fsh-house/house/save", houseInfo, Long.class);
        return id;
    }
}
spring.application.name=fsh-substitution
server.port=8082