SpringCloud注册中心切换nacos
2020-12-26 17:43
211 查看
导读
因为种种原因,现在很多公司微服务的注册中心不在使用eureka,纷纷转向阿里的nacos,公司早就在使用nacos,但是是别人搭建的,出于好奇,今天自己手动创建项目,完整搭建一遍...自己动手丰衣足食
搭建nacos
创建项目
api-product
项目结构
<?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 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>pom.xml
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
项目结构
<?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"> <mode 3ff8 lVersion>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
项目结构
<?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
相关文章推荐
- SpringCloud Alibaba Nacos作为配置中心(四)-----------不同环境下切换配置文件
- Eureka切换Nacos时发现两个注册中心的解决方法
- SpringCloud注册中心Eureka、Consul、Nacos简单对比
- OpenStack Compute节点切换controller的方法
- jq使用自定义属性实现有title的tab切换
- layui-tabs切换如何只触发一次
- 简单的IE浏览器代理切换软件-ezProxySwitch
- 禁止其它用户使用su命令切换用户身份
- redis3.2.5 主从切换
- ViewPage Fragment点击切换
- Linux Mint切换到windows后时间不正常
- 鼠标响应事件 当鼠标经过/移出时切换css
- Android app应用多语言切换功能实现
- 简易版主题切换功能的实现
- android 屏幕方向切换 锁定方向
- 如何实现多风格选择 样式实时切换
- activity动画切换
- JSP中include动作标记实现页面的动态切换
- 在CMD命令行中切换到管理员权限模式
- keepalived 实现主从VIP切换