-
Notifications
You must be signed in to change notification settings - Fork 59
/
Application.java
executable file
·140 lines (129 loc) · 5.16 KB
/
Application.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Copyright 2019 Wuyi Chen.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.thoughtmechanix.licenses;
import java.util.Collections;
import java.util.List;
import com.thoughtmechanix.licenses.config.ServiceConfig;
import com.thoughtmechanix.licenses.model.License;
import com.thoughtmechanix.licenses.repository.LicenseRepository;
import com.thoughtmechanix.licenses.utils.UserContextInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.sleuth.Sampler;
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.web.client.RestTemplate;
/**
* The bootstrap class for the licensing service.
*
* @author Wuyi Chen
* @date 04/08/2019
* @version 1.0
* @since 1.0
*/
@SpringBootApplication
@EntityScan(basePackageClasses = {License.class})
@EnableJpaRepositories(basePackageClasses = {LicenseRepository.class})
@EnableDiscoveryClient // Enable Spring DiscoveryClient (call services registered with service discovery engine)
@EnableFeignClients // Enable Netflix Feign (call services registered with service discovery engine)
@EnableCircuitBreaker // Enable Hystrix
@EnableEurekaClient
@RefreshScope
public class Application {
@Autowired
private ServiceConfig serviceConfig;
/**
* Replace the default Sampler by {@code AlwaysSampler}.
*
* <p>The purpose of this function is to send all the tracing data to
* the Zipkin server, otherwise only 10% tracing data will be sent.
*
* @return The {@code AlwaysSampler} object to replace the default
* Sampler.
*/
@Bean
public Sampler defaultSampler() {
return new AlwaysSampler();
}
/**
* Build the connection to Redis server.
*
* @return The object of {@code JedisConnectionFactory}.
*/
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory jedisConnFactory = new JedisConnectionFactory();
jedisConnFactory.setHostName(serviceConfig.getRedisServer());
jedisConnFactory.setPort(serviceConfig.getRedisPort());
return jedisConnFactory;
}
/**
* Create a {@code RedisTemplate} object for executing operations with
* Redis.
*
* @return The object of {@code RedisTemplate}.
*/
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(jedisConnectionFactory());
return template;
}
/**
* Get a {@code RestTemplate}.
*
* @return The object of {@code RestTemplate}.
*/
@LoadBalanced // This annotation tells Spring Cloud to create a Ribbon backed RestTemplate class.
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
/**
* Inject the access token into the downstream service calls.
*
* @return The {@code RestTemplate} for sending HTTP requests.
*/
@Primary
@Bean
public RestTemplate getCustomRestTemplate() {
RestTemplate template = new RestTemplate();
List<ClientHttpRequestInterceptor> interceptors = template.getInterceptors();
if (interceptors == null) {
template.setInterceptors(Collections.singletonList(new UserContextInterceptor())); // UserContextInterceptor will inject Authentication header in every REST call
} else {
interceptors.add(new UserContextInterceptor());
template.setInterceptors(interceptors);
}
return template;
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}