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

 

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