Skip to content

Commit ef4cb53

Browse files
author
quding
committed
Spring Cloud
1 parent 5a0546c commit ef4cb53

File tree

23 files changed

+626
-0
lines changed

23 files changed

+626
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<groupId>cn.mrdear.springcloud</groupId>
7+
<artifactId>springcloud-parent</artifactId>
8+
<version>1.0.0</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
<artifactId>service-discovery-eureka</artifactId>
12+
13+
<dependencies>
14+
<dependency>
15+
<groupId>org.springframework.cloud</groupId>
16+
<artifactId>spring-cloud-starter-eureka-server</artifactId>
17+
</dependency>
18+
</dependencies>
19+
20+
<build>
21+
<plugins>
22+
<plugin>
23+
<groupId>org.apache.maven.plugins</groupId>
24+
<artifactId>maven-compiler-plugin</artifactId>
25+
</plugin>
26+
</plugins>
27+
</build>
28+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cn.mrdear;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
6+
7+
/**
8+
* @author Niu Li
9+
* @since 2017/5/25
10+
*/
11+
@EnableEurekaServer//启动服务注册中心
12+
@SpringBootApplication
13+
public class EurekaApplication {
14+
public static void main(String[] args) {
15+
SpringApplication.run(EurekaApplication.class,args);
16+
}
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
server:
3+
port: 8761
4+
5+
spring:
6+
application:
7+
name: eureka-server
8+
9+
eureka:
10+
instance:
11+
hostname: localhost
12+
prefer-ip-address: true
13+
client:
14+
register-with-eureka: false
15+
fetch-registry: false
16+
service-url:
17+
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
18+
19+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>springcloud-parent</artifactId>
7+
<groupId>cn.mrdear.springcloud</groupId>
8+
<version>1.0.0</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<groupId>cn.mrdear.springcloud</groupId>
13+
<artifactId>Service-Product-Consumer-Feign</artifactId>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.springframework.cloud</groupId>
18+
<artifactId>spring-cloud-starter-feign</artifactId>
19+
</dependency>
20+
<dependency>
21+
<groupId>io.github.openfeign</groupId>
22+
<artifactId>feign-httpclient</artifactId>
23+
<version>9.5.0</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.springframework.cloud</groupId>
27+
<artifactId>spring-cloud-starter-eureka</artifactId>
28+
</dependency>
29+
</dependencies>
30+
</project>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import org.apache.http.client.HttpClient;
2+
import org.apache.http.client.config.RequestConfig;
3+
import org.apache.http.config.RegistryBuilder;
4+
import org.apache.http.conn.socket.ConnectionSocketFactory;
5+
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
6+
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
7+
import org.apache.http.impl.client.CloseableHttpClient;
8+
import org.apache.http.impl.client.HttpClientBuilder;
9+
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
10+
import org.springframework.boot.SpringApplication;
11+
import org.springframework.boot.autoconfigure.SpringBootApplication;
12+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
13+
import org.springframework.cloud.netflix.feign.EnableFeignClients;
14+
import org.springframework.context.annotation.Bean;
15+
16+
17+
/**
18+
* @author Niu Li
19+
* @since 2017/6/3
20+
*/
21+
@SpringBootApplication(scanBasePackages = "cn.medear.springcloud.controller")
22+
@EnableDiscoveryClient
23+
@EnableFeignClients(basePackages = "cn.medear.springcloud.restclient")
24+
public class UserConsumeFeignApplication {
25+
26+
public static void main(String[] args) {
27+
SpringApplication.run(UserConsumeFeignApplication.class, args);
28+
}
29+
30+
/**
31+
* 自定义feign的Httpclient,其注入在HttpClientFeignLoadBalancedConfiguration中
32+
* @return
33+
*/
34+
@Bean
35+
public HttpClient feignHttpClient() {
36+
PoolingHttpClientConnectionManager HTTP_CLIENT_CONNECTION_MANAGER =
37+
new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", SSLConnectionSocketFactory.getSocketFactory()).build());
38+
HTTP_CLIENT_CONNECTION_MANAGER.setDefaultMaxPerRoute(100);
39+
HTTP_CLIENT_CONNECTION_MANAGER.setMaxTotal(200);
40+
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(60000).setConnectTimeout(60000).setSocketTimeout(60000).build();
41+
CloseableHttpClient httpClient = HttpClientBuilder.create()
42+
.setConnectionManager(HTTP_CLIENT_CONNECTION_MANAGER)
43+
.setDefaultRequestConfig(requestConfig).build();
44+
return httpClient;
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package cn.medear.springcloud.controller;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.PathVariable;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
import javax.annotation.Resource;
11+
12+
import cn.medear.springcloud.entity.User;
13+
import cn.medear.springcloud.restclient.UserClient;
14+
import lombok.extern.slf4j.Slf4j;
15+
16+
/**
17+
* 用于消费UserService的类
18+
* @author Niu Li
19+
* @since 2017/6/3
20+
*/
21+
@RestController
22+
@Slf4j
23+
public class ConsumeUserController {
24+
25+
@Resource
26+
private UserClient userClient;
27+
28+
private static final ObjectMapper objectMapper = new ObjectMapper();
29+
30+
@GetMapping("/{id}")
31+
public String findUserById(@PathVariable Long id) {
32+
User user = userClient.findById(id);
33+
String valueAsString = null;
34+
try {
35+
valueAsString = objectMapper.writeValueAsString(user);
36+
} catch (JsonProcessingException e) {
37+
log.error("parse json error. {}",e);
38+
}
39+
return valueAsString;
40+
}
41+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cn.medear.springcloud.entity;
2+
3+
import java.io.Serializable;
4+
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
import lombok.ToString;
8+
9+
/**
10+
* @author Niu Li
11+
* @since 2017/6/3
12+
*/
13+
@Data
14+
@NoArgsConstructor
15+
@ToString
16+
public class User implements Serializable{
17+
18+
private static final long serialVersionUID = -2885884654600114856L;
19+
20+
private Long id;
21+
22+
private String username;
23+
24+
private String password;
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cn.medear.springcloud.restclient;
2+
3+
import org.springframework.cloud.netflix.feign.FeignClient;
4+
import org.springframework.web.bind.annotation.PathVariable;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RequestMethod;
7+
8+
import cn.medear.springcloud.entity.User;
9+
10+
/**
11+
* @author Niu Li
12+
* @since 2017/6/4
13+
*/
14+
@FeignClient("service-user-provider")
15+
public interface UserClient {
16+
17+
@RequestMapping(value = "/{id}",method = RequestMethod.GET)
18+
User findById(@PathVariable("id") Long id);
19+
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
server:
2+
port: 8085
3+
4+
spring:
5+
application:
6+
name: service-product-consumer-fegin
7+
8+
eureka:
9+
client:
10+
service-url:
11+
default-zone: http://localhost:8761/eureka/
12+
instance:
13+
prefer-ip-address: true
14+
15+
feign:
16+
httpclient:
17+
enabled: true
18+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>springcloud-parent</artifactId>
7+
<groupId>cn.mrdear.springcloud</groupId>
8+
<version>1.0.0</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<groupId>cn.mrdear.springcloud</groupId>
13+
<artifactId>Service-Product-Consumer-Ribbon</artifactId>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.springframework.cloud</groupId>
18+
<artifactId>spring-cloud-starter-eureka</artifactId>
19+
</dependency>
20+
</dependencies>
21+
22+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cn.mrdear.springcloud;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
6+
7+
/**
8+
* @author Niu Li
9+
* @since 2017/6/3
10+
*/
11+
@SpringBootApplication
12+
@EnableDiscoveryClient
13+
public class UserConsumeApplication {
14+
15+
public static void main(String[] args) {
16+
SpringApplication.run(UserConsumeApplication.class,args);
17+
}
18+
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cn.mrdear.springcloud.config;
2+
3+
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.client.RestTemplate;
7+
8+
/**
9+
* @author Niu Li
10+
* @since 2017/6/3
11+
*/
12+
@Configuration
13+
//@RibbonClient(name = "user-resporitory",configuration = MyConfig.class)
14+
//可以指定针对某服务器采取指定的配置负载均衡算法,配置文件中配置更加方便
15+
public class BeanConfig {
16+
17+
@Bean
18+
@LoadBalanced //开启客户端负载均衡,自动配置类在LoadBalancerAutoConfiguration中
19+
public RestTemplate restTemplate(){
20+
return new RestTemplate();
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cn.mrdear.springcloud.controller;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.PathVariable;
5+
import org.springframework.web.bind.annotation.RestController;
6+
import org.springframework.web.client.RestTemplate;
7+
8+
import javax.annotation.Resource;
9+
10+
/**
11+
* 用于消费UserService的类
12+
* @author Niu Li
13+
* @since 2017/6/3
14+
*/
15+
@RestController
16+
public class ConsumeUserController {
17+
18+
@Resource
19+
private RestTemplate restTemplate;
20+
21+
@GetMapping("/{id}")
22+
public String findUserById(@PathVariable Long id) {
23+
return restTemplate.getForObject("http://service-user-provider/"+id,String.class);
24+
}
25+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
server:
2+
port: 8082
3+
4+
spring:
5+
application:
6+
name: service-product-consumer-ribbon
7+
8+
eureka:
9+
client:
10+
service-url:
11+
default-zone: http://localhost:8761/eureka/
12+
instance:
13+
prefer-ip-address: true
14+
15+
# 负载均衡配置为随机,不配置默认轮询
16+
user-resporitory:
17+
ribbon:
18+
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

0 commit comments

Comments
 (0)