Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
spring-cloud-netflix
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
openSource
spring-cloud-netflix
Commits
cf615282
Commit
cf615282
authored
Oct 20, 2014
by
Dave Syer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move @Autowired into @Configuration
I prefer to keep the actual components free of autowiring, so they are easier to navigate in an IDE (i.e. you can see how they are constructed)
parent
66b4d4c6
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
49 additions
and
43 deletions
+49
-43
RibbonAutoConfiguration.java
...amework/cloud/netflix/ribbon/RibbonAutoConfiguration.java
+46
-37
RibbonLoadBalancerClient.java
...mework/cloud/netflix/ribbon/RibbonLoadBalancerClient.java
+3
-6
No files found.
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/RibbonAutoConfiguration.java
View file @
cf615282
package
org
.
springframework
.
cloud
.
netflix
.
ribbon
;
package
org
.
springframework
.
cloud
.
netflix
.
ribbon
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.List
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.autoconfigure.AutoConfigureAfter
;
import
org.springframework.boot.autoconfigure.AutoConfigureAfter
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
...
@@ -13,51 +17,56 @@ import org.springframework.web.client.RestTemplate;
...
@@ -13,51 +17,56 @@ import org.springframework.web.client.RestTemplate;
import
com.netflix.loadbalancer.BaseLoadBalancer
;
import
com.netflix.loadbalancer.BaseLoadBalancer
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.List
;
/**
/**
* @author Spencer Gibb
* @author Spencer Gibb
*/
*/
@Configuration
@Configuration
@AutoConfigureAfter
(
EurekaClientAutoConfiguration
.
class
)
@AutoConfigureAfter
(
EurekaClientAutoConfiguration
.
class
)
public
class
RibbonAutoConfiguration
{
public
class
RibbonAutoConfiguration
{
@Autowired
(
required
=
false
)
@Autowired
(
required
=
false
)
private
List
<
BaseLoadBalancer
>
balancers
=
Collections
.
emptyList
();
private
List
<
BaseLoadBalancer
>
balancers
=
Collections
.
emptyList
();
@Autowired
(
required
=
false
)
@Autowired
private
EurekaRibbonClientPreprocessor
clientPreprocessor
;
private
EurekaRibbonClientPreprocessor
clientPreprocessor
;
// TODO: need to find a default for this?
@Autowired
private
SpringClientFactory
springClientFactory
;
@Bean
@ConditionalOnMissingBean
(
RestTemplate
.
class
)
public
RestTemplate
restTemplate
(
RibbonInterceptor
ribbonInterceptor
)
{
RestTemplate
restTemplate
=
new
RestTemplate
();
List
<
ClientHttpRequestInterceptor
>
list
=
new
ArrayList
<>();
list
.
add
(
ribbonInterceptor
);
restTemplate
.
setInterceptors
(
list
);
return
restTemplate
;
}
@Bean
@ConditionalOnMissingBean
(
LoadBalancerClient
.
class
)
public
LoadBalancerClient
loadBalancerClient
()
{
return
new
RibbonLoadBalancerClient
(
clientPreprocessor
,
springClientFactory
,
balancers
);
}
@Bean
public
RibbonInterceptor
ribbonInterceptor
(
LoadBalancerClient
loadBalancerClient
)
{
return
new
RibbonInterceptor
(
loadBalancerClient
);
}
@Configuration
protected
static
class
DefaultRibbonClientPreprocessor
{
@Bean
public
RibbonClientPreprocessor
ribbonClientPreprocessor
()
{
return
new
RibbonClientPreprocessor
()
{
@Override
public
void
preprocess
(
String
serviceId
)
{
// no-op
}
};
}
@Bean
}
@ConditionalOnMissingBean
(
RestTemplate
.
class
)
public
RestTemplate
restTemplate
(
RibbonInterceptor
ribbonInterceptor
)
{
RestTemplate
restTemplate
=
new
RestTemplate
();
List
<
ClientHttpRequestInterceptor
>
list
=
new
ArrayList
<>();
list
.
add
(
ribbonInterceptor
);
restTemplate
.
setInterceptors
(
list
);
return
restTemplate
;
}
@Bean
@ConditionalOnMissingBean
(
LoadBalancerClient
.
class
)
public
LoadBalancerClient
loadBalancerClient
()
{
return
new
RibbonLoadBalancerClient
(
balancers
);
}
@Bean
public
RibbonInterceptor
ribbonInterceptor
(
LoadBalancerClient
loadBalancerClient
)
{
return
new
RibbonInterceptor
(
loadBalancerClient
);
}
@Bean
public
RibbonClientPreprocessor
ribbonClientPreprocessor
()
{
return
new
RibbonClientPreprocessor
()
{
@Override
public
void
preprocess
(
String
serviceId
)
{
//no-op
}
};
}
}
}
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/RibbonLoadBalancerClient.java
View file @
cf615282
...
@@ -4,15 +4,14 @@ import java.util.HashMap;
...
@@ -4,15 +4,14 @@ import java.util.HashMap;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
import
com.netflix.loadbalancer.AbstractLoadBalancer
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.cloud.client.ServiceInstance
;
import
org.springframework.cloud.client.ServiceInstance
;
import
org.springframework.cloud.client.loadbalancer.LoadBalancerClient
;
import
org.springframework.cloud.client.loadbalancer.LoadBalancerClient
;
import
org.springframework.cloud.client.loadbalancer.LoadBalancerRequest
;
import
com.netflix.loadbalancer.AbstractLoadBalancer
;
import
com.netflix.loadbalancer.BaseLoadBalancer
;
import
com.netflix.loadbalancer.BaseLoadBalancer
;
import
com.netflix.loadbalancer.ILoadBalancer
;
import
com.netflix.loadbalancer.ILoadBalancer
;
import
com.netflix.loadbalancer.Server
;
import
com.netflix.loadbalancer.Server
;
import
org.springframework.cloud.client.loadbalancer.LoadBalancerRequest
;
/**
/**
* @author Spencer Gibb
* @author Spencer Gibb
...
@@ -20,15 +19,13 @@ import org.springframework.cloud.client.loadbalancer.LoadBalancerRequest;
...
@@ -20,15 +19,13 @@ import org.springframework.cloud.client.loadbalancer.LoadBalancerRequest;
*/
*/
public
class
RibbonLoadBalancerClient
implements
LoadBalancerClient
{
public
class
RibbonLoadBalancerClient
implements
LoadBalancerClient
{
@Autowired
private
RibbonClientPreprocessor
ribbonClientPreprocessor
;
private
RibbonClientPreprocessor
ribbonClientPreprocessor
;
@Autowired
private
SpringClientFactory
clientFactory
;
private
SpringClientFactory
clientFactory
;
private
Map
<
String
,
ILoadBalancer
>
balancers
=
new
HashMap
<
String
,
ILoadBalancer
>();
private
Map
<
String
,
ILoadBalancer
>
balancers
=
new
HashMap
<
String
,
ILoadBalancer
>();
public
RibbonLoadBalancerClient
(
List
<
BaseLoadBalancer
>
balancers
)
{
public
RibbonLoadBalancerClient
(
RibbonClientPreprocessor
ribbonClientPreprocessor
,
SpringClientFactory
clientFactory
,
List
<
BaseLoadBalancer
>
balancers
)
{
for
(
BaseLoadBalancer
balancer
:
balancers
)
{
for
(
BaseLoadBalancer
balancer
:
balancers
)
{
this
.
balancers
.
put
(
balancer
.
getName
(),
balancer
);
this
.
balancers
.
put
(
balancer
.
getName
(),
balancer
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment