电竞比分网-中国电竞赛事及体育赛事平台

分享

33、springboot整合springcloud

 印度阿三17 2019-03-10

Spring Cloud

Spring Cloud是一個分布式的整體解決方案。Spring Cloud 為開發(fā)者提供了在分布式系統(tǒng) (配置管理,服務(wù)發(fā)現(xiàn),熔斷,路由,微代理,控制總線,一次性token,全局瑣,leader選舉, 分布式session,集群狀態(tài))中快速構(gòu)建的工具,使用Spring Cloud的開發(fā)者可以快速的啟 動服務(wù)或構(gòu)建應(yīng)用、同時能夠快速和云平臺資源進(jìn)行對接。

?

SpringCloud分布式開發(fā)五大常用組件 服務(wù)發(fā)現(xiàn)——Netflix Eureka? 客服端負(fù)載均衡——Netflix Ribbon? 斷路器——Netflix Hystrix? 服務(wù)網(wǎng)關(guān)——Netflix Zuul? 分布式配置——Spring Cloud Config

?

新建工程: 服務(wù)發(fā)現(xiàn)(注冊中心):Eureka

此時需要引入:

?

配置文件

server.port=8761
#主機(jī)名
eureka.instance.hostname=server
#不做高可用不進(jìn)行設(shè)置
#不把本身注冊在注冊中心
eureka.client.register-with-eureka=false
#不從eureka上獲取服務(wù)的注冊信息
eureka.client.fetch-registry=false
#服務(wù)中心地址
eureka.client.service-url.DEFAULT_ZONE=http://localhost:8761/eureka/

?

開啟服務(wù):

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

}

?

訪問網(wǎng)頁:

此時的服務(wù)是開啟的!??!

?

?服務(wù)提供者:

?

TicketService.java

package com.cr.provider.service;
import org.springframework.stereotype.Service;
@Service
public class TicketService {
    public String buyTicket(){
        return "戰(zhàn)狼2";
    }
}

?

?

?TicketController.java

import org.springframework.web.bind.annotation.RestController;
@RestController
public class TicketController {
    @Autowired
    TicketService ticketService;

    //通過http協(xié)議進(jìn)行發(fā)送的
    @GetMapping("/buy")
    public String getTicket(){
        return ticketService.buyTicket();
    }
}

?

配置文件:

server.port=8081
#應(yīng)用起名字spring.application.name=provider
#注冊服務(wù)時使用服務(wù)的ip地址
eureka.instance.prefer-ip-address=true
#服務(wù)中心地址
eureka.client.service-url.DEFAULT_ZONE=http://localhost:8761/eureka/

?

啟動服務(wù)訪問:
@SpringBootApplication
public class ProviderApplication {

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

}

?

此時查看注冊證中心:

?

?

此時打包兩個jar文件分別未8081、8082端口,分別進(jìn)行多個服務(wù)的注冊

此時:同一個應(yīng)用的兩個實例

?

服務(wù)消費者:

?

?

UserController.java

package com.cr.consumer.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;

@RestController
public class UserController {

    //用于獲取http請求的信息
    @Autowired
    RestTemplate restTemplate;


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

        String ticket = restTemplate.getForObject("http://PROVIDER/buy", String.class);
        return name   "購買了"   ticket ;
    }

}

?

?

配置文件:

server.port=8088spring.application.name=consumer
#注冊服務(wù)時使用服務(wù)的ip地址
eureka.instance.prefer-ip-address=true
#服務(wù)中心地址
eureka.client.service-url.DEFAULT_ZONE=http://localhost:8761/eureka/

?

主類:

package com.cr.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

//開啟發(fā)現(xiàn)服務(wù)功能
@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApplication {

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


    //http請求
    @LoadBalanced//使用負(fù)載均衡機(jī)制
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

?

?

啟動服務(wù):

?

?

負(fù)載均衡機(jī)制,沒執(zhí)行一次就更換一次

?

來源:http://www./content-4-136401.html

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多