新聞中心
這篇文章給大家分享的是有關Spring Cloud Alibaba如何使用nacos注冊中心的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
創(chuàng)新互聯(lián)建站專注于企業(yè)成都營銷網(wǎng)站建設、網(wǎng)站重做改版、阿克蘇網(wǎng)站定制設計、自適應品牌網(wǎng)站建設、HTML5建站、商城網(wǎng)站開發(fā)、集團公司官網(wǎng)建設、成都外貿(mào)網(wǎng)站建設、高端網(wǎng)站制作、響應式網(wǎng)頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為阿克蘇等各大城市提供網(wǎng)站開發(fā)制作服務。
spring-cloud-alibaba-basis 創(chuàng)建基礎依賴
首先我們創(chuàng)建一個spring-cloud-alibaba-basis 基礎依賴 工程里面制定我們要用到的公用的版本
spring boot 版本 2.1.7.RELEASE
spring cloud 版本 Greenwich.RELEASE
spring cloud 阿里巴巴的版本 2.1.0.RELEASE
Spring IO Platform 版本依賴
4.0.0 com.xian.cloud spring-cloud-alibaba-basis pom 1.0-SNAPSHOT spring cloud alibaba 總pom spring cloud alibaba 教程總pom版本控制 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
Spring IO Platform 這個jar包有興趣的同學可以去關注一下他里面進行了第三方常用jar包的版本管理。每個spring 對應的第三方jar版本都羅列在上面了。這樣方便我們對于第三方jar包的版本管理。#為了解決jar包版本沖突而存在 sping boot 除了提供我們熟知的
這樣我們在新建倆個module
服務提供者 cloud-discovery-server
com.xian.cloud spring-cloud-alibaba-basis 1.0-SNAPSHOT cloud-discovery-server 服務提供者 服務提供者
因為我們在父類的pom里面定義了公共依賴的jar包,所以我們不需要再次引入jar包只需要制定parent pom就可以繼承這些jar包。同樣下面的消費者服務我也會同樣如此利用maven的jar包傳遞方式進行案例的開發(fā)
創(chuàng)建服務啟動類 和對外服務的controller 類
啟動類
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
@SpringBootApplication
public class DiscoveryServerApplication {
public static void main(String[] args) {
SpringApplication.run(DiscoveryServerApplication.class, args);
}
}對外提供服務的http接口
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")
@Slf4j
public class DiscoverCotroller {
/**
* 對外提供的服務 HTTP接口
* @param name
* @return
*/
@GetMapping("/hello")
public String hello(@RequestParam String name) {
log.info("invoked name = " + name);
return "hello " + name;
}
}編寫YAML文件
server: port: 9012 spring: profiles: active: dev application: name: cloud-discovery-server cloud: nacos: discovery: server-addr: 47.99.209.72:8848
服務消費者 cloud-discovery-client
spring-cloud-alibaba-basis com.xian.cloud 1.0-SNAPSHOT 4.0.0 cloud-discovery-client 服務消費者
創(chuàng)建服務啟動類 和調用服務提供者的的controller http接口
啟動類
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
@SpringBootApplication
public class DiscoveryClientApplication {
public static void main(String[] args) {
SpringApplication.run(DiscoveryClientApplication.class, args);
}
}消費者服務接口
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
@Slf4j
public class DiscoveryClientController {
//服務提供者 項目名稱 spring.application.name
public static final String CLOUD_DISCOVERY_SERVER = "cloud-discovery-server";
/**
* 在Spring Cloud Commons中提供了大量的與服務治理相關的抽象接口,包括DiscoveryClient、LoadBalancerClient等。
* 從LoadBalancerClient接口的命名中,是一個負載均衡客戶端的抽象定義
*/
@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 "調用 " + url + ", 返回 : " + result;
}
}編寫YAML文件
server: port: 9011 spring: profiles: active: dev application: name: cloud-discovery-client cloud: nacos: discovery: server-addr: 47.99.209.72:8848
然后啟動服務 查看nacos控制臺 
顯示倆個服務都已經(jīng)注冊到了 nacos注冊中心
然后我們就請求http://localhost:9011/client/test 看看是否能夠顯示我們想要的結果 
到此時已經(jīng)完成了我們注冊中心??梢栽谔峁┓照咛幐淖兌丝谑欠衲苓M行負載均衡
服務端又分別啟動了 9013、9014倆個端口 服務 
再次進行測試http://localhost:9011/client/test

感謝各位的閱讀!關于“Spring Cloud Alibaba如何使用nacos注冊中心”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
文章名稱:SpringCloudAlibaba如何使用nacos注冊中心
分享地址:http://www.dlmjj.cn/article/ijddoh.html


咨詢
建站咨詢
