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

7. Service Discovery & Service Consumer Ribbon

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In the article "Service Registration & Service provider", you may have learned how to develop a service provider. In generation, the service provider is usually deployed on the intranet, that is, the server where the service provider is located is completely isolated from the Internet. This article talks about service discovery (service consumer), which is usually deployed on a server connected to the Internet, providing restful interfaces to H5 and App calls.

Service consumers: Service Consumer is essentially an Eureka Client. After startup, it gets the registration information of all instances from the Eureka Server, including IP address, port, and so on, and caches it locally. This information is updated by default every 30 seconds. If communication with Eureka Server is interrupted or Eureka Server is down, Service Consumer can still communicate with Service Provider through local cache.

1. Create a new project sc-eureka-client-consumer-ribbon and use the corresponding pom.xml file

4.0.0 spring-cloud sc-eureka-client-consumer-ribbon 0.0.1-SNAPSHOT jar sc-eureka-client-consumer-ribbon http://maven.apache.org org.springframework.boot spring-boot-starter-parent 2.0.4.RELEASE org.springframework.cloud spring-cloud-dependencies Finchley.RELEASE pom UTF-8 1.8 1.8 org.springframework.cloud spring-cloud-starter-netflix-eureka-client 2.0.1.RELEASE org.springframework.cloud Spring-cloud-starter-netflix-ribbon 2.0.1.RELEASE org.springframework.boot spring-boot-starter-web 2.0.4.RELEASE

Note: spring-cloud-starter-netflix-ribbon is recommended for spring cloud 2.x, and spring-cloud-starter-ribbon has been marked as expired

2. Create a new class ConsumerApplication.java,spring boot startup class

Package sc.consumer;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableEurekaClientpublic class ConsumerApplication {public static void main (String [] args) {SpringApplication.run (ConsumerApplication.class, args);}}

3. Create the class RestTemplateConfig.java, which is a configuration class and mainly returns the org.springframework.web.client.RestTemplate object.

Package sc.consumer;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.client.RestTemplate;@Configurationpublic class RestTemplateConfig {@ Bean @ LoadBalanced public RestTemplate getRestTemplate () {return new RestTemplate ();}

4. Create configuration files bootstrap.yml and application.yml;. Refer to the source code for details.

5. Other class interfaces are as follows

6. Start registry sc-eureka-server and service provider sc-eureka-client-provider. For more information, please refer to "eureka Registry stand-alone" and "Service Registration & Service provider".

7. Start the service consumer sc-eureka-client-consumer-ribbon and verify whether the startup is successful

You can also check whether there is any error in the log.

8. Access relevant APIs

Query:

Http://127.0.0.1:5600/cli/user/getUser/4

List:

Http://127.0.0.1:5600/cli/user/listUser

Add:

Http://127.0.0.1:5600/cli/user/addUser

Update:

Http://127.0.0.1:5600/cli/user/updateUser

Delete:

Http://127.0.0.1:5600/cli/user/deleteUser/6

Https://gitee.com/hjj520/spring-cloud-2.x/tree/master/sc-eureka-client-consumer-ribbon

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