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
@EnableDiscoveryClient
When 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