参考nacos学习笔记
启动nacos注册中心
springcloud-2.0-feign-parent
├── springcloud-2.0-feign-member
│ ├── springcloud-2.0-feign-member-api
│ ├── springcloud-2.0-feign-member-service
│ ├── pom.xml
├── springcloud-2.0-feign-order
│ ├── springcloud-2.0-feign-order-api
│ ├── springcloud-2.0-feign-order-service
│ ├── pom.xml
├── pom.xml
pom.xml
<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.0modelVersion>
<groupId>com.minegroupId>
<artifactId>springcloud-2.0-feign-parentartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>pompackaging>
<modules>
<module>springcloud-2.0-feign-membermodule>
<module>springcloud-2.0-feign-ordermodule>
modules>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.3.RELEASEversion>
parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Finchley.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
<version>2.0.1.RELEASEversion>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
<version>2.0.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
dependencies>
project>
pom.xml
<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.0modelVersion>
<parent>
<groupId>com.minegroupId>
<artifactId>springcloud-2.0-feign-parentartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<artifactId>springcloud-2.0-feign-memberartifactId>
<packaging>pompackaging>
<modules>
<module>springcloud-2.0-feign-member-apimodule>
<module>springcloud-2.0-feign-member-servicemodule>
modules>
project>
pom.xml
<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.0modelVersion>
<parent>
<groupId>com.minegroupId>
<artifactId>springcloud-2.0-feign-memberartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<artifactId>springcloud-2.0-feign-member-apiartifactId>
project>
MemberModel
package com.mine.service.model;
import lombok.Data;
@Data
public class MemberModel {
public String name;
public Integer age;
}
MemberService
package com.mine.service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.mine.service.model.MemberModel;
public interface MemberService {
@RequestMapping("/getMember")
public MemberModel getMember(@RequestParam("name") String name);
}
pom.xml
<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.0modelVersion>
<parent>
<groupId>com.minegroupId>
<artifactId>springcloud-2.0-feign-memberartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<artifactId>springcloud-2.0-feign-member-serviceartifactId>
<dependencies>
<dependency>
<groupId>com.minegroupId>
<artifactId>springcloud-2.0-feign-member-apiartifactId>
<version>${project.version}version>
dependency>
dependencies>
project>
bootstrap.properties
server.port=8000
spring.application.name=nacos-member
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
#spring.cloud.nacos.config.file-extension=yml
#spring.cloud.nacos.config.group=dev-group
spring.cloud.nacos.config.shared-dataids=common.properties
MemberServiceImpl
package com.mine.service.impl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.mine.service.MemberService;
import com.mine.service.model.MemberModel;
@RestController
public class MemberServiceImpl implements MemberService {
@Value("${server.port}")
private String port;
@RequestMapping("/getMember")
public MemberModel getMember(@RequestParam("name") String name) {
System.out.println("i am from " + port);
MemberModel memberModel = new MemberModel();
memberModel.setName(name);
memberModel.setAge(10);
return memberModel;
}
}
NacosAppMember
package com.mine;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NacosAppMember {
public static void main(String[] args) {
SpringApplication.run(NacosAppMember.class, args);
}
}
pom.xml
<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.0modelVersion>
<parent>
<groupId>com.minegroupId>
<artifactId>springcloud-2.0-feign-parentartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<artifactId>springcloud-2.0-feign-orderartifactId>
<packaging>pompackaging>
<modules>
<module>springcloud-2.0-feign-order-apimodule>
<module>springcloud-2.0-feign-order-servicemodule>
modules>
project>
pom.xml
<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.0modelVersion>
<parent>
<groupId>com.minegroupId>
<artifactId>springcloud-2.0-feign-orderartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<artifactId>springcloud-2.0-feign-order-apiartifactId>
project>
OrderService
package com.mine.service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
public interface OrderService {
@RequestMapping("/gerMemberFromOrder")
public String gerMemberFromOrder(@RequestParam("name") String name);
}
pom.xml
<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.0modelVersion>
<parent>
<groupId>com.minegroupId>
<artifactId>springcloud-2.0-feign-orderartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<artifactId>springcloud-2.0-feign-order-serviceartifactId>
<dependencies>
<dependency>
<groupId>com.minegroupId>
<artifactId>springcloud-2.0-feign-order-apiartifactId>
<version>${project.version}version>
dependency>
<dependency>
<groupId>com.minegroupId>
<artifactId>springcloud-2.0-feign-member-apiartifactId>
<version>${project.version}version>
dependency>
dependencies>
project>
bootstrap.properties
server.port=8010
spring.application.name=nacos-order
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
#spring.cloud.nacos.config.file-extension=yml
#spring.cloud.nacos.config.group=dev-group
spring.cloud.nacos.config.shared-dataids=common.properties
MemberServiceFeign
package com.mine.service.feign.member;
import org.springframework.cloud.openfeign.FeignClient;
import com.mine.service.MemberService;
@FeignClient("nacos-member")
public interface MemberServiceFeign extends MemberService {
}
OrderServiceImpl
package com.mine.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.mine.service.OrderService;
import com.mine.service.feign.member.MemberServiceFeign;
@RestController
public class OrderServiceImpl implements OrderService {
@Autowired
private MemberServiceFeign memberServiceFeign;
@RequestMapping("/gerMemberFromOrder")
public String gerMemberFromOrder(@RequestParam("name") String name) {
return memberServiceFeign.getMember(name).toString();
}
}
NacosAppOrder
package com.mine;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NacosAppOrder {
public static void main(String[] args) {
SpringApplication.run(NacosAppOrder.class, args);
}
}
启动nacos注册中心
启动NacosAppOrder与NacosAppMember
注意:feign客户端调用,默认支持ribbon客户端负载均衡,所以订单中心与会员中心都可以修改端口号启动多台实现负载均衡效果。