宋子宪博客

Spring Cloud Ribbon的配置教程

添加pom坐标依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>springloaded</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>

修改application.yml 追加eureka的服务注册地址

server:
port: 80
eureka:
client:
register-with-eureka: false
service-url: 
defaultZone: http://eureka7001.com:8001/eureka/,http://eureka7002.com:8002/eureka/

对ConfigBean进行新注解@LoadBalanced 获得Rest时加入Ribbon的配置

 @Configuration
 public class ConfigBean{
      @Bean
      @LoadBalanced
      public RestTemplate getRestTemplate(){
    
      return new RestTemplate();
      }
    }

主启动类DeptConsumer80_App添加@EnableEurekaClient

@SpringBootApplication
@EnableEurekaClient
public class DeptConsumer80_App{
  public static void main(String[] args){

     SpringApplication.run(DeptConsumer80_App.class, args);
  }
}

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »