In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how Spring Cloud Alibaba uses the nacos registry. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Spring-cloud-alibaba-basis creates basic dependencies
First of all, let's create a spring-cloud-alibaba-basis basic dependency project that defines the common version we want to use.
Spring boot version of 2.1.7.RELEASE
Spring cloud version of Greenwich.RELEASE
Spring cloud Alibaba's version 2.1.0.RELEASE
Spring IO Platform version dependency
4.0.0 com.xian.cloud spring-cloud-alibaba-basis pom 1.0-SNAPSHOT spring cloud alibaba Total pom spring cloud alibaba tutorial Total pom version Control cloud-discovery-server cloud-discovery-client-common UTF-8 1.8 1.8 1.8 2.1.0.RELEASE Greenwich.RELEASE 2.1.7.RELEASE Cairo-SR8 com.alibaba.cloud spring-cloud-alibaba-dependencies ${spring-cloud-alibaba.version} pom import Org.springframework.boot spring-boot-dependencies ${spring-boot.version} pom import org.springframework.cloud spring-cloud-dependencies ${spring -cloud.version} pom import io.spring.platform platform-bom ${spring-platform.version} pom import Com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery org.springframework.boot spring-boot-starter-web org.projectlombok lombok true
Students who are interested in Spring IO Platform jar package can pay attention to the version management of jar packages commonly used by third parties. The third-party jar version for each spring is listed above. This facilitates version management of third-party jar packages. # in order to resolve jar package version conflicts, sping boot has spring-boot-dependencies dependencies in addition to the way we are familiar with it.
So we're building two new module.
Service provider cloud-discovery-server com.xian.cloud spring-cloud-alibaba-basis 1.0-SNAPSHOT cloud-discovery-server service provider service provider
Because we define publicly dependent jar packages in the pom of the parent class, we don't need to introduce jar packages again, we just need to develop parent pom to inherit these jar packages. The same is true for the following consumer services. I will also use maven's jar package delivery method to develop cases.
Create service startup classes and controller classes for external services
Startup class
Package com.xian.cloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;/** * @ Author: xlr * @ Date: Created in 2:44 PM 2019-10-27 * / @ EnableDiscoveryClient@SpringBootApplicationpublic class DiscoveryServerApplication {public static void main (String [] args) {SpringApplication.run (DiscoveryServerApplication.class, args);}}
Http interface providing services to the outside world
Package com.xian.cloud.controller;import lombok.extern.slf4j.Slf4j;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController / * @ Author: xlr * @ Date: Created in 2:57 PM 2019-10-27 * / @ RestController@RequestMapping ("server") @ Slf4jpublic class DiscoverCotroller {/ * external service HTTP interface * @ param name * @ return * / @ GetMapping ("/ hello") public String hello (@ RequestParam String name) {log.info ("invoked name =" + name); return "hello" + name }}
Write YAML files
Server: port: 9012spring: profiles: active: dev application: name: cloud-discovery-server cloud: nacos: discovery: server-addr: 47.99.209.72 profiles 8848 Service Consumer cloud-discovery-client spring-cloud-alibaba-basis com.xian.cloud 1.0-SNAPSHOT 4.0.0 cloud-discovery-client Service Consumer
Create the service startup class and invoke the controller http interface of the service provider
Startup class
Import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;/** * @ Author: xlr * @ Date: Created in 3:03 PM 2019-10-27 * / @ EnableDiscoveryClient@SpringBootApplicationpublic class DiscoveryClientApplication {public static void main (String [] args) {SpringApplication.run (DiscoveryClientApplication.class, args);}}
Consumer service interface
Package com.xian.cloud.controller;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cloud.client.ServiceInstance;import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate / * * @ Author: xlr * @ Date: Created in 3:04 PM 2019-10-27 * / @ RequestMapping ("client") @ RestController@Slf4jpublic class DiscoveryClientController {/ / Service provider project name spring.application.name public static final String CLOUD_DISCOVERY_SERVER = "cloud-discovery-server"; / * * provides a large number of abstract interfaces related to service governance in Spring Cloud Commons, including DiscoveryClient, LoadBalancerClient, etc. * from the naming of the LoadBalancerClient interface, it is an abstract definition of a load balancing client * / @ Autowired private LoadBalancerClient loadBalancerClient; @ RequestMapping (value = "/ test", method = RequestMethod.GET) public String test () {ServiceInstance serviceInstance = loadBalancerClient.choose (CLOUD_DISCOVERY_SERVER); log.info ("ServiceInstance: {}", serviceInstance); String url = serviceInstance.getUri () + "/ server/hello?name=" + "tom" RestTemplate restTemplate = new RestTemplate (); String result = restTemplate.getForObject (url, String.class); return "call" + url + ", return:" + result;}} "
Write YAML files
Server: port: 9011spring: profiles: active: dev application: name: cloud-discovery-client cloud: nacos: discovery: server-addr: 47.99.209.72:8848
Then start the service to view the nacos console
Shows that both services are registered with the nacos registry
Then we asked http://localhost:9011/client/test to see if we could show the results we wanted.
By this time our registry has been completed. You can change whether the port can be load balanced at the service provider.
The server starts two port services of 9013 and 9014 respectively.
Test http://localhost:9011/client/test again
Thank you for reading! This is the end of the article on "how Spring Cloud Alibaba uses the nacos registry". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.