spring cloud 服务提供者

Posted 毛会懂

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring cloud 服务提供者相关的知识,希望对你有一定的参考价值。

1. pom 依赖:


<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>


</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR7</version>
<type>pom</type>
<scope>import</scope>
</dependency>

</dependencies>
</dependencyManagement>
</project>

 

2. 启动类:

@SpringBootApplication
@EnableEurekaClient
public class HelloApplication {

public static void main(String[] args) {
SpringApplication.run(HelloApplication.class, args);
}
}

 

3. 业务逻辑

@RestController
public class HelloController {
private static Logger LOG = LoggerFactory.getLogger(HelloController.class);

@Autowired
private DiscoveryClient client;

@RequestMapping("/hello")
public String hello() throws InterruptedException {
int time = new Random().nextInt(2000);
LOG.info("时长是:{}",time);
Thread.sleep(time);
ServiceInstance instance = client.getLocalServiceInstance();
String msg = "host:" + instance.getHost() + ",端口:" + instance.getPort() + ",serviceId:" + instance.getServiceId();
LOG.info(msg);
return msg;
}
}

 

4. 配置:

server:
port: 8086
spring:
application:
name: hello-service
eureka:
client:
serviceUrl:
defaultZone: http://admin:[email protected]:8761/eureka/,http://admin:[email protected]:8762/eureka/,http://admin:[email protected]:8763/eureka/
instance:
preferIpAddress: true




























































以上是关于spring cloud 服务提供者的主要内容,如果未能解决你的问题,请参考以下文章

spring cloud微服务架构 服务提供者和服务消费者

Spring cloud实战微服——服务提供者

spring cloud 服务提供者

spring cloud(服务注册中心及服务提供者——初学一)

Spring Cloud Eureka 注册中心 服务消费者 服务提供者之间的关系以及高可用之间的联系

Spring Cloud入门程序——注册服务提供者