- Add pom.xml configuration
Description: This service registration and discovery using a Eureka, so consumers need to introduce end eureka, to invoke the service using EurekaClient
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
- Modify application.yml eureka added service registered address, as shown below:
Description: service-url the arrangement of figure 3 is the cluster service address eureka -
ConfigBean Code
: Main code getting RestTemplate, add annotations to achieve @LoadBalanced client load balancingPackage com.thr.springcloud.cfgbean; Import org.springframework.cloud.client.loadbalancer.LoadBalanced; Import org.springframework.context.annotation.Bean; Import org.springframework.context.annotation.Configuration; Import org.springframework.web .client.RestTemplate; Import com.netflix.loadbalancer.IRule; Import com.netflix.loadbalancer.RandomRule; / ** * notes @Configuration Description: applicationContext.xml spring corresponding to the * case added @Configuration i.e. annotated CfgBean class is equivalent to the applicationContext.xml * @author xiongxiaomeng * / @Configuration public class CfgBean { / ** * Use RestTemplate Rest call service * when @LoadBalanced get RestTemplate, adding load balancing Ribbon of configuration * @return Rest service call template * / @Bean @LoadBalanced public RestTemplate getRestTemplate () { return new new RestTemplate (); } / ** * defined load balancing algorithm: * default: polling, the current change: random * @return random algorithm * / @Bean public IRule myrule () { return new new RandomRule (); } }
- Add EurekaClient class master boot configuration:
- Client Access category UserController_Consumer.java
Package com.thr.springcloud.controller; Import java.util.List; 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.RestController; Import org.springframework.web.client.RestTemplate; Import com.thr.springcloud.entities.User; / ** * consumer side RestController * consumption end are those who call the Controller service provider, using RestTemplate way here call * @author xiongxiaomeng * * / @RestController public class UserController_Consumer { // Prior to joining Eureka + Ribbon, using ip + port access method // Private static String REST_URL_PREFIX Final = " HTTP: // localhost : 8001"; // After adding Eureka + Ribbon, you can direct the use of the service name the invocation, no longer need to use ip + port is called Private static Final String REST_URL_PREFIX = "HTTP: // THRCLOUD-DEPT" ; // use restTemplate access restful interface is very simple and crude no brains. (url, requestMap, ResponseBean.class) representing three parameters @Autowired Private RestTemplate _restTemplate; @RequestMapping (value = "/ Consumer / User / the Add" ) public Boolean the Add (the User User) { return _restTemplate.postForObject(REST_URL_PREFIX + "/user/add", user, Boolean.class); } @RequestMapping(value="/consumer/user/get/{userId}") public User get(@PathVariable("userId") Long userId) { return _restTemplate.getForObject(REST_URL_PREFIX + "/user/get/" + userId, User.class); } @SuppressWarnings("unchecked") @RequestMapping(value="/consumer/user/list") public List<User> list() { return _restTemplate.getForObject(REST_URL_PREFIX + "/user/list", List.class); } @RequestMapping(value="/consumer/user/discovery") public Object discovery() { return _restTemplate.getForObject(REST_URL_PREFIX + "/user/discovery", Object.class); } }
springcloud the Ribbon client load balancing configuration uses
Guess you like
Origin www.cnblogs.com/smartbear/p/11706580.html
Ranking