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);
  }
}

Last modification:July 25th, 2019 at 07:14 am
如果觉得这篇技术文章对你有用,请随意赞赏

Leave a Comment