SpringCloud註冊中心切換nacos
阿新 • • 發佈:2020-12-26
導讀
因為種種原因,現在很多公司微服務的註冊中心不在使用eureka,紛紛轉向阿里的nacos,公司早就在使用nacos,但是是別人搭建的,出於好奇,今天自己手動建立專案,完整搭建一遍...自己動手豐衣足食
搭建nacos
建立專案
api-product
專案結構
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"pom.xmlxsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ybchen</groupId> <artifactId>api-product</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.2.0.RELEASE</version> </dependency> </dependencies> </project>
IProduct.java
package com.ybchen; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; // name=服務名 @FeignClient(name = "service-product") @RequestMapping("/api/v1/product") public interface IProduct { @GetMapping("getData") Object getData(@RequestParam("name") String name); }
service-product
專案結構
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.18.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.ybchen</groupId> <artifactId>service-product</artifactId> <version>0.0.1-SNAPSHOT</version> <name>service-product</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>com.ybchen</groupId> <artifactId>api-product</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--nacos--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>2.0.0.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.0.0.RELEASE</version> </dependency> <!--ribbon--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> <!--openfeign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies> <!--管理cloud--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>pom.xml
application.properties
server.port=8888
#nacos config
spring.cloud.nacos.discovery.server-addr=47.116.143.16:8848
spring.application.name=service-product
bootstrap.properties
#nacos config
spring.cloud.nacos.config.server-addr=47.116.143.16:8848
spring.application.name=service-product
ServiceProductApplication.java
package com.ybchen.serviceproduct;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class ServiceProductApplication {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ServiceProductApplication.class, args);
}
}
IProductImpl.java
package com.ybchen.serviceproduct.service; import com.ybchen.IProduct; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.Map; /** * @Description: * @Author:chenyanbin * @Date:2020/12/26 4:07 下午 * @Versiion:1.0 */ @RestController public class IProductImpl implements IProduct { @Override public Object getData(String name) { Map<String,String> resultMap=new HashMap<>(); resultMap.put("id","1"); resultMap.put("name",name); return resultMap; } }
service-order
專案結構
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.18.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.ybchen</groupId> <artifactId>service-order</artifactId> <version>0.0.1-SNAPSHOT</version> <name>service-order</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>com.ybchen</groupId> <artifactId>api-product</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <!--nacos--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>2.0.0.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.0.0.RELEASE</version> </dependency> <!--ribbon--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> <!--openfeign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies> <!--管理cloud--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>pom.xml
application.properties
server.port=9999 #nacos config spring.cloud.nacos.discovery.server-addr=47.116.143.16:8848 spring.application.name=service-order
bootstrap.properties
#nacos config spring.cloud.nacos.config.server-addr=47.116.143.16:8848 spring.application.name=service-order
ServiceOrderApplication.java
package com.ybchen.serviceorder; import com.ybchen.IProduct; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @EnableFeignClients(clients = { IProduct.class }) @EnableDiscoveryClient @SpringBootApplication public class ServiceOrderApplication { @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(ServiceOrderApplication.class, args); } }
PayVO.java
package com.ybchen.serviceorder.vo; /** * @Description: * @Author:chenyanbin * @Date:2020/12/26 4:11 下午 * @Versiion:1.0 */ public class PayVO { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "PayVO{" + "name='" + name + '\'' + '}'; } }
PayOrderController.java
package com.ybchen.serviceorder.controller; import com.ybchen.IProduct; import com.ybchen.serviceorder.vo.PayVO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import java.util.Map; /** * @Description: * @Author:chenyanbin * @Date:2020/12/26 4:10 下午 * @Versiion:1.0 */ @RestController public class PayOrderController { @Autowired private IProduct product; @PostMapping("pay") public Object pay(@RequestBody PayVO vo) { Object data = product.getData(vo.getName()); Map<String, String> result = (Map) data; result.put("status","支付成功"); return result; } }
專案啟動
測試
專案原始碼
連結: https://pan.baidu.com/s/1EfYjpiJaAKCyuLKnPiqckw 密碼: o3uf