33°

spring cloud 之 Eureka

 

一、eureka 服务端配置

        1、pom依赖 

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-server -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    <version>2.2.0.RELEASE</version>
</dependency>

        2、配置文件

spring.application.name=springcloud-eureka-client
server.port=8000
## 取消服务器的自我注册
eureka.client.register-with-eureka=false
## 不检索服务
eureka.client.fetch-registry=false
## Eureka客户端服务注册地址
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

      3、配置服务启动

@SpringBootApplication
@EnableEurekaServer //开启eureka服务
public class EuretaApplication {
public static void main(String[] args) {
    SpringApplication.run(EuretaApplication.class, args);
    System.out.println("注册服务中心启动。。。。。。。");
}

}

二、eureka 客户端(注册服务)

    1、pom依赖 

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.2.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>2.2.0.RELEASE</version>
</dependency>

     2、配置文件

spring.application.name=spring-cloud-client
server.port=8004
## Eureka客户端服务注册地址
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
## 实现自我注册
eureka.client.register-with-eureka=true
## 检索服务
eureka.client.fetch-registry=true

    3、代码业务

@RestController
public class MemberApiContorller {
@RequestMapping("/getMember")
public String getMember(){

    return "this is member,and server port is :"+serverport;
}

}

    4、客户端启动注册

@SpringBootApplication
@EnableEurekaClient  //开启客户端注册
public class EurekaCliApplication {
public static void main(String[] args) {
    SpringApplication.run(EurekaCliApplication.class, args);
}

}

三、消费者

    1、pom依赖 

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>2.2.0.RELEASE</version>
</dependency>

    2、配置文件

spring.application.name=spring-cloud-order
server.port=8002
## Eureka客户端服务注册地址
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
## 实现自我注册
eureka.client.register-with-eureka=true
## 检索服务
eureka.client.fetch-registry=true

    3、代码业务

@RestController
public class OrderController {
@Autowired
private RestTemplate restTemplate;

@RequestMapping("/getOrder")
public String getOrder(){

        //说明:spring-cloud-client是注册服务的id,不是域名! String url = "http://spring-cloud-client/getMember"; String result = restTemplate.getForObject(url, String.class); System.out.println("订单服务返回:"+result); return result; } }

    4、客户端启动注册

@SpringBootApplication
@EnableEurekaClient
public class EurekaOrderApplication {
public static void main(String[] args) {
    SpringApplication.run(EurekaOrderApplication.class, args);
}

//解决RestTemplate找不到
@Bean
@LoadBalanced //解决负载均衡问题,如果不配置此注解,请求"http://spring-cloud-client/getMember",报找不到此路径错误,加上此注解,

                   // 自动找打服务名称spring-cloud-client对应的服务,并返回正常的结果值 RestTemplate getRestTemplate(){

    return new RestTemplate();
}

}

注:初次研究,欢迎大家指正。

本文由【李升鹏】发布于开源中国,原文链接:https://my.oschina.net/u/3211737/blog/3141840

全部评论: 0

    我有话说: