org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE
org.springframework.cloud
spring-cloud-dependencies
Finchley.M7
pom
import
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
spring-milestones
Spring Milestones
https://repo.spring.io/libs-milestone
false
application.yml
###服务端口号
server:
port: 8100
###eureka 基本信息配置
eureka:
instance:
###注册到eurekaip地址
hostname: 127.0.0.1
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
###因为自己是为注册中心,不需要自己注册自己
register-with-eureka: false
###因为自己是为注册中心,不需要检索服务
fetch-registry: false
```
启动Eureka服务
//@EnableEurekaServer作用:开启eurekaServer
@EnableEurekaServer
@SpringBootApplication
public class AppEureka {
public static void main(String[] args) {
SpringApplication.run(AppEureka.class, args);
}
}
```
org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE
org.springframework.cloud
spring-cloud-dependencies
Finchley.M7
pom
import
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
spring-milestones
Spring Milestones
https://repo.spring.io/libs-milestone
false
application.yml
###服务启动端口号
server:
port: 8000
###服务名称(服务注册到eureka名称(别名))
spring:
application:
name: app-dawn-member
###服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka
###因为该应用为注册中心,不会注册自己
register-with-eureka: true
###是否需要从eureka上获取注册信息
fetch-registry: true
服务接口
@RestController
public class MemberController {
@RequestMapping("/getMember")
public String getMember() {
return "this is getMember";
}
}
启动提供者服务
@SpringBootApplication
@EnableEurekaClient
public class AppMember {
public static void main(String[] args) {
SpringApplication.run(AppMember.class, args);
}
}
5. 服务消费者
Maven依赖信息
org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE
org.springframework.cloud
spring-cloud-dependencies
Finchley.M7
pom
import
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
spring-milestones
Spring Milestones
https://repo.spring.io/libs-milestone
false
application.yml
###服务启动端口号
server:
port: 8001
###服务名称(服务注册到eureka名称)
spring:
application:
name: app-dawn-order
###服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka
###因为该应用为注册中心,不会注册自己
register-with-eureka: true
###是否需要从eureka上获取注册信息
fetch-registry: true
使用rest方式调用服务
@RestController
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/getorder")
public String getOrder() {
// order 使用rpc 远程调用技术 调用 会员服务
String memberUrl = "http://app-dawn-member/getMember";
String result = restTemplate.getForObject(memberUrl, String.class);
System.out.println("会员服务调用订单服务,result:" + result);
return result;
}
}
启动消费者
@SpringBootApplication
@EnableEurekaClient
public class AppOrder {
public static void main(String[] args) {
SpringApplication.run(AppOrder.class, args);
}
//@LoadBanlanced就能让这个RestTemplat在请求时拥有客户负载均衡的能力
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
6. 高可用注册中心
默认情况下Eureka是让服务注册中心不注册自己,高可用实际上将自己作为服务向其他服务注册中心注册自己,这样就可以形成一组相互注册的服务注册中心,从而实现服务清单的相互同步,达到高可用效果
###因为该应用为注册中心,不会注册自己
register-with-eureka: true
###不需要去注册中心上检索服务
fetch-registry: true
7. Eureka集群环境搭建
Eure01配置
###服务端口号
server:
port: 8100
###eureka 基本信息配置
spring:
application:
name: eureka-server
eureka:
instance:
###注册到eurekaip地址
hostname: 127.0.0.1
client:
serviceUrl:
defaultZone: http://127.0.0.1:8200/eureka/
###因为自己是为注册中心,不需要自己注册自己
register-with-eureka: true
###因为自己是为注册中心,不需要检索服务
fetch-registry: true
Eureka02配置
###服务端口号
server:
port: 8200
###eureka 基本信息配置
spring:
application:
name: eureka-server
eureka:
instance:
###注册到eurekaip地址
hostname: 127.0.0.1
client:
serviceUrl:
defaultZone: http://127.0.0.1:8100/eureka/
###因为自己是为注册中心,不需要自己注册自己
register-with-eureka: true
###因为自己是为注册中心,不需要检索服务
fetch-registry: true
客户端继承Eureka集群
server:
port: 8000
spring:
application:
name: app-dawn-member
#eureka:
# client:
# service-url:
# defaultZone: http://localhost:8100/eureka
###集群地址
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka,http://localhost:8200/eureka
register-with-eureka: true
fetch-registry: true