日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

33、springboot整合springcloud

 印度阿三17 2019-03-10

Spring Cloud

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

?

SpringCloud分布式開(kāi)發(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)(注冊(cè)中心):Eureka

此時(shí)需要引入:

?

配置文件

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

?

開(kāi)啟服務(wù):

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

}

?

訪問(wèn)網(wǎng)頁(yè):

此時(shí)的服務(wù)是開(kāi)啟的?。?!

?

?服務(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;

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

?

配置文件:

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

?

啟動(dòng)服務(wù)訪問(wèn):
@SpringBootApplication
public class ProviderApplication {

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

}

?

此時(shí)查看注冊(cè)證中心:

?

?

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

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

?

服務(wù)消費(fèi)者:

?

?

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請(qǐng)求的信息
    @Autowired
    RestTemplate restTemplate;


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

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

}

?

?

配置文件:

server.port=8088spring.application.name=consumer
#注冊(cè)服務(wù)時(shí)使用服務(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;

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

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


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

?

?

啟動(dòng)服務(wù):

?

?

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

?

來(lái)源:http://www./content-4-136401.html

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

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多