spring cloud eureka RestTemplate及普通負載均衡

Eureka有3個概念

1: Eureka註冊中心

2: Eureka提供者

3: Eureka消費者

註冊中心參考https://blog.csdn.net/tang05709/article/details/105902943

註冊提供者

關鍵點1: pom.xml

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
<dependencies>
        <dependency>
            <groupId>com.don</groupId>
            <artifactId>tcloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.22</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>
    </dependencies>

第1個、第2個、第n個,都這樣

 

關鍵點2: application.yml

port 各是各的

service-url 配置註冊中心集羣

server:
  port: 8001


spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost:3306/tcloud?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=utf8
    username: root
    password: 123456
  application:
    name: tcloud-payment-provider


mybatis:
  mapperLocations: classpath:mapper/*.xml
  type-aliases-package: com.don.tcloud.providerpayment.entities
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://www.eureka7001.com:7001/eureka,http://www.eureka7002.com:7002/eureka

port不一樣,其他的可以都一樣

 

關鍵點3: 啓動類Eureka Client註解

@EnableEurekaClient

 

Controller示例

@GetMapping("/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Integer id)
    {
        Payment payment = paymentService.getPaymentById(id);
        if (null == payment) {
            return new CommonResult(445, "fail", null);
        }
        return new CommonResult(200, "cuccess: server port" + serverPort, payment);
    }

 

 

註冊消費者

關鍵點1:pom.xml

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
<dependencies>
        <dependency>
            <groupId>com.don</groupId>
            <artifactId>tcloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>
    </dependencies>

關鍵點2: application.yml

server:
  port: 80

spring:
  application:
    name: tcloud-order-provider

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://www.eureka7001.com:7001/eureka,http://www.eureka7002.com:7002/eureka

關鍵點3:在Controller中調用RestTemplate

@Autowired
    private RestTemplate restTemplate;
    //private final static String PAYMENR_URI = "http://127.0.0.1:8001";
    private final static String PAYMENR_URI = "http://TCLOUD-PAYMENT-PROVIDER";

連接地址就是服務註冊中心的Application name

關鍵點4: 配置RestTemplate的Bean

@Configuration
public class RestTemplateConfig {

    @Bean
    @LoadBalanced // 賦予RestTemplate負載均衡的能力
    public RestTemplate getRestTemplate()
    {
        return new RestTemplate();
    }
}

 

關鍵點5: 啓動類加上Eureka Client註解

@EnableEurekaClient

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章