Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Simple usage of spring-clound

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "simple use of spring-clound", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "the simple use of spring-clound" bar!

1 the first step

1) create an eureka service center / / equivalent to the principle of dubbo using zookeeper

Pom.xml

Org.springframework.cloud spring-cloud-starter-netflix-eureka-server

Application.properties

Eureka.client.service-url.defaultZone= http://localhost:8761/eureka/

Server.port=8001

Eureka.instance.hostname=eureka-server

Eureka.client.register-with-eureka=false

Eureka.client.fetch-registry=false

3) enable eureka @ EnableEurekaServer in the main application

Test result

2 create the application of the service provider

1) pom.xml

Org.springframework.cloud spring-cloud-starter-netflix-eureka-client

Application.properties

Server.port=8002

Spring.application.name=provider

# use the ip address of the service to enter the line

Eureka.instance.prefer-ip-address=true

Eureka.client.service-url.defaultZone: http://127.0.0.1:8761/eureka/

Create the service content @ Service public class BookService {public String getBook () {return "teenager, how are you?" ;}}

4) create controller @ RestController that provides services to the outside world

Public class BookController {

@ AutowiredBookService bookService;@GetMapping ("/ book")

Public String getBook () {

Return bookService.getBook ()

}}

3 create consumers

1) pom.xml

Org.springframework.cloud spring-cloud-starter-netflix-eureka-client

Application.properties

Spring.application.name=consumer

Server.port=8200

Eureka.instance.prefer-ip-address=true

Eureka.client.service-url.defaultZone: http://localhost:8761/eureka/

3) write controller that invokes the service

@ RestController

Public class helloController {

@ AutowiredRestTemplate restTemplate;@GetMapping ("/ hello")

Public String UserBook (String name) {

String s = restTemplate.getForObject ("http://PROVIDER/book",String.class); return name+" purchased "+ s"

}}

4) enable remote invocation of the service @ EnableDiscoveryClient

@ SpringBootApplication

Public class ConsumerApplication {

Public static void main (String [] args) {SpringApplication.run (ConsumerApplication.class, args);} @ Bean@LoadBalancedpublic RestTemplate restTemplate () {

Return new RestTemplate ()

}}

Test results:

Thank you for your reading, the above is the content of "simple use of spring-clound", after the study of this article, I believe you have a deeper understanding of the simple use of spring-clound, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report