Function as consumers of access providers (eureka bedding case)

 

1. Create a class entity, such as the provider of this essay's Euraka simple little demo for beginners in some display

2. Create a sub-project as a consumer

(1) add dependencies: remember introducing dependent entity class

<dependencies>
        <dependency>
            <groupId>com.offcn</groupId>
            <artifactId>microservice_cloud_02_api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

 (2) application.yml file set port 80

  (3) create a configuration class

@Configuration
public class ConfigBean {

    @Bean
    public RestTemplate getConfigBean(){
        return new RestTemplate();
    }
}

 (4) Create a controller class that implements the functions of consumer access provider functions

@RestController
public class ProductController_Customer {

    @Autowired
    private RestTemplate restTemplate;

    private static final String REST_URL_PREFIX = "http://localhost:8001";

    @RequestMapping(value = "/customer/product/add")
    public boolean add(Product product){
        return restTemplate.postForObject(REST_URL_PREFIX+"/product/add",product,Boolean.class);
    }

    @RequestMapping(value = "/customer/product/get/{id}")
    public Product get(@PathVariable("id") Long id){
        return restTemplate.getForObject(URI.create(REST_URL_PREFIX+"/product/get/"+id),Product.class);
    }

    @RequestMapping(value = "/customer/product/get/list")
    public List<Product> list(){
        return restTemplate.getForObject(URI.create(REST_URL_PREFIX+"/product/get/list"),List.class);
    }
}

 (5) create a startup class

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class CustomerApplication {
    public static void main(String[] args) {
        SpringApplication.run(CustomerApplication.class,args);
    }
}

 Because this implementation to the next and complex cases, and men more than the amount of time a user configures a lot of configuration class features, too cumbersome, so use it to replace the eureka

Guess you like

Origin www.cnblogs.com/xiaoyuer0506/p/11817425.html