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.dev1groupId>
<artifactId>SpringCloudDemoartifactId>
<version>1.0-SNAPSHOTversion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.6.RELEASEversion>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-loggingartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.4version>
<scope>providedscope>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Greenwich.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<repositories>
<repository>
<id>spring-snapshotsid>
<name>Spring Snapshotsname>
<url>http://repo.spring.io/libs-snapshot-localurl>
<snapshots>
<enabled>trueenabled>
snapshots>
repository>
<repository>
<id>spring-milestonesid>
<name>Spring Milestonesname>
<url>http://repo.spring.io/libs-milestone-localurl>
<snapshots>
<enabled>falseenabled>
snapshots>
repository>
<repository>
<id>spring-releasesid>
<name>Spring Releasesname>
<url>http://repo.spring.io/libs-release-localurl>
<snapshots>
<enabled>falseenabled>
snapshots>
repository>
repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshotsid>
<name>Spring Snapshotsname>
<url>http://repo.spring.io/libs-snapshot-localurl>
<snapshots>
<enabled>trueenabled>
snapshots>
pluginRepository>
<pluginRepository>
<id>spring-milestonesid>
<name>Spring Milestonesname>
<url>http://repo.spring.io/libs-milestone-localurl>
<snapshots>
<enabled>falseenabled>
snapshots>
pluginRepository>
pluginRepositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
注意所有的服务名中不出现下划线,必须使用中划线
(1) SpringCloudDemo下创建Eureaka-Center9000子模块
在 SpringCloudDemo 下创建子模块Eureaka-Center9000
(2) 引入maven坐标
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
(3) 配置application.yml
#EurekaServer端口9000
spring:
application:
name: Eureaka-Center9000
server:
port: 9000 #端口 决定后面的管理页面也使用9000端口
#配置eureka server
eureka:
client:
register-with-eureka: false #是否将自己注册到注册中心
fetch-registry: false #是否从eureka中获取注册信息
service-url: #配置暴露给Eureka Client的请求地址
defaultZone: http://127.0.0.1:9000/eureka/
server:
enable-self-preservation: false #关闭自我保护
eviction-interval-timer-in-ms: 4000 #剔除服务间隔
registerWithEureka: 是否将自己注册到Eureka服务中,本身就是所有无需注册
fetchRegistry : 是否从Eureka中获取注册信息
serviceUrlEureka: 客户端与Eureka服务端进行交互的地址
(4) 配置启动类
在 com.dev1.eureka 下创建启动类 Center9000
EnableEurekaServer : 激活Eureka Server端配置
@SpringBootApplication
//激活eureakaserver
@EnableEurekaServer
public class Center9000 {
public static void main(String[] args) {
SpringApplication.run(Center9000.class,args);
}
}
(5)服务注册中心管理后台
打开浏览器访问
http://localhost:9000
访问端口由配置文件中的server port决定
即可进入EurekaServer内置的管理控制台,显示效果如下
服务A注册
(1) 服务模块中引入坐标
在 Service-A9001 的pom文件中添加eureka client的相关坐标
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
(2) 配置application.yml文件
在工程的 application.yml 中添加Eureka Server的主机地址
server:
port: 9001
spring:
application:
name: Service-A9001
eureka:
client:
service-url:
defaultZone: http://localhost:9000/eureka/
(3) 修改启动类添加服务注册注解
从Spring Cloud Edgware版本开始, @EnableDiscoveryClient 或 @EnableEurekaClient 可省略。只需加上相关依赖,并进行相应配置,即可将微服务注册到服务发现组件上。
@SpringBootApplication
//激活eurekaClient
@EnableEurekaClient
//@EnableDiscoveryClient
public class A9001 {
public static void main(String[] args) {
SpringApplication.run(A9001.class,args);
}
}
复制:Eureaka-Center9000,重命名 Eureaka-Center8000
application.yml
spring:
application:
name: Eureka-Center
server:
port: 8000
#配置eureka server
eureka:
instance:
prefer-ip-address: true #使用ip地址注册
instance-id: ${spring.cloud.client.ip-address}:${server.port}#向注册中心中注册服务id
lease-renewal-interval-in-seconds: 5 #向注册中心中注册服务id
lease-expiration-duration-in-seconds: 10 #续约到期的时间
client:
register-with-eureka: false #是否将自己注册到注册中心
fetch-registry: false #是否从eureka中获取注册信息
service-url: #配置暴露给Eureka Client的请求地址
defaultZone: http://127.0.0.1:9000/eureka/
server:
enable-self-preservation: false #关闭自我保护
eviction-interval-timer-in-ms: 4000 #剔除服务间隔
机器2
Eureaka-Center9000 application.yml
spring:
application:
name: Eureka-Center
server:
port: 9000
#配置eureka server
eureka:
instance:
prefer-ip-address: true #使用ip地址注册
instance-id: ${spring.cloud.client.ip-address}:${server.port}#向注册中心中注册服务id
lease-renewal-interval-in-seconds: 5 #向注册中心中注册服务id
lease-expiration-duration-in-seconds: 10 #续约到期的时间
client:
register-with-eureka: false #是否将自己注册到注册中心
fetch-registry: false #是否从eureka中获取注册信息
service-url: #配置暴露给Eureka Client的请求地址
defaultZone: http://127.0.0.1:8000/eureka/
server:
enable-self-preservation: false #关闭自我保护
eviction-interval-timer-in-ms: 4000 #剔除服务间隔
即完成互相注册
其他服务需要修改
defaultZone: http://localhost:9000/eureka/,http://localhost:8000/eureka/
定义Person类
public class Person {
private Long id;
private String name;
//省略get /set
编写Controller
@RestController
@RequestMapping("/person")
public class PersonController {
@RequestMapping(method = RequestMethod.GET,value = "/find/{id}")
public Person find(@PathVariable Long id){
Person person = new Person();
person.setId(id);
person.setName("我是服务A的根据id查询Person信息");
return person;
}
}
(1)在Service-A9001增加业务方法
@RequestMapping(method = RequestMethod.GET,value = "/findnames")
public QueryResult findNames(){
QueryResult qr = new QueryResult();
//创建名单列表
List list = new ArrayList<>();
//循环添加学生
for (long i = 0; i < 10; i++) {
Person person = new Person();
person.setId(1000+i);
person.setName("张"+i);
list.add(person);
}
//封装成一个普通对象
qr.setData(list);
return qr;
}
实体类
public class QueryResult {
private int code = 200;
private Object data;
(2)创建 Cosumer-C9003
@RestController
@RequestMapping("/idcard")
public class IdCardController {
@Autowired
private RestTemplate r ;
@RequestMapping(method = RequestMethod.GET,value = "/createIdcards")
public List createIdcards(){
//访问服务A获取学生的名单列表
QueryResult qr = r.getForObject("http://localhost:9001/person/findnames", QueryResult.class);
List list = new ArrayList();
//把名字取出,名字加上准考证
List ps = (List) qr.getData();
for(LinkedHashMap p:ps){
list.add(p.get("name")+"准考证");
}
return list;
}
}
@SpringBootApplication
//激活eurekaClient
@EnableEurekaClient
//@EnableDiscoveryClient
public class C9003 {
//创建RestTemplate,由@Bean将实例放到spring容器里面
@Bean
RestTemplate restTemplate(){
RestTemplate r = new RestTemplate();
return r;
}
public static void main(String[] args) {
SpringApplication.run(C9003.class,args);
}
}