使用 SpringBoot + SpringCloud 簡單體驗分布式、負(fù)載均衡

一、前言
demo 級別

二、代碼與結(jié)構(gòu)
代碼放在 github 上了:https://github.com/TheWays/springCloud.git

1、注冊中心


image.png

① RegisterApplication
僅添加注解:@EnableEurekaServer 的操作

package com.cun.register;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class RegisterApplication {

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

}

② application.yml 配置

image.png

③ pom 依賴

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>

④ 效果:http://localhost:9500
(紅色框的內(nèi)容為 Provider 啟動后才有的)

image.png

2、服務(wù)提供者


image.png

① ProviderApplication (沒改過)

package com.cun.provider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProviderApplication {

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

}

② MessageService

package com.cun.provider.service;

import org.springframework.stereotype.Service;

@Service
public class MessageService {

public String getMessage(){
    return "ITAEM";
}

}

③ MessageController

package com.cun.provider.controller;

import com.cun.provider.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MessageController {

@Autowired
MessageService messageService;

@GetMapping("/get")
public String getMessage(){
    return "provider提供信息:"+messageService.getMessage();
}

}

④ application.yml 配置

server:
port: 9400
spring:
application:
name: provider-message

eureka:
instance:
prefer-ip-address: true # 注冊服務(wù)的時候使用服務(wù)的ip地址
client:
service-url:
defaultZone: http://localhost:9500/eureka/

⑤ pom 依賴

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>

⑥ 效果:http://localhost:9400/get

image.png

3、服務(wù)消費者

image.png

① ConsumerApplication
@EnableDiscoveryClient 開啟發(fā)現(xiàn)服務(wù)功能、RestTemplate 使用負(fù)載均衡機(jī)制

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

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

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

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

}

② MessageController

package com.cun.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 MessageController {

@Autowired
RestTemplate restTemplate;

@GetMapping("/show")
public String showMessage(){
    // 服務(wù)提供者url、返回數(shù)據(jù)類型
    String s = restTemplate.getForObject("http://provider-message/get", String.class);
    return "consumer獲取信息:"+s;
}

}

③ application.yml 配置

spring:
application:
name: consumer-message
server:
port: 9600

eureka:
instance:
prefer-ip-address: true # 注冊服務(wù)的時候使用服務(wù)的ip地址
client:
service-url:
defaultZone: http://localhost:9500/eureka/

④ pom 依賴

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>

⑤ 效果:http://localhost:9600/show

image.png

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容