Springcloud (iii): service delivery and invocation

Source: Internet
Author: User

Tag: COM encoder width sum load balancer. Style in turn INF STC

Services provided

1, POM package configuration
<Dependencies>        <Dependency>            <groupId>Org.springframework.cloud</groupId>            <Artifactid>Spring-cloud-starter-eureka-server</Artifactid>        </Dependency>        <Dependency>            <groupId>Org.springframework.boot</groupId>            <Artifactid>Spring-boot-starter-test</Artifactid>            <Scope>Test</Scope>        </Dependency>    </Dependencies>
2. Configuration files
spring.application.name=spring-cloud-producerserver.port=9000eureka.client.serviceurl.defaultzone=http:// localhost:8000/eureka/
3. Startup class

To add annotations to the startup class @EnableDiscoveryClient

@EnableDiscoveryClientWhen you add annotations, the project has the functionality to register the service. Once the project is started, you can see the Spring-cloud-producer service on the Registration Center page.

4. Controller
@RestController  Public class Hellocontroller {        @RequestMapping ("/hello")    public  String Index (@ Requestparam String name) {        return "Hello" +name+ ", this is first Messge";    }}
Service invocation

1, POM package configuration
<Dependencies>        <Dependency>            <groupId>Org.springframework.cloud</groupId>            <Artifactid>Spring-cloud-starter-feign</Artifactid>        </Dependency>        <Dependency>            <groupId>Org.springframework.cloud</groupId>            <Artifactid>Spring-cloud-starter-eureka-server</Artifactid>        </Dependency>        <Dependency>            <groupId>Org.springframework.boot</groupId>            <Artifactid>Spring-boot-starter-test</Artifactid>            <Scope>Test</Scope>        </Dependency>    </Dependencies>
2. Configuration files
spring.application.name=spring-cloud-consumerserver.port=9001eureka.client.serviceurl.defaultzone=http:// localhost:8000/eureka/
3. Startup class

Start class additions @EnableDiscoveryClient and @EnableFeignClients annotations.

4. Feign Call Implementation

Feign is a declarative Web service client. Using feign can make it easier to write a Web service client by defining an interface and then adding annotations to it, as well as supporting annotations of the JAX-RS standard.

The feign also supports pluggable encoders and decoders. Spring Cloud encapsulates the feign, enabling it to support Spring MVC standard annotations and httpmessageconverters.

The feign can be used in combination with the Eureka and ribbon to support load balancing.

@FeignClient (name= "Spring-cloud-producer")publicinterface  helloremote {     = "/hello")    public string Hello (@RequestParam (value = "Name") string name);} 
5. Web layer Invoke remote service
@RestController  Public class Consumercontroller {    @Autowired    helloremote helloremote;        @RequestMapping ("/hello/{name}")    public string index (@PathVariable ("Name ") string Name) {        return  Helloremote.hello (name);}    }
Test

Start Spring-cloud-eureka, Spring-cloud-producer, spring-cloud-consumer three items in turn

First Enter: http://localhost:9000/hello?name=neo check whether the Spring-cloud-producer service is normal

Return:hello neo,this is first messge

Description Spring-cloud-producer Normal start, the service provided is also normal.

In the browser, enter:http://localhost:9001/hello/neo

Return:hello neo,this is first messge

Indicates that the client has successfully invoked the remote service hello via feign and returned the result to the browser.

Load Balancing

Springcloud (iii): service delivery and invocation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.