nacos+feign简化代码最终章
创建代码
? nacos-provider服务方,nacos-consumer服务方,nacos-parent父工程
maven坐标
nacos-provider:
<dependencies>
<!--nacos坐标-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.1.0</version>
</dependency>
<!--springboot web启动依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
? nacos-consumer:
<dependencies>
<!--nacos-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.1.0</version>
</dependency>
<!--springboot web启动依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--feign简化代码坐标-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
? nacos-parent:
<!--spring boot 环境 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!--spring cloud 版本-->
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>
<!--引入Spring Cloud 依赖-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
yml
? nacos-provider:
server:
port: 8000
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #配置nacos服务端地址
application:
name: nacos-provider
? nacos-consumer
server:
port: 9000
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #配nacos服务端地址
application:
name: nacos-consumer
服务调用代码
nacos-provider:
domain包下面的Goods类
package com.miracle.nacosprovider.domain;
public class Goods {
private int id;
private String title;
private int price;
private int count;
public Goods() {
}
public Goods(int id, String title, int price, int count) {
this.id = id;
this.title = title;
this.price = price;
this.count = count;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
dao包下面的类GoodsDao
package com.miracle.nacosprovider.dao;
import com.miracle.nacosprovider.domain.Goods;
import org.springframework.stereotype.Repository;
@Repository
public class GoodsDao {
public Goods findOne(int id){
return new Goods(1,"华为手机",8999,10000);
}
}
service包下面的GoodsService类
package com.miracle.nacosprovider.service;
import com.miracle.nacosprovider.dao.GoodsDao;
import com.miracle.nacosprovider.domain.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class GoodsService {
@Autowired
private GoodsDao goodsDao;
public Goods findOne(int id){
return goodsDao.findOne(id);
}
}
controller下面的类GoodsController
package com.miracle.nacosprovider.controller;
import com.miracle.nacosprovider.domain.Goods;
import com.miracle.nacosprovider.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/goods")
public class GoodsController {
@Autowired
private GoodsService goodsService;
@GetMapping("/goods/{id}")
public Goods findOne(@PathVariable("id") int id){
Goods goods = goodsService.findOne(id);
return goods;
}
}
启动类NacosProviderApp
package com.miracle.nacosprovider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApp {
public static void main(String[] args) {
SpringApplication.run(NacosProviderApp.class,args);
}
}
nacos-consumer:
domain和provider一样不变
controller包里面的OrderController类
package com.miracle.nacosconsumer.controller;
import com.miracle.nacosconsumer.domain.Goods;
import com.miracle.nacosconsumer.feign.GoodsFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private GoodsFeignClient goodsFeignClient;
@GetMapping("/goods/{id}")
public Goods findGoodsById(@PathVariable("id") int id){
Goods goods = goodsFeignClient.findGoodsById(id);
return goods;
}
}
feign包下面的GoodsFeignClient接口
package com.miracle.nacosconsumer.feign;
import com.miracle.nacosconsumer.domain.Goods;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(value = "nacos-provider")
public interface GoodsFeignClient {
@GetMapping("/goods/goods/{id}")
public Goods findGoodsById(@PathVariable("id") int id);
}
nacos启动
第一步
Nacos(Dynamic Naming and Configuration Service) 是阿里巴巴2018年7月开源的项目。它专注于服务发现和配置管理领域 致力于帮助您发现、配置和管理微服务。Nacos 支持几乎所有主流类型的“服务”的发现、配置和管理。一句话概括就是Nacos = Spring Cloud注册中心 + Spring Cloud配置中心。
官网:https://nacos.io/ 下载地址: https://github.com/alibaba/nacos/releases
第二步
? 在本地找到下载的nacos找到bin目录下的startup.cmd,双击启动即可,然后打开使用网址为http://http://localhost:8848/nacos,找到下面找到服务列表查看是否成功就可以了
|