Eureka:
导入依赖:
//服务端
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
//客户端
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-clent</artifactId>
</dependency>
添加application.yml文件
#服务端
server:
port: 8888 #端口号
spring:
application:
name: name #服务名称
eureka:
instance:
hostname: localhost
client:
fetch-registry: false #是否提取服务
register-with-eureka: false #是否向注册中心注册
service-url:
defaultZone: http://localhost:7001/eureka #监视地址
#客户端
server:
port: 8888 #端口号
spring:
application:
name: name #服务名称
eureka:
client:
fetch-registry: true #是否提取服务
register-with-eureka: true #是否向注册中心注册
service-url:
defaultZone: http://localhost:7001/eureka #注册地址
最后在启动类上开启服务
@EnableEurekaServer 服务端
@EnableEurekaClent 客户端
Ribbon负载均衡
1.com.netflix.loadbalancer.RoundRobinRule 轮询,默认策略。
2.com.netflix.loadbalancer.RandomRule 随机
需要在启动类之外的包中写入配置类
@Configuration
public class RuleConfig {
@Bean
public IRule getRule()
{
return new RandomRule(); //策略
}
}
再从启动类中开启负载均衡策略
@SpringBootApplication
@EnableEurekaClient
//name:需要负载均衡的服务名,configuration:自定义策略的字节码文件
@RibbonClient(name = "CLOUD-PAYMENT-SERVICE",configuration = RuleConfig.class)
public class FeignOrderMain80 {
public static void main(String[] args) {
SpringApplication.run(FeignOrderMain80.class,args);
}
}