负载均衡-Ribbon如何对负载均衡策略进行扩展
This commit is contained in:
88
spring-cloud-09-consumer/pom.xml
Normal file
88
spring-cloud-09-consumer/pom.xml
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.roncoo.education</groupId>
|
||||||
|
<artifactId>spring-cloud-09-consumer</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>spring-cloud-09-consumer</name>
|
||||||
|
<description>spring-cloud project for Spring Boot</description>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>1.5.1.RELEASE</version>
|
||||||
|
<relativePath /> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-eureka</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-actuator</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>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-dependencies</artifactId>
|
||||||
|
<version>Dalston.BUILD-SNAPSHOT</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>spring-snapshots</id>
|
||||||
|
<name>Spring Snapshots</name>
|
||||||
|
<url>https://repo.spring.io/snapshot</url>
|
||||||
|
<snapshots>
|
||||||
|
<enabled>true</enabled>
|
||||||
|
</snapshots>
|
||||||
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<id>spring-milestones</id>
|
||||||
|
<name>Spring Milestones</name>
|
||||||
|
<url>https://repo.spring.io/milestone</url>
|
||||||
|
<snapshots>
|
||||||
|
<enabled>false</enabled>
|
||||||
|
</snapshots>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package com.roncoo.education;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||||
|
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
@EnableEurekaClient
|
||||||
|
@SpringBootApplication
|
||||||
|
public class ConsumerApplication {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@LoadBalanced
|
||||||
|
protected RestTemplate restTemplate() {
|
||||||
|
return new RestTemplate();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(ConsumerApplication.class, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
//package com.roncoo.education.configuration;
|
||||||
|
//import org.springframework.cloud.netflix.ribbon.RibbonClient;
|
||||||
|
//import org.springframework.context.annotation.Configuration;
|
||||||
|
//
|
||||||
|
//@Configuration
|
||||||
|
//@RibbonClient(name = "spring-cloud-provider2", configuration = RoundRobinRuleConfig.class)
|
||||||
|
//public class Provider2Configuration {
|
||||||
|
//
|
||||||
|
//}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
//package com.roncoo.education.configuration;
|
||||||
|
//
|
||||||
|
//import org.springframework.cloud.netflix.ribbon.RibbonClient;
|
||||||
|
//import org.springframework.context.annotation.Configuration;
|
||||||
|
//
|
||||||
|
//@Configuration
|
||||||
|
//@RibbonClient(name = "spring-cloud-provider", configuration = RandomRuleConfig.class)
|
||||||
|
//public class ProviderConfiguration {
|
||||||
|
//
|
||||||
|
//}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
//package com.roncoo.education.configuration;
|
||||||
|
//import org.springframework.context.annotation.Bean;
|
||||||
|
//import org.springframework.context.annotation.Configuration;
|
||||||
|
//import com.netflix.loadbalancer.IRule;
|
||||||
|
//import com.netflix.loadbalancer.RandomRule;
|
||||||
|
//
|
||||||
|
///**
|
||||||
|
// * 随机算法
|
||||||
|
// *
|
||||||
|
// * @author wujing
|
||||||
|
// */
|
||||||
|
////@Configuration
|
||||||
|
//public class RandomRuleConfig {
|
||||||
|
// @Bean
|
||||||
|
// public IRule randomRule() {
|
||||||
|
// return new RandomRule();
|
||||||
|
// }
|
||||||
|
//}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
package com.roncoo.education.configuration;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.netflix.client.config.IClientConfig;
|
||||||
|
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
|
||||||
|
import com.netflix.loadbalancer.ILoadBalancer;
|
||||||
|
import com.netflix.loadbalancer.Server;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义Ribbon的负载均衡策略
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class RoncooCustomRule extends AbstractLoadBalancerRule {
|
||||||
|
|
||||||
|
private Server choose(ILoadBalancer lb, Object key) {
|
||||||
|
if (lb == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Server server = null;
|
||||||
|
|
||||||
|
while (server == null) {
|
||||||
|
if (Thread.interrupted()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Server> upList = lb.getReachableServers(); // 可用的服务实例
|
||||||
|
|
||||||
|
// 只获取端口为:7779的服务实例
|
||||||
|
for (Server s : upList) {
|
||||||
|
if (s.getPort() == 7779) {
|
||||||
|
server = s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (server == null) {
|
||||||
|
Thread.yield();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("实例IP:" + server.getHost() + " 端口:" + server.getPort());
|
||||||
|
|
||||||
|
if (server.isAlive()) {
|
||||||
|
return (server);
|
||||||
|
}
|
||||||
|
|
||||||
|
server = null;
|
||||||
|
Thread.yield();
|
||||||
|
}
|
||||||
|
|
||||||
|
return server;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Server choose(Object key) {
|
||||||
|
return choose(getLoadBalancer(), key);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void initWithNiwsConfig(IClientConfig clientConfig) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
//package com.roncoo.education.configuration;
|
||||||
|
//import org.springframework.context.annotation.Bean;
|
||||||
|
//import com.netflix.loadbalancer.IRule;
|
||||||
|
//import com.netflix.loadbalancer.RandomRule;
|
||||||
|
//import com.netflix.loadbalancer.RoundRobinRule;
|
||||||
|
///**
|
||||||
|
// * 轮询算法
|
||||||
|
// *
|
||||||
|
// * @author wujing
|
||||||
|
// */
|
||||||
|
//// @Configuration
|
||||||
|
//public class RoundRobinRuleConfig {
|
||||||
|
// @Bean
|
||||||
|
// public IRule roundRobinRule() {
|
||||||
|
// return new RandomRule();
|
||||||
|
// }
|
||||||
|
//}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
|
||||||
|
package com.roncoo.education.controller;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author wujing
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping(value = "/user", method = RequestMethod.POST)
|
||||||
|
public class UserController {
|
||||||
|
|
||||||
|
//private static final String URL="http://localhost:7777/api/user/{id}";
|
||||||
|
private static final String URL="http://spring-cloud-provider/api/user/{id}";
|
||||||
|
private static final String URL2="http://spring-cloud-provider2/api/user/{id}";
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RestTemplate restTemplate;
|
||||||
|
|
||||||
|
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
|
||||||
|
public String get(@PathVariable(value = "id") int id) {
|
||||||
|
return restTemplate.getForObject(URL, String.class, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/2/{id}", method = RequestMethod.GET)
|
||||||
|
public String get2(@PathVariable(value = "id") int id) {
|
||||||
|
return restTemplate.getForObject(URL2, String.class, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
# server
|
||||||
|
server.port=8888
|
||||||
|
|
||||||
|
# spring
|
||||||
|
spring.application.name=spring-cloud-consumer
|
||||||
|
|
||||||
|
# eureka
|
||||||
|
#eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
|
||||||
|
eureka.client.serviceUrl.defaultZone=http://roncoo:123456@localhost:8761/eureka/
|
||||||
|
|
||||||
|
# info自定义
|
||||||
|
info.build.name=@project.name@
|
||||||
|
info.build.description=@project.description@
|
||||||
|
info.build.groupId=@project.groupId@
|
||||||
|
info.build.artifact=@project.artifactId@
|
||||||
|
info.build.version=@project.version@
|
||||||
|
|
||||||
|
eureka.instance.status-page-url-path=/info
|
||||||
|
eureka.instance.instanceId=${spring.application.name}:${random.value}
|
||||||
|
eureka.instance.prefer-ip-address=true
|
||||||
|
|
||||||
|
#设置拉取服务注册信息时间,默认60s
|
||||||
|
eureka.client.registry-fetch-interval-seconds=30
|
||||||
|
|
||||||
|
#指定续约更新频率,默认是30s
|
||||||
|
eureka.instance.lease-renewal-interval-in-seconds=15
|
||||||
|
|
||||||
|
#设置过期剔除时间,默认90s
|
||||||
|
eureka.instance.lease-expiration-duration-in-seconds=45
|
||||||
|
|
||||||
|
#设置策略
|
||||||
|
#spring-cloud-provider.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
|
||||||
|
spring-cloud-provider2.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
|
||||||
|
spring-cloud-provider.ribbon.NFLoadBalancerRuleClassName=com.roncoo.education.configuration.RoncooCustomRule
|
||||||
|
#格式为:应用名.ribbon.NFLoadBalancerRuleClassName=xxx
|
||||||
|
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.roncoo.education;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest
|
||||||
|
public class ConsumerApplicationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
88
spring-cloud-09-provider-01/pom.xml
Normal file
88
spring-cloud-09-provider-01/pom.xml
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.roncoo.education</groupId>
|
||||||
|
<artifactId>spring-cloud-09-provider-01</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>spring-cloud-09-provider-01</name>
|
||||||
|
<description>spring-cloud project for Spring Boot</description>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>1.5.1.RELEASE</version>
|
||||||
|
<relativePath /> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-eureka</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-actuator</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>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-dependencies</artifactId>
|
||||||
|
<version>Dalston.BUILD-SNAPSHOT</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>spring-snapshots</id>
|
||||||
|
<name>Spring Snapshots</name>
|
||||||
|
<url>https://repo.spring.io/snapshot</url>
|
||||||
|
<snapshots>
|
||||||
|
<enabled>true</enabled>
|
||||||
|
</snapshots>
|
||||||
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<id>spring-milestones</id>
|
||||||
|
<name>Spring Milestones</name>
|
||||||
|
<url>https://repo.spring.io/milestone</url>
|
||||||
|
<snapshots>
|
||||||
|
<enabled>false</enabled>
|
||||||
|
</snapshots>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.roncoo.education;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
|
||||||
|
|
||||||
|
@EnableEurekaClient
|
||||||
|
@SpringBootApplication
|
||||||
|
public class ProviderApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(ProviderApplication.class, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package com.roncoo.education.bean;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 实体类
|
||||||
|
*
|
||||||
|
* @author wujing
|
||||||
|
*/
|
||||||
|
public class User {
|
||||||
|
private int id;
|
||||||
|
private String name;
|
||||||
|
private Date createTime;
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getCreateTime() {
|
||||||
|
return createTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCreateTime(Date createTime) {
|
||||||
|
this.createTime = createTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "RoncooUser [id=" + id + ", name=" + name + ", createTime=" + createTime + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package com.roncoo.education.controller;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import com.roncoo.education.bean.User;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author wujing
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping(value = "/api/user")
|
||||||
|
public class ApiUserController {
|
||||||
|
|
||||||
|
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
|
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
|
||||||
|
public User view(@PathVariable int id) {
|
||||||
|
User user = new User();
|
||||||
|
user.setId(id);
|
||||||
|
user.setName("无境1");
|
||||||
|
user.setCreateTime(new Date());
|
||||||
|
logger.info("请求接口返回:{}", user);
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
# server
|
||||||
|
server.port=7778
|
||||||
|
|
||||||
|
# spring
|
||||||
|
spring.application.name=spring-cloud-provider
|
||||||
|
|
||||||
|
# eureka
|
||||||
|
#eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
|
||||||
|
eureka.client.serviceUrl.defaultZone=http://roncoo:123456@roncoo1:8761/eureka/
|
||||||
|
|
||||||
|
# info自定义
|
||||||
|
info.build.name=@project.name@
|
||||||
|
info.build.description=@project.description@
|
||||||
|
info.build.groupId=@project.groupId@
|
||||||
|
info.build.artifact=@project.artifactId@
|
||||||
|
info.build.version=@project.version@
|
||||||
|
|
||||||
|
eureka.instance.status-page-url-path=/info
|
||||||
|
eureka.instance.instanceId=${spring.application.name}:${random.value}
|
||||||
|
eureka.instance.prefer-ip-address=true
|
||||||
|
|
||||||
|
#设置拉取服务注册信息时间,默认60s
|
||||||
|
eureka.client.registry-fetch-interval-seconds=30
|
||||||
|
|
||||||
|
#指定续约更新频率,默认是30s
|
||||||
|
eureka.instance.lease-renewal-interval-in-seconds=15
|
||||||
|
|
||||||
|
#设置过期剔除时间,默认90s
|
||||||
|
eureka.instance.lease-expiration-duration-in-seconds=45
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.roncoo.education;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest
|
||||||
|
public class ProviderApplicationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
88
spring-cloud-09-provider-02/pom.xml
Normal file
88
spring-cloud-09-provider-02/pom.xml
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.roncoo.education</groupId>
|
||||||
|
<artifactId>spring-cloud-09-provider-02</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>spring-cloud-09-provider-02</name>
|
||||||
|
<description>spring-cloud project for Spring Boot</description>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>1.5.1.RELEASE</version>
|
||||||
|
<relativePath /> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-eureka</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-actuator</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>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-dependencies</artifactId>
|
||||||
|
<version>Dalston.BUILD-SNAPSHOT</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>spring-snapshots</id>
|
||||||
|
<name>Spring Snapshots</name>
|
||||||
|
<url>https://repo.spring.io/snapshot</url>
|
||||||
|
<snapshots>
|
||||||
|
<enabled>true</enabled>
|
||||||
|
</snapshots>
|
||||||
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<id>spring-milestones</id>
|
||||||
|
<name>Spring Milestones</name>
|
||||||
|
<url>https://repo.spring.io/milestone</url>
|
||||||
|
<snapshots>
|
||||||
|
<enabled>false</enabled>
|
||||||
|
</snapshots>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.roncoo.education;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
|
||||||
|
|
||||||
|
@EnableEurekaClient
|
||||||
|
@SpringBootApplication
|
||||||
|
public class ProviderApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(ProviderApplication.class, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package com.roncoo.education.bean;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 实体类
|
||||||
|
*
|
||||||
|
* @author wujing
|
||||||
|
*/
|
||||||
|
public class User {
|
||||||
|
private int id;
|
||||||
|
private String name;
|
||||||
|
private Date createTime;
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getCreateTime() {
|
||||||
|
return createTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCreateTime(Date createTime) {
|
||||||
|
this.createTime = createTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "RoncooUser [id=" + id + ", name=" + name + ", createTime=" + createTime + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package com.roncoo.education.controller;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import com.roncoo.education.bean.User;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author wujing
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping(value = "/api/user")
|
||||||
|
public class ApiUserController {
|
||||||
|
|
||||||
|
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
|
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
|
||||||
|
public User view(@PathVariable int id) {
|
||||||
|
User user = new User();
|
||||||
|
user.setId(id);
|
||||||
|
user.setName("无境2");
|
||||||
|
user.setCreateTime(new Date());
|
||||||
|
logger.info("请求接口返回:{}", user);
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
# server
|
||||||
|
server.port=7779
|
||||||
|
|
||||||
|
# spring
|
||||||
|
spring.application.name=spring-cloud-provider
|
||||||
|
|
||||||
|
# eureka
|
||||||
|
#eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
|
||||||
|
eureka.client.serviceUrl.defaultZone=http://roncoo:123456@roncoo1:8761/eureka/
|
||||||
|
|
||||||
|
# info自定义
|
||||||
|
info.build.name=@project.name@
|
||||||
|
info.build.description=@project.description@
|
||||||
|
info.build.groupId=@project.groupId@
|
||||||
|
info.build.artifact=@project.artifactId@
|
||||||
|
info.build.version=@project.version@
|
||||||
|
|
||||||
|
eureka.instance.status-page-url-path=/info
|
||||||
|
eureka.instance.instanceId=${spring.application.name}:${random.value}
|
||||||
|
eureka.instance.prefer-ip-address=true
|
||||||
|
|
||||||
|
#设置拉取服务注册信息时间,默认60s
|
||||||
|
eureka.client.registry-fetch-interval-seconds=30
|
||||||
|
|
||||||
|
#指定续约更新频率,默认是30s
|
||||||
|
eureka.instance.lease-renewal-interval-in-seconds=15
|
||||||
|
|
||||||
|
#设置过期剔除时间,默认90s
|
||||||
|
eureka.instance.lease-expiration-duration-in-seconds=45
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.roncoo.education;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest
|
||||||
|
public class ProviderApplicationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
88
spring-cloud-09-service/pom.xml
Normal file
88
spring-cloud-09-service/pom.xml
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.roncoo.education</groupId>
|
||||||
|
<artifactId>spring-cloud-09-service</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>spring-cloud-09-service</name>
|
||||||
|
<description>spring-cloud project for Spring Boot</description>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>1.5.1.RELEASE</version>
|
||||||
|
<relativePath /> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-eureka-server</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-security</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>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-dependencies</artifactId>
|
||||||
|
<version>Dalston.BUILD-SNAPSHOT</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>spring-snapshots</id>
|
||||||
|
<name>Spring Snapshots</name>
|
||||||
|
<url>https://repo.spring.io/snapshot</url>
|
||||||
|
<snapshots>
|
||||||
|
<enabled>true</enabled>
|
||||||
|
</snapshots>
|
||||||
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<id>spring-milestones</id>
|
||||||
|
<name>Spring Milestones</name>
|
||||||
|
<url>https://repo.spring.io/milestone</url>
|
||||||
|
<snapshots>
|
||||||
|
<enabled>false</enabled>
|
||||||
|
</snapshots>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.roncoo.education;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
|
||||||
|
|
||||||
|
@EnableEurekaServer
|
||||||
|
@SpringBootApplication
|
||||||
|
public class ServiceApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(ServiceApplication.class, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
# server (eureka 默认端口为:8761)
|
||||||
|
server.port=8761
|
||||||
|
|
||||||
|
# spring
|
||||||
|
spring.application.name=spring-cloud-server
|
||||||
|
|
||||||
|
# eureka
|
||||||
|
# 是否注册到eureka
|
||||||
|
eureka.client.register-with-eureka=false
|
||||||
|
# 是否从eureka获取注册信息
|
||||||
|
eureka.client.fetch-registry=false
|
||||||
|
# eureka服务器的地址(注意:地址最后面的 /eureka/ 这个是固定值)
|
||||||
|
#eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
|
||||||
|
eureka.client.serviceUrl.defaultZone=http://roncoo:123456@localhost:8761/eureka/
|
||||||
|
eureka.client.availability-zones.roncoo=roncooZone
|
||||||
|
eureka.client.service-url.roncooZone=http://roncoo:123456@localhost:8761/eureka/
|
||||||
|
eureka.client.region=lll
|
||||||
|
|
||||||
|
|
||||||
|
# info自定义
|
||||||
|
info.build.name=@project.name@
|
||||||
|
info.build.description=@project.description@
|
||||||
|
info.build.groupId=@project.groupId@
|
||||||
|
info.build.artifact=@project.artifactId@
|
||||||
|
info.build.version=@project.version@
|
||||||
|
|
||||||
|
# 指定环境
|
||||||
|
eureka.environment=dev
|
||||||
|
|
||||||
|
#指定数据中心
|
||||||
|
eureka.datacenter=roncoo
|
||||||
|
|
||||||
|
# 关闭自我保护模式
|
||||||
|
eureka.server.enable-self-preservation=false
|
||||||
|
|
||||||
|
#设置清理无效节点的时间间隔,默认60000,即是60s
|
||||||
|
eureka.server.eviction-interval-timer-in-ms=5000
|
||||||
|
|
||||||
|
# 服务认证
|
||||||
|
security.basic.enabled=true
|
||||||
|
security.user.name=roncoo
|
||||||
|
security.user.password=123456
|
||||||
|
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.roncoo.education;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest
|
||||||
|
public class ServiceApplicationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user