Man of few words said, or to attach Gangster blog: https: //blog.csdn.net/springcyb/article/details/89147639
This time we use to record what consumers, on top of this blog is not for the novice, I feel particularly good, because according to the above operation will encounter many pit, blogger blog no problem but there are some elements did not write clearly, if novice own brain supplement will not debug
1. Continue to create a spring initialzr project on a project, named spring_ribbon
2. Configure port
server: port: 8021 spring: application: name: springcloud-myribbon eureka: client: service-url: defaultZone: http://localhost:8000/eureka
3. Modify the main java file
In the startup class project, the registration by @EnableDiscoveryClient to the service center; and to inject a bean ioc program: restTemplate; and by @LoadBalanced annotation indicates that the load balancing function is turned restRemplate
package com.example.springcloud_myribbon; 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 SpringcloudMyribbonApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudMyribbonApplication.class, args); } // The spring is injected into the vessel RestTemplate // LoadBalanced load balancing default polling // by implanting producer can use the call instance the controller @Bean @LoadBalanced RestTemplate RestTemplate () { return new new RestTemplate (); } }
3. Consumers calling producer
Before producers springcloud-provider operations, the establishment of a UserController class, must see the following directory structure this is very important, very important, very important
UserController bags and SpringcloudProviderApplication located in the same directory, SpringcloudProviderApplication at run-time configuration file only under its own subdirectory
code show as below
package com.example.springcloudprovider.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class UserController { /** * 测试 * * @return */ @GetMapping("testPro") @ResponseBody public String test() { System.out.println("ribbon调用生产者成功"); return "success"; } }
Began operating the new spring_ribbon
Also the UserController a new, referring to the above directory structure, the directory structure is important to remember, as follows
package com.example.springcloud_myribbon.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class UserController { @Autowired private RestTemplate restTemplate; /** * 调用生产者 */ //调用请求 //get 查 //Post 增 //delete 删 //put 改 @GetMapping("testPro") public String select(){ String result = restTemplate.getForObject("http://SPRINGCLOUD-USER-REG/testPro",String.class); return result; } }
4. Run the project, and then open the following results appear http://127.0.0.1:8021/testPro
5 Error summary
Some annotation errors if the display is 404 illustrates the path during the call, in addition to the problem, there may be wrongly addressed, it is also possible when the main flow under carefully check
If the problem is 500, I encountered is the problem file directory structure, be sure to remember Controller subdirectory at SpringcloudProviderApplication