How provider call customer service registered in Eureka

eureka-server 作为注册中心
yml:
server:
port: 8888

eureka:
instance:
hostname: localhost # 设置主机名
client:
registerWithEureka: false # 是否向 Eureka 注册服务。该应用为服务注册中心,不需要自注册,设置为 false
fetchRegistry: false # 是否检索服务。该应用为服务注册中心,职责为注册和发现服务,无需检索服务,设置为 false
server:
waitTimeInMsWhenSyncEmpty: 0 # 设置同步为空时的等待时间。默认 5 * MINUTES

 
application:
package org.spring.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
* Spring Boot Eureka Server 应用启动类
*
* Created by bysocket on 21/06/17.
*/
@EnableEurekaServer // Eureka Server 标识
@SpringBootApplication // Spring Boot 应用标识
public class EurekaServerApplication {

public static void main(String args) {
// 程序启动入口
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
SpringApplication.run(EurekaServerApplication.class,args);
}
}



 
provider 作为服务提供者
 
yml:
server:
port: 8080

eureka:
client:
service-url:
defaultZone: http://localhost:8888/eureka/

spring:
application:
name: provider-service

application:
/**
* Spring Boot Eureka Server 应用启动类
*
* Created by bysocket on 21/06/17.
*/
@EnableEurekaClient // Eureka Client 标识
@SpringBootApplication // Spring Boot 应用标识
public class ProviderApplication {

public static void main(String args) {
// 程序启动入口
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
SpringApplication.run(ProviderApplication.class,args);
}
}



 
 
ProviderController:
package org.spring.springboot.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
* Provider HelloWorld 案例
* <p>
* Created by bysocket on 06/22/17.
*/
@RestController
public class ProviderController {

private static final Logger LOGGER = LoggerFactory.getLogger(ProviderController.class);

@Autowired
private DiscoveryClient discoveryClient;

@Autowired
private Registration registration;

@RequestMapping("/provider")
public String provider() {
ServiceInstance instance = serviceInstance();
LOGGER.info("provider service, host = " + instance.getHost()
+ ", service_id = " + instance.getServiceId());
return "Hello,Provider!";
}

public ServiceInstance serviceInstance() {
List<ServiceInstance> list = discoveryClient.getInstances(registration.getServiceId());
if (list != null && list.size() > 0) {
return list.get(0);
}
return null;
}
}

问题来了:
那服务消费者 customer 工程,怎么调用 provider 工程里面提供的 http://localhost:8080/provider 服务呢?
 
 
已解决:
customer 工程如下
没问题了,DD 书上写的。
通过 Ribbon 负载服务消费完成。
代码如下:
yml:
server:
port: 8081

eureka:
client:
service-url:
defaultZone: http://localhost:8888/eureka/

spring:
application:
name: customer-service

application:
/**
* Spring Boot Eureka Server 应用启动类
*
* Created by bysocket on 21/06/17.
*/
@EnableDiscoveryClient // Eureka Discovery Client 标识
@SpringBootApplication // Spring Boot 应用标识
public class CustomerApplication {

@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}

public static void main(String[] args) {
// 程序启动入口
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
SpringApplication.run(CustomerApplication.class,args);
}
}

controller:
/**
* Customer HelloWorld 案例
* <p>
* Created by bysocket on 06/22/17.
*/
@RestController
public class CustomerController {

private static final Logger LOGGER = LoggerFactory.getLogger(CustomerController.class);

@Autowired
private RestTemplate restTemplate;

@RequestMapping("/customer")
public String customer() {
String providerMsg = restTemplate.getForEntity("http://PROVIDER-SERVICE/provider",
String.class).getBody();

return "Hello,Customer! msg from provider : <br/><br/> " + providerMsg;
}
}
已邀请:

泥瓦匠BYSocket - bysocket.com

赞同来自: itmuch.com

没问题了,DD 书上写的。
通过 Ribbon 负载服务消费完成。
代码如下:
yml:
server:
port: 8081

eureka:
client:
service-url:
defaultZone: http://localhost:8888/eureka/

spring:
application:
name: customer-service

application:
/**
* Spring Boot Eureka Server 应用启动类
*
* Created by bysocket on 21/06/17.
*/
@EnableDiscoveryClient // Eureka Discovery Client 标识
@SpringBootApplication // Spring Boot 应用标识
public class CustomerApplication {

@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}

public static void main(String[] args) {
// 程序启动入口
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
SpringApplication.run(CustomerApplication.class,args);
}
}

controller:
/**
* Customer HelloWorld 案例
* <p>
* Created by bysocket on 06/22/17.
*/
@RestController
public class CustomerController {

private static final Logger LOGGER = LoggerFactory.getLogger(CustomerController.class);

@Autowired
private RestTemplate restTemplate;

@RequestMapping("/customer")
public String customer() {
String providerMsg = restTemplate.getForEntity("http://PROVIDER-SERVICE/provider",
String.class).getBody();

return "Hello,Customer! msg from provider : <br/><br/> " + providerMsg;
}
}

gamei

赞同来自:

看不出问题的关键,访问应该是http://localhost:8080/provider-service/provider吧?

itmuch.com - 《Spring Cloud与Docker微服务架构实战》作者

赞同来自:

英文好好

xiaobaxi - Fang Oba

赞同来自:

标题就是英文的,一下子没看懂,原谅我英语水平差

程序猿DD - http://blog.didispace.com

赞同来自:

英文好好

要回复问题请先登录注册